2017-08-28 78 views
0

我有一個活動使用Fotoapparat Library來獲取圖片並將其保存到文件中。接下來,我使用Google Mobile Vision API創建該文件的位圖並檢測文本。我已經使用爲此提供的標準代碼。谷歌手機視覺API沒有檢測到肖像模式下的文字

TextRecognizer ocrFrame = new TextRecognizer.Builder(context).build(); 
    Frame frame = new Frame.Builder().setBitmap(BitmapFactory.decodeFile(pathToPhoto)).build(); 

    SparseArray<TextBlock> sparseTextBlocks = ocrFrame.detect(frame); 
    if (sparseTextBlocks.size() <= 0) 
     return null; 

    ArrayList<TextBlock> textBlocks = new ArrayList<>(); 
    for (int i = 0; i < sparseTextBlocks.size(); i++) { 
     textBlocks.add(sparseTextBlocks.get(sparseTextBlocks.keyAt(i))); 
    } 

的OCR完全在橫向模式,但在縱向模式下幾乎不會檢測到任何文本。我已通過顯示圖像確認圖像未在人像模式下翻轉。它提供了一個垂直圖像。我真的不知道爲什麼會發生這種情況。任何線索?

回答

3

這裏是另一個替代實現移動願景API

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

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

總體而言,您的代碼看起來像fi ne,我認爲這可能是您的圖書館保存圖片的方向可能與Mobile Vision API衝突的方式,嘗試在側面項目或其他圖庫上使用原生android捕獲,如果您的應用程序仍然無法運行,請嘗試將靜止圖片即使他們採取的肖像,可以幫助以及

希望它可以幫助

+1

謝謝!你是對的。 FotoApparat保存了與Mobile Vision API衝突的圖片方向。通過旋轉Matrix來修復它。非常感謝! –

+0

非常感謝你!我的照片也出現了錯誤的旋轉,我終於明白了。太讚了! – Trickzter