Extract text from image in Android using Firebase ML Kit
Firebase ML Kit is a mobile SDK for Android and iOS that enables developers to build apps using machine learning with ease. It has various capabilities including text recognition, image labelling, on-device translation among others. In this post, we are going to create a simple Android app that recognizes text from images we take with our camera. So let’s get started.
Create the project
First off, create a new Android project with an Empty Activity. We are going to use minimum SDK version 23 and our primary language will be Java.
Create the layout
Next, we need to create the layout for our app. The layout looks as below:
Connect your app to Firebase
We need to connect our app to Firebase in order to use the APIs. Android Studio has a built-in Firebase tool for easy linking of your app to Firebase. Click on Tools->Firebase
in your Android Studio top menu. On the sidebar that will pop up, navigate to Firebase ML and Connect to Firebase
then add the dependencies required by clicking on Add Firebase ML to your app
and accepting the changes.
In your build.gradle(app)
file, check for this dependency:
implementation ‘com.google.firebase:firebase-ml-vision:24.0.0’
Also add this line of code to your AndroidManifest.xml
file
<meta-data android:name=”com.google.firebase.ml.vision.DEPENDENCIES” android:value=”ocr”/>
MainActivity
In our MainActivity.java
file we are first going to bind our layout elements which we had an id for each in our layout. Next, we should set a click listener to our button that will perform the necessary operations. The first is checking for the camera permission. Don’t forget to add the permission to your AndroidManifest
:
<uses-permission android:name=”android.permission.CAMERA”/>
In the method onActivityResult()
, we perform the main operations of our app after the permission is granted by the user. We create a FirebaseVisionImage
object from our image/bitmap. We then need to display the recognized text from our image to the TextView in our layout.
This project is hosted on GitHub:
And the Kotlin version: