On Device Text Recognition using Firebase ML Kit in Android {Scratch Series}

Yeah, you heard that right! Our lives just got easier.

Setting up a basic project

implementation ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’

Setting up CameraKit

@BindView(R.id.camView) CameraView mCameraView;
@BindView(R.id.cameraBtn) Button mCameraButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@Override
public void onResume() {
super.onResume();
mCameraView.start();
}
@Override
public void onPause() {
mCameraView.stop();
super.onPause();
}
mCameraView.addCameraKitListener(new CameraKitEventListener() {
@Override
public void onEvent(CameraKitEvent cameraKitEvent) {

}

@Override
public void onError(CameraKitError cameraKitError) {

}

@Override
public void onImage(CameraKitImage cameraKitImage) {

Bitmap bitmap = cameraKitImage.getBitmap();
bitmap = Bitmap.createScaledBitmap(bitmap, mCameraView.getWidth(), mCameraView.getHeight(), false);
}

@Override
public void onVideo(CameraKitVideo cameraKitVideo) {

}
});
mCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCameraView.captureImage();
}
});

Setting up Firebase ML Kit

implementation ‘com.google.firebase:firebase-core:15.0.0’
implementation ‘com.google.firebase:firebase-ml-vision:15.0.0’

Hooking up with Firebase Console

package="com.poojab26.textrecognizer">

Setup the Graphic Overlay utils

Add the Text Recognition logic

<com.poojab26.textrecognizer.GraphicUtils.GraphicOverlay
android:id="@+id/graphic_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
@BindView(R.id.graphic_overlay) GraphicOverlay mGraphicOverlay;
private void runTextRecognition(Bitmap bitmap) {
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
FirebaseVisionTextDetector detector = FirebaseVision.getInstance()
.getVisionTextDetector();
detector.detectInImage(image)
.addOnSuccessListener(
new OnSuccessListener<FirebaseVisionText>() {
@Override
public void onSuccess(FirebaseVisionText texts) {
processTextRecognitionResult(texts);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Task failed with an exception
e.printStackTrace();
}
});
}
private void processTextRecognitionResult(FirebaseVisionText texts) {
List<FirebaseVisionText.Block> blocks = texts.getBlocks();
if (blocks.size() == 0) {
Log.d("TAG", "No text found");
return;
}
mGraphicOverlay.clear();
for (int i = 0; i < blocks.size(); i++) {
List<FirebaseVisionText.Line> lines = blocks.get(i).getLines();
for (int j = 0; j < lines.size(); j++) {
List<FirebaseVisionText.Element> elements = lines.get(j).getElements();
for (int k = 0; k < elements.size(); k++) {
GraphicOverlay.Graphic textGraphic = new TextGraphic(mGraphicOverlay, elements.get(k));
mGraphicOverlay.add(textGraphic);

}
}
}
}
@Override
public void onImage(CameraKitImage cameraKitImage) {

Bitmap bitmap = cameraKitImage.getBitmap();
bitmap = Bitmap.createScaledBitmap(bitmap, mCameraView.getWidth(), mCameraView.getHeight(), false);
mCameraView.stop();
runTextRecognition(bitmap);


}
mCameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGraphicOverlay.clear();
mCameraView.start();

mCameraView.captureImage();

}
});

Your app is ready

{Scratch Series} is a series I’m developing that will focus on building short weekend projects from scratch.

Clap your hands 👏 as many times as you can to show your support!

Hi, I am Pooja Bhaumik. A creative developer but also a logical designer. You can find me on Linkedin or stalk me on GitHub. Or just drop a mail to pbhaumik26@gmail.com if you wish to talk tech with me.

--

--

--

SDE II in Uni Cards | Google Developer Expert @Flutter | Mentor @Mentorcruise & GoogleForStartups | Youtube & Technical Writer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Return Largest Numbers In Arrays from freecodecamp.com

The new exciting technological EPIC

How to Use docker cp to Copy a File from a Docker Container to Host

Executing commands in a shell… how does it work?

How to use different Environment Variables in Angular 13

Angular 13 Environment Variable configuration

2020-21 Transfer Window — Data scraping with Python

A gentle Introduction to SQL

Sudoku : Human vs Machine

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
Pooja Bhaumik

Pooja Bhaumik

SDE II in Uni Cards | Google Developer Expert @Flutter | Mentor @Mentorcruise & GoogleForStartups | Youtube & Technical Writer

More from Medium

How to connect YouTube Data API to Android App

Getting Started With Android Application Development (Part 1)

Android: Handle typing status in a chat app

Dabbling with MongoDB Query API