/ 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)

        android:layout_height="wrap_content" />
        android:layout_height="wrap_content" />

Add Permission

<uses-permission android:name="android.permission.CAMERA" />

and OCR meta-data in AndroidManifest.xml file.

            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")
//  Init camera source to use high resolution and auto focus
        mCameraSource = CameraSource.Builder(applicationContext, textRecognizer)
                .setRequestedPreviewSize(1280, 1024)
surface_camera_preview.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceChanged(p0: SurfaceHolder?, p1: Int, p2: Int, p3: Int) {
override fun surfaceDestroyed(p0: SurfaceHolder?) {
            override fun surfaceCreated(p0: SurfaceHolder?) {
                try {
                    if (isCameraPermissionGranted()) {
                    } else {
                } 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) {

                tv_result.post {
                    val stringBuilder = StringBuilder()
                    for (i in 0 until items.size()) {
                        val item = items.valueAt(i)
                    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


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