0

I am trying to edit the Ocr project here
https://github.com/googlesamples/android-vision/tree/master/visionSamples/ocr-codelab/ocr-reader-complete

I want to send the string to OcrCaptureActivity.java if it is work.

In OcrDetectorProcess.java (Original)

@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    graphicOverlay.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());
            OcrGraphic graphic = new OcrGraphic(graphicOverlay, item);
            graphicOverlay.add(graphic);
        }
    }
}

I changed OcrDetectorProcessor.java

@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    graphicOverlay.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());
            OcrGraphic graphic = new OcrGraphic(graphicOverlay, item);
            graphicOverlay.add(graphic);
        }
    }
    OcrCaptureActivity ocrCaptureActivity = new OcrCaptureActivity();
    ocrCaptureActivity.printtext();

}

I added this code in OcrCaptureActivity.java

public void printtext() {
     textView = findViewById(R.id.resulttext);
    textView.post(new Runnable() {
        @Override
        public void run() {
            textView.setText("fdsfsdfsdf");
        }
    });

}

But the error message was shown as below

java.lang.RuntimeException: Can't create handler inside thread Thread[Thread-6,5,main] that has not called Looper.prepare()
anonymous
  • 228
  • 1
  • 11

1 Answers1

0

You should take a reference of this activity if the activity already exists and choose one of the options displayed in this answer.

If the activity does not exist yet, you could either use the intents or bundles, or even preferences, if it is only a string.

RickertBrandsen
  • 201
  • 4
  • 15