2015-12-07 87 views
4

我是一名學生,也是Android開發的初學者,需要爲我的學校項目的聯繫人管理應用程序執行Android OCR功能。這是將圖像從名稱牌。Android OCR:無法解決getBoundingBox的錯誤

我已經搜索了很多關於這方面的信息的論壇,並找到了幾個很好的例子。

目前我已經在線找到一個開源代碼,不需要設置ndk和環境變量等來設置。但是,當我將它實現到eclipse(juno)時,有一個小錯誤,我不確定如何解決。

我已經搜索了一段時間沒有任何結果。因此,決定在這裏問。以下是有錯誤的代碼。

final class OcrRecognizeAsyncTask extends AsyncTask<Void, Void, Boolean> { 

    // private static final boolean PERFORM_FISHER_THRESHOLDING = false; 
    // private static final boolean PERFORM_OTSU_THRESHOLDING = false; 
    // private static final boolean PERFORM_SOBEL_THRESHOLDING = false; 

    private CaptureActivity activity; 
    private TessBaseAPI baseApi; 
    private byte[] data; 
    private int width; 
    private int height; 
    private OcrResult ocrResult; 
    private long timeRequired; 

    OcrRecognizeAsyncTask(CaptureActivity activity, TessBaseAPI baseApi, byte[] data, int width, int height) { 
    this.activity = activity; 
    this.baseApi = baseApi; 
    this.data = data; 
    this.width = width; 
    this.height = height; 
    } 

    @Override 
    protected Boolean doInBackground(Void... arg0) { 
    long start = System.currentTimeMillis(); 
    Bitmap bitmap = activity.getCameraManager().buildLuminanceSource(data, width, height).renderCroppedGreyscaleBitmap(); 
    String textResult; 

    //  if (PERFORM_FISHER_THRESHOLDING) { 
    //  Pix thresholdedImage = Thresholder.fisherAdaptiveThreshold(ReadFile.readBitmap(bitmap), 48, 48, 0.1F, 2.5F); 
    //  Log.e("OcrRecognizeAsyncTask", "thresholding completed. converting to bmp. size:" + bitmap.getWidth() + "x" + bitmap.getHeight()); 
    //  bitmap = WriteFile.writeBitmap(thresholdedImage); 
    //  } 
    //  if (PERFORM_OTSU_THRESHOLDING) { 
    //  Pix thresholdedImage = Binarize.otsuAdaptiveThreshold(ReadFile.readBitmap(bitmap), 48, 48, 9, 9, 0.1F); 
    //  Log.e("OcrRecognizeAsyncTask", "thresholding completed. converting to bmp. size:" + bitmap.getWidth() + "x" + bitmap.getHeight()); 
    //  bitmap = WriteFile.writeBitmap(thresholdedImage); 
    //  } 
    //  if (PERFORM_SOBEL_THRESHOLDING) { 
    //  Pix thresholdedImage = Thresholder.sobelEdgeThreshold(ReadFile.readBitmap(bitmap), 64); 
    //  Log.e("OcrRecognizeAsyncTask", "thresholding completed. converting to bmp. size:" + bitmap.getWidth() + "x" + bitmap.getHeight()); 
    //  bitmap = WriteFile.writeBitmap(thresholdedImage); 
    //  } 

    try {  
     baseApi.setImage(ReadFile.readBitmap(bitmap)); 
     textResult = baseApi.getUTF8Text(); 
     timeRequired = System.currentTimeMillis() - start; 

     // Check for failure to recognize text 
     if (textResult == null || textResult.equals("")) { 
     return false; 
     } 
     ocrResult = new OcrResult(); 
     ocrResult.setWordConfidences(baseApi.wordConfidences()); 
     ocrResult.setMeanConfidence(baseApi.meanConfidence()); 
     ocrResult.setRegionBoundingBoxes(baseApi.getRegions().getBoxRects()); 
     ocrResult.setTextlineBoundingBoxes(baseApi.getTextlines().getBoxRects()); 
     ocrResult.setWordBoundingBoxes(baseApi.getWords().getBoxRects()); 
     ocrResult.setStripBoundingBoxes(baseApi.getStrips().getBoxRects()); 

     // Iterate through the results. 
     final ResultIterator iterator = baseApi.getResultIterator(); 
     int[] lastBoundingBox; 
     ArrayList<Rect> charBoxes = new ArrayList<Rect>(); 
     iterator.begin(); 
     do { 
      lastBoundingBox =iterator.getBoundingBox(PageIteratorLevel.RIL_SYMBOL); 
      Rect lastRectBox = new Rect(lastBoundingBox[0], lastBoundingBox[1], 
        lastBoundingBox[2], lastBoundingBox[3]); 
      charBoxes.add(lastRectBox); 
     } while (iterator.next(PageIteratorLevel.RIL_SYMBOL)); 
     iterator.delete(); 
     ocrResult.setCharacterBoundingBoxes(charBoxes); 

    } catch (RuntimeException e) { 
     Log.e("OcrRecognizeAsyncTask", "Caught RuntimeException in request to Tesseract. Setting state to CONTINUOUS_STOPPED."); 
     e.printStackTrace(); 
     try { 
     baseApi.clear(); 
     activity.stopHandler(); 
     } catch (NullPointerException e1) { 
     // Continue 
     } 
     return false; 
    } 
    timeRequired = System.currentTimeMillis() - start; 
    ocrResult.setBitmap(bitmap); 
    ocrResult.setText(textResult); 
    ocrResult.setRecognitionTimeRequired(timeRequired); 
    return true; 
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
    super.onPostExecute(result); 

    Handler handler = activity.getHandler(); 
    if (handler != null) { 
     // Send results for single-shot mode recognition. 
     if (result) { 
     Message message = Message.obtain(handler, R.id.ocr_decode_succeeded, ocrResult); 
     message.sendToTarget(); 
     } else { 
     Message message = Message.obtain(handler, R.id.ocr_decode_failed, ocrResult); 
     message.sendToTarget(); 
     } 
     activity.getProgressDialog().dismiss(); 
    } 
    if (baseApi != null) { 
     baseApi.clear(); 
    } 
    } 
} 

誤差駐留在do和while循環:

do { 
      lastBoundingBox = iterator.***getBoundingBox***(PageIteratorLevel.RIL_SYMBOL); 
      Rect lastRectBox = new Rect(lastBoundingBox[0], lastBoundingBox[1], 
        lastBoundingBox[2], lastBoundingBox[3]); 
      charBoxes.add(lastRectBox); 
     } while (iterator.next(PageIteratorLevel.RIL_SYMBOL)); 
     iterator.***delete***(); 
     ocrResult.setCharacterBoundingBoxes(charBoxes); 

在上述代碼爲粗體, getBoundingBox並刪除具有以下錯誤:

「的方法getBoundingBox(INT )未定義爲類型ResultIterator「

」方法getBoundingBox(int)未定義類型ResultIterator「

我已經嘗試了幾種方法,但不能使其工作。 如果有任何解決方案,請幫助我,非常感謝。

+0

你是否含有該文件:https://github.com/rmtheis/tess-two/blob/master/tess-two/src/com/googlecode/tesseract/android/PageIterator.java –

+1

@Donovan問題可能是tesseract庫的來源。 'PageIterator' API在版本之間改變,並不總是有'getBoundingBox'方法。 –

+0

@MorrisonChang嗨,你的意思是將代碼包含在你指向我的項目的鏈接中?我已經嘗試將代碼作爲另一個類添加到項目中,並且getBoundingBox仍然具有相同的錯誤。真的很感謝你的幫助:) –

回答

1

@ SebastianRoth是對的:您使用的預編譯庫已過期。我建議安裝NDK並按照build instructions的OCR引擎。這將重新編譯該庫,包括其getBoundingBox方法。

+0

試過了,現在可以工作了,非常感謝:) –

相關問題