1

I started out by following the Text API tutorial to detect TextBlocks, which worked fine. But I now want to detect text lines, and encountered a problem.

// TODO: Create the TextRecognizer
TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build();

// TODO: Set the TextRecognizer's Processor.
textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay));

textRecognizer.setProcessor can only use TextBlock. Is there any way for it to detect lines?

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
GeeSplit
  • 53
  • 3
  • 12

4 Answers4

3

Click Here, to read full code. Hopefully, it will help you.

   Bitmap bitmap = decodeBitmapUri(this, imageUri);
            if (detector.isOperational() && bitmap != null) {
                Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                SparseArray<TextBlock> textBlocks = detector.detect(frame);
                String blocks = "";
                String lines = "";
                String words = "";
                for (int index = 0; index < textBlocks.size(); index++) {
                    //extract scanned text blocks here
                    TextBlock tBlock = textBlocks.valueAt(index);
                    blocks = blocks + tBlock.getValue() + "\n" + "\n";
                    for (Text line : tBlock.getComponents()) {
                        //extract scanned text lines here
                        lines = lines + line.getValue() + "\n";
                        for (Text element : line.getComponents()) {
                            //extract scanned text words here
                            words = words + element.getValue() + ", ";
                        }
                    }
Shahzad Afridi
  • 2,058
  • 26
  • 24
1

use this one:

List<Line> lines = (List<Line>) text.getComponents();
for(Line elements : lines) {
  Log.i("current lines ", ": " + elements.getValue());
}
Hanzala
  • 1,965
  • 1
  • 16
  • 43
Meraj Ali
  • 11
  • 2
0

This tutorial (https://codelabs.developers.google.com/codelabs/mobile-vision-ocr/#6) says that "The engine puts all the text it recognizes in a TextBlock into one complete sentence, even if it sees the sentence broken over multiple lines."

"You can get the Lines from a TextBlock by calling getComponents, and then you can iterate over each line to get the location and values of the text within it. This lets you put the text in the place it actually appears."

// Break the text into multiple lines and draw each one according to its own bounding box.
List<? extends Text> textComponents = mText.getComponents();
for(Text currentText : textComponents) {
    float left = translateX(currentText.getBoundingBox().left);
    float bottom = translateY(currentText.getBoundingBox().bottom);
    canvas.drawText(currentText.getValue(), left, bottom, sTextPaint);
}
Pedro
  • 391
  • 1
  • 8
  • Yes, that's what it does now. But I want to be able to draw the outlining box around each 'Line' instead of the whole 'TextBlock'. This way the user is able to tap each individual line. – GeeSplit Aug 06 '16 at 21:21
0

The solution I came up with, going from Pedro Madeira's answer, was this:

    List<? extends Text> textComponents = mText.getComponents();
    for (Text currentText : textComponents) {
        RectF rect = new RectF(currentText.getBoundingBox());
        rect.left = translateX(rect.left);
        rect.top = translateY(rect.top);
        rect.right = translateX(rect.right);
        rect.bottom = translateY(rect.bottom);
        canvas.drawRect(rect, sRectPaint);
GeeSplit
  • 53
  • 3
  • 12