OCR [Image to Text Recognition] [Firebase ML]

Diehard04
2 min readMay 25, 2020

Hi guys,

Optical Character Recognition (OCR) is a process of converting the printed text on images into machine-encoded text. Means it can be used for many purposes like recognizing text from images, scanning codes or numbers for particular services.

First create A project in android Studio after that register your app in Firebase console.

Below is some step you have to do in your Firebase console and Android project.

Step 1- Take Camera permission in Android after button click .

https://developer.android.com/training/camera/photobasics#java

Step 2. Create a new project in Firebase Account and your package name in that account. Download the google-service.json file form Firebase and paste in the app level.

Step 3- Add Firebase dependency your gradle file

dependencies {
// ...
implementation 'com.google.firebase:firebase-analytics:15.0.2'
implementation 'com.google.firebase:firebase-ml-vision:15.0.0'

}

Step 4- Add one more dependency in the manifest of your project in Application level.

<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="ocr" />

Now you had added all the required dependency in you project.

Here I am going to share step by step how to write code in your project to get text from image.

Step 1- Get Bitmap from the selected Image.

From the Uri you supposed to convert image into Bitmap foramet and send this bitmap file to convert into FirebaseVisionImage.

Step 2- Convert Bitmap into text

Below is the whole code where you supposed to pass bitmap file in the method.

private void runTextRecognition(Bitmap myBitmap) {
FirebaseVisionImage firebaseVisionImage = FirebaseVisionImage.fromBitmap(myBitmap);
FirebaseVisionTextDetector firebaseVisionTextDetector = FirebaseVision.getInstance().getVisionTextDetector();
firebaseVisionTextDetector.detectInImage(firebaseVisionImage).addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
@Override
public void onSuccess(FirebaseVisionText firebaseVisionText) {
processExtractedText(firebaseVisionText);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("onFail= ", e.getMessage());
Toast.makeText(getActivity(), "Exception = " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}

private void processExtractedText(FirebaseVisionText firebaseVisionText) {
List<FirebaseVisionText.Block> blockList = firebaseVisionText.getBlocks();
if (blockList.size() == 0) {
myTextView.setText(null);
Toast.makeText(getActivity(), "No Text Found On This Image", Toast.LENGTH_SHORT).show();
} else {
for (FirebaseVisionText.Block block : firebaseVisionText.getBlocks()) {
String text = block.getText();
myTextView.setText(text);
}
}
}

myTextView is the textview filed where you can see all the converted text from the image.

So this is the whole code you need to add in your project.

Below is the app link of play store where you can see how it’s working.https://play.google.com/store/apps/details?id=com.diehard04.textreader

--

--

Diehard04

Full time Android Developer, FreeLancern, SpringBoot, Flutter Developer