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.
Set up Preview Layout
In layout file add a SurfaceView to preview the camera and TextView to show the result (detected text)
<uses-permission android:name="android.permission.CAMERA" />
and OCR meta-data in AndroidManifest.xml file.
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.
The sample project is available on my GitHub repo. Don’t forget to give ⭐️
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.
If you like this article don’t forget to👏. Happy coding 😄