0

このsample Android programでは、デバイスのカメラを使用して、com.google.android.gms:play-services-visionライブラリを通じて光学式文字認識を実行します。GoogleサンプルのAndroidコードでOCR Detector.Processorからデータを返す方法

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

上記のプロセスは、OcrCaptureActivityによって開始されています:私は、ロギング使用して識別されたテキストを参照することができる午前visionSamples\ocr-codelab\ocr-reader-complete\app\src\main\java\com\google\android\gms\samples\vision\ocrreader\OcrDetectorProcessor.receiveDetections()

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); 

は、だから我々は "のそれ以上のセットを参照してください「もの」は、アクティビティをクランクアップする典型的な方法ではありません。

この質問はバックOcrCaptureActivityからOcrDetectorProcessorからの結果をフィードする方法についてです。

私はOcrCaptureActivityonActivityResult()を追加しようとしたが、それは発生しません:OcrDetectorProcessorActivityないので、私は単純に新しいテントを作成し、setResult()方法を使用することはできません

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

適時に(Androidの戻るボタンが押されたときに)実行されるOcrDetectorProcessor.release()メソッドがありますが、親プロセスと通信する方法がわかりません。

答えて

0

一般的には、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 
} 
関連する問題