在此示例Android程序中,该设备的相机用于通过com.google.android.gms:play-services-vision
库执行光学角色识别。
在visionSamplesocr-codelabocr-reader-completeappsrcmainjavacomgoogleandroidgmssamplesvisionocrreaderOcrDetectorProcessor.receiveDetections()
中,我可以看到使用日志记录识别的文本:
Log.d("OcrDetectorProcessor", "Text detected! " + item.getValue());
上述过程由OcrCaptureActivity
:
TextRecognizer textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay));
CameraSource mCameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)/* snip */.build();
CameraSourcePreview mPreview = (CameraSourcePreview) findViewById(R.id.preview);
mPreview.start(mCameraSource, mGraphicOverlay);
因此,我们看到上面的"东西"不是您启动活动的典型方法。
这个问题是关于如何从 OcrDetectorProcessor
送回 OcrCaptureActivity
的结果。
我尝试将onActivityResult()
添加到OcrCaptureActivity
,>但不会发射:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v(TAG, ">>>>>>> OnActivityResult intent: " + data);
}
因为OcrDetectorProcessor
不是Activity
,所以我不能简单地创建新意图并使用setResult()
方法。
有一个OcrDetectorProcessor.release()
方法,该方法在正确的时间运行(按下Android返回按钮时),但我不确定如何与父进程进行通信。
通常您需要做的是保存对OcrDetectorProcessor
的引用,然后编写数据检索方法并从OcrCaptureActivity
调用。
因此,将此更改为您的" onCreate()":
//TextRecognizer textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay));
mDetectorProcessor = new OcrDetectorProcessor(mGraphicOverlay);
TextRecognizer textRecognizer.setProcessor(mDetectorProcessor);
然后在您的OcrDetectorProcessor
类中,添加一个数据检索方法,该方法返回您选择的实例变量:
public int[] getResults() {
return new int[] {mFoundResults.size(), mNotFoundResults.size()};
}
然后将此方法添加到OcrCaptureActivity()
:
@Override
public void onBackPressed() {
int[] results = mDetectorProcessor.getResults();
Log.v(TAG, "About to finish OCR. Setting extras.");
Intent data = new Intent();
data.putExtra("totalItemCount", results[0]);
data.putExtra("newItemCount", results[1]);
setResult(RESULT_OK, data);
finish();
super.onBackPressed(); // Needs to be down here
}