0

I've been working with Android Mobile Vision OCR API for a while. Everything is work perfectly until i found that i need to extract just single words from the whole SparseArray (Mobile Vision API default return is a TextBlocks which defined in a SparseArray)

SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);

  for (int i = 0; i < textBlocks.size(); i++) {

       TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
            List<Line> lines = (List<Line>) textBlock.getComponents();
            for (Line line : lines) {
                List<Element> elements = (List<Element>) 
                line.getComponents();
                for (Element element : elements) {
                    word = element.getValue();

                    Log.d(TAG, "word Read : " + word);
                }
            }
        }

When i check

Log.d(TAG, "word Read : " + word);

it print out repeatedly all element in the SparseArray

enter image description here

It seems that i'm asking a not-so-obvious question. But can i extract just a single or couple word from those "words" printed above ? For example, i want to extract the word which has character above 12 and has number in it.

Any help or hints will much Appreciated.

  • When you say `has character above 12` do you mean `has 12 or more characters`? And when you say `has number in it`, do you mean you want to match a specific number or any number at all? – TheWanderer Oct 27 '18 at 23:07
  • Yes, it just a case. What i need is substract a single string from sparseArray which fullfill my certain condition. – Ya'qub Qamarudin Ad Dibiaza Oct 27 '18 at 23:11

2 Answers2

1

You could add logical expression to filter result like below:

    word = element.getValue();
    if (word .length() > 12 && word .matches("[0-9]+")) {
        Log.d(TAG, "word Read : " + word);
    }
navylover
  • 12,383
  • 5
  • 28
  • 41
0

You are running word in a loop that's why it's printing all the values. When you run it only once according to the answer of @navylover you will get a single string. Just remove the for loop

Arahasya
  • 525
  • 3
  • 14