3

I am trying to detect a text with a specific format from a live camera feed and show a toast message when that text is detected automatically. I was able to detect the text and put a box around it. But I'm having a hard time showing that toast message.

This is the receiveDetections method from the Processor

@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    mGraphicOverlay.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());

            // Check if it is the correct format
            if (item.getValue().matches("^\\d{3} \\d{3} \\d{4} \\d{4}")){
                OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
                mGraphicOverlay.add(graphic);

                // Show the toast message

            }
        }


    }
}

-> Showing a toast is not my end goal, If I'm able to fix that I'll fix the main problem. -> I'm building on top of the code labs tutorial for the text vision api

Bereket Gobeze
  • 3,446
  • 3
  • 15
  • 14
  • 1
    You don't mention the issue you're having. If I'm not mistaking, receiveDetections is not called in UI thread, you just need to post a runnable (via runOnUIThread, or via a Handler) on the UI thread to display your toast. – badoualy Jul 27 '17 at 07:04
  • @badoualy I was having this error when trying to show the toast 'Can't create handler inside thread that has not called Looper.prepare()'. – Bereket Gobeze Jul 27 '17 at 18:21
  • Yes, you get this because receiveDetections is not called on the UI thread as mentioned in my first comment – badoualy Jul 28 '17 at 11:05

1 Answers1

7

First pass context to OcrDetectorProcessor class from OcrCaptureActivity and runUiThread from that context. This piece of code show all text at once. If you want to show words one by one you need to split from TextBlock items.

Context context;

OcrDetectorProcessor(GraphicOverlay<OcrGraphic> ocrGraphicOverlay, Context context) {
    mGraphicOverlay = ocrGraphicOverlay;
    this.context = context;
}

@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    mGraphicOverlay.clear();
    final String result;
    String detectedText = "";
    SparseArray<TextBlock> items = detections.getDetectedItems();
    for (int i = 0; i < items.size(); ++i) {

        final TextBlock item = items.valueAt(i);
        OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
        mGraphicOverlay.add(graphic);
        detectedText += item.getValue();
    }
    result = detectedText;
    ((OcrCaptureActivity)context).runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
        }
    });
}
Ozgur
  • 3,738
  • 17
  • 34
VolkanSahin45
  • 1,922
  • 12
  • 25