I am currently writing code that should be able to view a picture of text and then extract the text from the picture for android based devices. I did some research online and found that Google provides their own API called "Mobile Vision" (a package with many items i.e. text recognition, facial recognition, etc). However, in their demos they only demonstrate live text recognition. I was wondering if anyone could give me an example of text recognition on a still image using the Mobile Vision API. Any help is welcome. Thanks.
Asked
Active
Viewed 1.4k times
1 Answers
28
The Google Play Services Mobile Vision API Documentation describes how to do this, you can use the TextRecognizer class to detect text in Frames. Once you have the Bitmap image you can then convert it into a frame and do processing on it. See below for an example.
// imageBitmap is the Bitmap image you're trying to process for text
if(imageBitmap != null) {
TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();
if(!textRecognizer.isOperational()) {
// Note: The first time that an app using a Vision API is installed on a
// device, GMS will download a native libraries to the device in order to do detection.
// Usually this completes before the app is run for the first time. But if that
// download has not yet completed, then the above call will not detect any text,
// barcodes, or faces.
// isOperational() can be used to check if the required native libraries are currently
// available. The detectors will automatically become operational once the library
// downloads complete on device.
Log.w(LOG_TAG, "Detector dependencies are not yet available.");
// Check for low storage. If there is low storage, the native library will not be
// downloaded, so detection will not become operational.
IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;
if (hasLowStorage) {
Toast.makeText(this,"Low Storage", Toast.LENGTH_LONG).show();
Log.w(LOG_TAG, "Low Storage");
}
}
Frame imageFrame = new Frame.Builder()
.setBitmap(imageBitmap)
.build();
SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);
for (int i = 0; i < textBlocks.size(); i++) {
TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
Log.i(LOG_TAG, textBlock.getValue());
// Do something with value
}
}
You also need to ensure you include the mobile vision dependency in the module's build.gradle
dependencies {
compile 'com.google.android.gms:play-services-vision:9.4.0'
}
And also include the following in the app's Android Manifest
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="ocr" />

businesscasual
- 755
- 9
- 14
-
Hi, By using the above code, it not detecting all the lines.I need to get all the lines from a "textBlocks" Object and also need to get all the values from "textBlock".Please Help me ASAP.Thanks in Advance... – Naveen Oct 07 '16 at 11:26
-
1To get lines from a textblock, just do: `List extends Text> textComponents = mText.getComponents(); for(Text currentText : textComponents) { // Do your thing here }` – LomaxOnTheRun Feb 06 '17 at 21:42
-
@can we restrict to read english text only using vision api ? – Android Developer World Sep 02 '17 at 05:16
-
@AndroidDeveloperWorld No, it recognizes 18+ different languages. See which languages are supported here: https://developers.google.com/vision/text-overview – w3bshark Oct 09 '17 at 17:22
-
@w3bshark In my scenario, telugu text should not be read on Aadhar card. It should read only English. – Android Developer World Oct 11 '17 at 07:59
-
can we download mobile vision dependency separately ? – Chamin Thilakarathne Oct 24 '19 at 09:32