Detect Text from Image in Android with Google Mobile Vision API
What if android could read text like us? Have you ever think about it? Is it possible?
The answer is Yes. Now it is very easy with the help of Google Mobile Vision API which is very powerful and reliable Optical character recognition(OCR) library and work most of the android device. This can also be used for live face detection and face tracking along with bar code scanning capabilities. Here is the doc for more details.
Add dependency
implementation 'com.google.android.gms:play-services-vision:15.0.1'
Set up Preview Layout
In layout file add a SurfaceView to preview the camera and TextView to show the result (detected text)
<SurfaceView
android:id="@+id/surface_camera_preview"
android:layout_width="match_parent"
android:layout_height="wrap_content" /><TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Add Permission
<uses-permission android:name="android.permission.CAMERA" />
and OCR meta-data in AndroidManifest.xml file.
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="ocr" />
Set up Camera Preview
First of all we need to initiate TextRecognizer object and check if it is ready or not. We can check this using isOperational method. If it is ready then we will use rear camera and auto focus.
Set up Detector Processor
To detect the text straight from camera we have to implement Detector Processor which will handle text detections when ever they become available.
There are two methods receiveDetections() will receive the text when ever they are ready to use. Here we have shown it in the TextView. Another one is release() which can be use for cleaning up resources.
Source Code
The sample project is available on my GitHub repo. Don’t forget to give ⭐️
Troubleshooting
Mobile Vision API has some dependencies. Without those we can’t use Text Recognizer API. Downloading of those dependencies will start at the time of installation but it may take some time. Till then we can’t use Text Recognizer API so we have to wait until those downloads are finished.
Screen Shot
If you like this article don’t forget to👏. Happy coding 😄