Search code examples
androidandroid-studiotextblock

Text alignment issue with OCR


I created an application for scanning text from an image and write it on a textview ,it worked successfully for single column of data,But in case of multiple columns it read columns randomly and not printing the same way as a table view that the original image appears. I used google vision api and parse the text into a SparseArray of type textblock(SparseArray<Textblock>).

Output screenshot of the scanned image: Output screenshot of the scanned image

 Frame frame=new Frame.Builder().setBitmap(bitmap).build();
                SparseArray<TextBlock> textBlocks = recognizer.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() + ", ";
                        }
                    }
                }
                if (textBlocks.size() == 0) {
                    etresult.setText("Scan Failed: Found nothing to scan");
                } else {
                    etresult.setText(etresult.getText() + "Blocks: " + "\n");
                    etresult.setText(etresult.getText() + blocks + "\n");

                    saveToInternalStorage(bitmap);
                }

Solution

  • After going through above code I can see that the OCR is supposed to scan the image and detect the text not the text alignment like tables and its columns. But you can recognise the alignment by using Bounding boxes which gives top-left,top-right,bottom-left,bottom-right corordinates. Please go through Google cloud vision api documentation. https://cloud.google.com/vision/docs/ocr