【问题标题】:How to return data from OCR Detector.Processor in Google sample Android code如何从 Google 示例 Android 代码中的 OCR Detector.Processor 返回数据
【发布时间】:2017-11-29 21:50:21
【问题描述】:

在这个sample Android program中,设备的摄像头用于通过com.google.android.gms:play-services-vision库进行光学字符识别。

visionSamples\ocr-codelab\ocr-reader-complete\app\src\main\java\com\google\android\gms\samples\vision\ocrreader\OcrDetectorProcessor.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,我不能简单地创建一个新的Intent 并使用setResult() 方法。

有一个 OcrDetectorProcessor.release() 方法,它在正确的时间运行(当按下 Android 后退按钮时),但我不确定如何让它与父进程通信。

【问题讨论】:

    标签: android android-intent android-camera ocr onactivityresult


    【解决方案1】:

    一般你需要做的是保存对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
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-02
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 2016-05-27
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      相关资源
      最近更新 更多