How to extract text from image Android app

MrAnderson1992 picture MrAnderson1992 · May 18, 2016 · Viewed 25.2k times · Source

I am working on a feature for my Android app. I would like to read text from a picture then save that text in a database. Is using OCR the best way? Is there another way? Google suggests in its documentation that NDK should only be used if strictly necessary but what are the downfalls exactly?

Any help would be great.

Image to be OCR'd

enter image description here

enter image description here

enter image description here

Answer

user7176550 picture user7176550 · Jan 5, 2017

you can use google vision library for convert image to text, it will give better output from image. Add below library in build gradle:

   compile 'com.google.android.gms:play-services-vision:10.0.0+'

    TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();

Frame imageFrame = new Frame.Builder()

        .setBitmap(bitmap)                 // your image bitmap
        .build();

String imageText = "";


SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);

for (int i = 0; i < textBlocks.size(); i++) {
    TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
    imageText = textBlock.getValue();                   // return string
}