2017-07-24 54 views
1

我試圖從實時相機饋送中檢測具有特定格式的文本,並在自動檢測到該文本時顯示烤麪包消息。 我能夠檢測文本並在其周圍放置一個框。但我很難表明敬酒信息。從相機識別文本時顯示烤麪包信息

這是從處理器receiveDetections方法

@Override 
public void receiveDetections(Detector.Detections<TextBlock> detections) { 
    mGraphicOverlay.clear(); 
    SparseArray<TextBlock> items = detections.getDetectedItems(); 
    for (int i = 0; i < items.size(); ++i) { 
     TextBlock item = items.valueAt(i); 
     if (item != null && item.getValue() != null) { 
      Log.d("OcrDetectorProcessor", "Text detected! " + item.getValue()); 

      // Check if it is the correct format 
      if (item.getValue().matches("^\\d{3} \\d{3} \\d{4} \\d{4}")){ 
       OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item); 
       mGraphicOverlay.add(graphic); 

       // Show the toast message 

      } 
     } 


    } 
} 

- >顯示舉杯不是我的最終目標,如果我能解決這個問題我會解決的主要問題。 - >我建立的文本視覺API代碼實驗室教程的頂部

+0

你沒有提到你遇到的問題。如果我沒有錯,receiveDetections不會在UI線程中調用,您只需在UI線程上發佈runnable(通過runOnUIThread或通過Handler)以顯示您的Toast。 – badoualy

+0

@badoualy我在嘗試顯示吐司時出現此錯誤'無法在未調用Looper.prepare()'的線程內創建處理程序。 –

+0

是的,你得到這個是因爲在我的第一個評論中提到的UI線程上沒有調用receiveDetections – badoualy

回答

4

第一遍上下文OcrDetectorProcessor類來自這方面OcrCaptureActivityrunUiThread。這段代碼一次顯示所有文本。如果你想逐個顯示單詞,你需要從TextBlock項目中拆分。

Context context; 

OcrDetectorProcessor(GraphicOverlay<OcrGraphic> ocrGraphicOverlay, Context context) { 
    mGraphicOverlay = ocrGraphicOverlay; 
    this.context = context; 
} 

@Override 
public void receiveDetections(Detector.Detections<TextBlock> detections) { 
    mGraphicOverlay.clear(); 
    final String result; 
    String detectedText = ""; 
    SparseArray<TextBlock> items = detections.getDetectedItems(); 
    for (int i = 0; i < items.size(); ++i) { 

     final TextBlock item = items.valueAt(i); 
     OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item); 
     mGraphicOverlay.add(graphic); 
     detectedText += item.getValue(); 
    } 
    result = detectedText; 
    ((OcrCaptureActivity)context).runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(context, result, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
}