2016-07-07 178 views
13

我目前正在编写的代码应该能够查看文本的图片,然后从图片中为基于android的设备提取文本。我在网上做了一些研究,发现Google提供了他们自己的称为“移动视觉”的API(包含许多项目,即文本识别,面部识别等)。但是,在他们的演示中,他们只展示活动文本识别。我想知道是否有人可以给我一个使用Mobile Vision API在静止图像上进行文本识别的例子。欢迎任何帮助。谢谢。谷歌移动视觉文本API示例

回答

21

Google Play服务Mobile Vision API文档介绍了如何执行此操作,您可以使用TextRecognizer类来检测Frames中的文本。一旦你有了位图图像,你可以将它转换成一个框架并对其进行处理。见下面的例子。

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

你还需要确保用户在使用模块的build.gradle

dependencies { 
    compile 'com.google.android.gms:play-services-vision:9.4.0' 
} 

移动愿景依赖,还包括在应用程序的Android清单

<meta-data 
    android:name="com.google.android.gms.vision.DEPENDENCIES" 
    android:value="ocr" /> 
+0

嗨以下,通过使用上面的代码,它没有检测到所有行。我需要从“textBlocks”对象中获取所有行,并且还需要从“textBlock”中获取所有值。请帮助我ASAP.Thanks in Advance ... – Naveen

+0

要从文本块中获取行,只需执行: 'List <?扩展文本> textComponents = mText.getComponents(); for(Text currentText:textComponents){//做你的事在这里}' – TinyDancer

+0

@can我们只能使用vision api来读取英文文本吗? –