/ Android

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.

//  Create text Recognizer
textRecognizer = TextRecognizer.Builder(this).build()
if (!textRecognizer.isOperational) {
            toast("Dependencies are not loaded yet...please try after few moment!!")
            Logger.d("Dependencies are downloading....try after few moment")
            return
        }
//  Init camera source to use high resolution and auto focus
        mCameraSource = CameraSource.Builder(applicationContext, textRecognizer)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setRequestedPreviewSize(1280, 1024)
                .setAutoFocusEnabled(true)
                .setRequestedFps(2.0f)
                .build()
surface_camera_preview.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceChanged(p0: SurfaceHolder?, p1: Int, p2: Int, p3: Int) {
}
override fun surfaceDestroyed(p0: SurfaceHolder?) {
                mCameraSource.stop()
            }
@SuppressLint("MissingPermission")
            override fun surfaceCreated(p0: SurfaceHolder?) {
                try {
                    if (isCameraPermissionGranted()) {
                        mCameraSource.start(surface_camera_preview.holder)
                    } else {
                        requestForPermission()
                    }
                } catch (e: Exception) {
                    toast("Error:  ${e.message}")
                }
            }
        })

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.

textRecognizer.setProcessor(object : Detector.Processor<TextBlock> {
            override fun release() {}

            override fun receiveDetections(detections: Detector.Detections<TextBlock>) {
                val items = detections.detectedItems

                if (items.size() <= 0) {
                    return
                }

                tv_result.post {
                    val stringBuilder = StringBuilder()
                    for (i in 0 until items.size()) {
                        val item = items.valueAt(i)
                        stringBuilder.append(item.value)
                        stringBuilder.append("\n")
                    }
                    tv_result.text = stringBuilder.toString()
                }
            }
        })

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 ⭐️

Screen Shot

1_zOomwEaVQfMt4Iw8kDGc2w

If you like this article don’t forget to👏. Happy coding 😄