Dale
Dale

Reputation: 5825

How to return data from OCR Detector.Processor in Google sample Android code

In this sample Android program, the device's camera is used to perform optical character recognition through the com.google.android.gms:play-services-vision library.

In visionSamples\ocr-codelab\ocr-reader-complete\app\src\main\java\com\google\android\gms\samples\vision\ocrreader\OcrDetectorProcessor.receiveDetections() I am able to see the text that is identified using logging:

Log.d("OcrDetectorProcessor", "Text detected! " + item.getValue());

The above process is started by 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);

So we see that the above set of "stuff" is not your typical way to crank-up an activity.

This question is about how to feed the results from the OcrDetectorProcessor back to OcrCaptureActivity.

I tried adding onActivityResult() to OcrCaptureActivity, but it does not fire:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.v(TAG, ">>>>>>> OnActivityResult intent: " + data);
}

Because OcrDetectorProcessor is not an Activity, I can't simply create a new Intent and use the setResult() method.

There is a OcrDetectorProcessor.release() method, which runs at the right time (when the Android back button is pressed), but I'm not sure how to have it communicate with the parent process.

Upvotes: 0

Views: 746

Answers (1)

Dale
Dale

Reputation: 5825

Generally what you need to do is to save a reference to the OcrDetectorProcessor, then write a data retrieval method and call it from OcrCaptureActivity.

So make this change to your 'onCreate()':

//TextRecognizer textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay));
mDetectorProcessor = new OcrDetectorProcessor(mGraphicOverlay);
TextRecognizer textRecognizer.setProcessor(mDetectorProcessor);

Then in your OcrDetectorProcessor class, add a data retrieval method that returns instance variables of your choice:

public int[] getResults() {
    return new int[] {mFoundResults.size(), mNotFoundResults.size()};
}

Then add this method to 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
}

Upvotes: 1

Related Questions