Java Code Examples for com.google.android.gms.vision.text.TextBlock#getComponents()

The following examples show how to use com.google.android.gms.vision.text.TextBlock#getComponents() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OcrGraphic.java    From flutter_mobile_vision with MIT License 6 votes vote down vote up
/**
 * Draws the text block annotations for position, size, and raw value on the supplied canvas.
 */
@Override
public void draw(Canvas canvas) {
    TextBlock text = textBlock;
    if (text == null) {
        return;
    }

    // Draws the bounding box around the TextBlock.
    RectF rect = new RectF(text.getBoundingBox());
    rect = translateRect(rect);
    canvas.drawRect(rect, rectPaint);

    // Break the text into multiple lines and draw each one according to its own bounding box.
    List<? extends Text> textComponents = text.getComponents();
    for (Text currentText : textComponents) {
        float left = translateX(currentText.getBoundingBox().left);
        float bottom = translateY(currentText.getBoundingBox().bottom);
        if (showText) {
            canvas.drawText(currentText.getValue(), left, bottom, textPaint);
        }
    }
}
 
Example 2
Source File: ImageTextReader.java    From loco-answers with GNU General Public License v3.0 5 votes vote down vote up
public String[] getTextFromBitmap2(Bitmap src) {
    if (textRecognizer.isOperational() && src != null) {
        Frame frame = new Frame.Builder().setBitmap(src).build();
        SparseArray<TextBlock> textBlocks = textRecognizer.detect(frame);
        String blocks = "";
        String lines = "";
        for (int index = 0; index < textBlocks.size(); index++) {
            TextBlock tBlock = textBlocks.valueAt(index);
            blocks = blocks + tBlock.getValue() + "\n";
            for (Text line : tBlock.getComponents()) {
                lines = lines + line.getValue() + "\n";
            }
        }

        if (textBlocks.size() == 0) {
            // Log.d(TAG, "getTextFromBitmap: Scan Failed: Found nothing to scan");
            return new String[]{"Scan Failed: Found nothing to scan"};
        } else {
            String[] textOnScreen = lines.split("\n");
            int lineCount = textOnScreen.length;
            if (lineCount > 3) {
                String question = "";
                for (int i = 0; i < lineCount - 3; i++) {
                    question += textOnScreen[i];
                }
                return new String[]{question, textOnScreen[lineCount - 3], textOnScreen[lineCount - 2], textOnScreen[lineCount - 1]};

            }
            return new String[]{"Scan Failed: Could not read options"};

        }
    } else {
        Log.d(TAG, "getTextFromBitmap: Could not set up the detector!");
        return new String[]{"Scan Failed:  Could not set up the detector!"};
    }
}