[Offline and Free] Recognize Text with ML Kit

  • text recognize (FirebaseVisionText)
  • Extract text from blocks of recognized text
implementation 'com.google.firebase:firebase-ml-vision:24.0.3'
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="ocr" />
Bitmap bitmap = BitmapFactory
.decodeResource(getResources(), R.drawable.my_img);
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance()
.getOnDeviceTextRecognizer();
detector.processImage(image)
.addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
@Override
public void onSuccess(FirebaseVisionText fvt) {
processDocumentTextBlock(fvt);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, e.getMessage());
}
});
private void processDocumentTextBlock(FirebaseVisionText result) {
String resultText = result.getText();
Log.d(TAG, "fullText" + resultText);
for (FirebaseVisionText.TextBlock block: result.getTextBlocks()) {
for(FirebaseVisionText.Line line: block.getLines()) {
String lineText = line.getText();
Log.d(TAG, "---line " + lineText);
for(FirebaseVisionText.Element element: line.getElements()){
String elementText = line.getText();
Log.d(TAG, "---element " + elementText);
}
}
}
}
FirebaseVisionDocumentText            FirebaseVisionText
Block - Block
Paragraph - Line
Word - Element
Symbol RecognizedLanguage

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store