TextRecognizer can only detect TextBlocks

GeeSplit picture GeeSplit · Aug 4, 2016 · Viewed 8.2k times · Source

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?

Answer

Shahzad Afridi picture Shahzad Afridi · Sep 21, 2017

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() + ", ";
                        }
                    }