com.google.android.gms.vision.text.TextBlock Java Examples

The following examples show how to use com.google.android.gms.vision.text.TextBlock. 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: ImageData.java    From PrivacyStreams with Apache License 2.0 7 votes vote down vote up
List<TextBlock> detectTextBlocks(UQI uqi) {
    List<TextBlock> result = new ArrayList<>();
    Bitmap bitmap = this.getBitmap(uqi);
    if (bitmap == null) return result;
    TextRecognizer textRecognizer = new TextRecognizer.Builder(uqi.getContext()).build();
    if (!textRecognizer.isOperational()) {
        Logging.warn("TextRecognizer is not operational");
        textRecognizer.release();
        return result;
    }
    Frame imageFrame = new Frame.Builder().setBitmap(bitmap).build();
    SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);
    for (int i = 0; i < textBlocks.size(); i++) {
        TextBlock textBlock = textBlocks.get(textBlocks.keyAt(i));
        result.add(textBlock);
    }
    textRecognizer.release();
    return result;
}
 
Example #2
Source File: OcrCaptureActivity.java    From Questor with MIT License 6 votes vote down vote up
/**
 * onTap is called to capture the first TextBlock under the tap location and return it to
 * the Initializing Activity.
 *
 * @param rawX - the raw position of the tap
 * @param rawY - the raw position of the tap.
 * @return true if the activity is ending.
 */
private boolean onTap(float rawX, float rawY) {
    OcrGraphic graphic = mGraphicOverlay.getGraphicAtLocation(rawX, rawY);
    TextBlock text = null;
    if (graphic != null) {
        text = graphic.getTextBlock();
        if (text != null && text.getValue() != null) {
            Intent data = new Intent();
            data.putExtra(TextBlockObject, text.getValue());
            setResult(CommonStatusCodes.SUCCESS, data);
            finish();
        }
        else {
            Log.d(TAG, "text data is null");
        }
    }
    else {
        Log.d(TAG,"no text detected");
    }
    return text != null;
}
 
Example #3
Source File: OcrCaptureActivity.java    From Moneycim with MIT License 6 votes vote down vote up
/**
 * onTap is called to capture the first TextBlock under the tap location and return it to
 * the Initializing Activity.
 *
 * @param rawX - the raw position of the tap
 * @param rawY - the raw position of the tap.
 * @return true if the activity is ending.
 */
private boolean onTap(float rawX, float rawY) {
    OcrGraphic graphic = mGraphicOverlay.getGraphicAtLocation(rawX, rawY);
    TextBlock text = null;
    if (graphic != null) {
        text = graphic.getTextBlock();
        if (text != null && text.getValue() != null) {
            Intent data = new Intent();
            data.putExtra(TextBlockObject, text.getValue());
            setResult(CommonStatusCodes.SUCCESS, data);
            finish();
        }
        else {
            Log.d(TAG, "text data is null");
        }
    }
    else {
        Log.d(TAG,"no text detected");
    }
    return text != null;
}
 
Example #4
Source File: OcrCaptureActivity.java    From OCR-Reader with MIT License 6 votes vote down vote up
/**
 * onTap is called to capture the first TextBlock under the tap location and return it to
 * the Initializing Activity.
 *
 * @param rawX - the raw position of the tap
 * @param rawY - the raw position of the tap.
 * @return true if the activity is ending.
 */
private boolean onTap(float rawX, float rawY) {
    OcrGraphic graphic = mGraphicOverlay.getGraphicAtLocation(rawX, rawY);
    TextBlock text = null;
    if (graphic != null) {
        text = graphic.getTextBlock();
        if (text != null && text.getValue() != null) {
            Intent data = new Intent();
            data.putExtra(TextBlockObject, text.getValue());
            setResult(CommonStatusCodes.SUCCESS, data);
            finish();
        }
        else {
            Log.d(TAG, "text data is null");
        }
    }
    else {
        Log.d(TAG,"no text detected");
    }
    return text != null;
}
 
Example #5
Source File: OcrCaptureActivity.java    From android-vision with Apache License 2.0 6 votes vote down vote up
/**
 * onTap is called to speak the tapped TextBlock, if any, out loud.
 *
 * @param rawX - the raw position of the tap
 * @param rawY - the raw position of the tap.
 * @return true if the tap was on a TextBlock
 */
private boolean onTap(float rawX, float rawY) {
    OcrGraphic graphic = graphicOverlay.getGraphicAtLocation(rawX, rawY);
    TextBlock text = null;
    if (graphic != null) {
        text = graphic.getTextBlock();
        if (text != null && text.getValue() != null) {
            Log.d(TAG, "text data is being spoken! " + text.getValue());
            // Speak the string.
            tts.speak(text.getValue(), TextToSpeech.QUEUE_ADD, null, "DEFAULT");
        }
        else {
            Log.d(TAG, "text data is null");
        }
    }
    else {
        Log.d(TAG,"no text detected");
    }
    return text != null;
}
 
Example #6
Source File: OcrCaptureActivity.java    From android-vision with Apache License 2.0 6 votes vote down vote up
/**
 * onTap is called to capture the first TextBlock under the tap location and return it to
 * the Initializing Activity.
 *
 * @param rawX - the raw position of the tap
 * @param rawY - the raw position of the tap.
 * @return true if the activity is ending.
 */
private boolean onTap(float rawX, float rawY) {
    OcrGraphic graphic = mGraphicOverlay.getGraphicAtLocation(rawX, rawY);
    TextBlock text = null;
    if (graphic != null) {
        text = graphic.getTextBlock();
        if (text != null && text.getValue() != null) {
            Intent data = new Intent();
            data.putExtra(TextBlockObject, text.getValue());
            setResult(CommonStatusCodes.SUCCESS, data);
            finish();
        }
        else {
            Log.d(TAG, "text data is null");
        }
    }
    else {
        Log.d(TAG,"no text detected");
    }
    return text != null;
}
 
Example #7
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 #8
Source File: OcrDetectorProcessor.java    From Questor with MIT License 5 votes vote down vote up
/**
 * Called by the detector to deliver detection results.
 * If your application called for it, this could be a place to check for
 * equivalent detections by tracking TextBlocks that are similar in location and content from
 * previous frames, or reduce noise by eliminating TextBlocks that have not persisted through
 * multiple detections.
 */
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    mGraphicOverlay.clear();
    SparseArray<TextBlock> items = detections.getDetectedItems();
    for (int i = 0; i < items.size(); ++i) {
        TextBlock item = items.valueAt(i);
        OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
        mGraphicOverlay.add(graphic);
    }
}
 
Example #9
Source File: OcrGraphic.java    From Document-Scanner with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks whether a point is within the bounding box of this graphic.
 * The provided point should be relative to this graphic's containing overlay.
 *
 * @param x An x parameter in the relative context of the canvas.
 * @param y A y parameter in the relative context of the canvas.
 * @return True if the provided point is contained within this graphic's bounding box.
 */
public boolean contains(float x, float y) {
    TextBlock text = mText;
    if (text == null) {
        return false;
    }
    RectF rect = new RectF(text.getBoundingBox());
    rect.left = translateX(rect.left);
    rect.top = translateY(rect.top);
    rect.right = translateX(rect.right);
    rect.bottom = translateY(rect.bottom);
    return (rect.left < x && rect.right > x && rect.top < y && rect.bottom > y);
}
 
Example #10
Source File: OcrGraphic.java    From OCR-Reader with MIT License 5 votes vote down vote up
/**
 * Checks whether a point is within the bounding box of this graphic.
 * The provided point should be relative to this graphic's containing overlay.
 * @param x An x parameter in the relative context of the canvas.
 * @param y A y parameter in the relative context of the canvas.
 * @return True if the provided point is contained within this graphic's bounding box.
 */
public boolean contains(float x, float y) {
    TextBlock text = mText;
    if (text == null) {
        return false;
    }
    RectF rect = new RectF(text.getBoundingBox());
    rect.left = translateX(rect.left);
    rect.top = translateY(rect.top);
    rect.right = translateX(rect.right);
    rect.bottom = translateY(rect.bottom);
    return (rect.left < x && rect.right > x && rect.top < y && rect.bottom > y);
}
 
Example #11
Source File: MainActivity.java    From text-detector with MIT License 5 votes vote down vote up
public void detectText(View view) {
    Bitmap textBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cat);

    TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();

    if (!textRecognizer.isOperational()) {
        new AlertDialog.Builder(this)
                .setMessage("Text recognizer could not be set up on your device :(")
                .show();
        return;
    }

    Frame frame = new Frame.Builder().setBitmap(textBitmap).build();
    SparseArray<TextBlock> text = textRecognizer.detect(frame);

    for (int i = 0; i < text.size(); ++i) {
        TextBlock item = text.valueAt(i);
        if (item != null && item.getValue() != null) {
            detectedTextView.setText(item.getValue());
        }
    }
}
 
Example #12
Source File: OcrGraphic.java    From Questor with MIT License 5 votes vote down vote up
/**
 * Checks whether a point is within the bounding box of this graphic.
 * The provided point should be relative to this graphic's containing overlay.
 * @param x An x parameter in the relative context of the canvas.
 * @param y A y parameter in the relative context of the canvas.
 * @return True if the provided point is contained within this graphic's bounding box.
 */
public boolean contains(float x, float y) {
    TextBlock text = mText;
    if (text == null) {
        return false;
    }
    RectF rect = new RectF(text.getBoundingBox());
    rect.left = translateX(rect.left);
    rect.top = translateY(rect.top);
    rect.right = translateX(rect.right);
    rect.bottom = translateY(rect.bottom);
    return (rect.left < x && rect.right > x && rect.top < y && rect.bottom > y);
}
 
Example #13
Source File: OcrCaptureActivity.java    From Document-Scanner with GNU General Public License v3.0 5 votes vote down vote up
/**
 * onTap is called to capture the first TextBlock under the tap location and return it to
 * the Initializing Activity.
 *
 * @param rawX - the raw position of the tap
 * @param rawY - the raw position of the tap.
 * @return true if the activity is ending.
 */
private boolean onTap(float rawX, float rawY) {
    OcrGraphic graphic = mGraphicOverlay.getGraphicAtLocation(rawX, rawY);
    TextBlock text = null;
    if (graphic != null) {
        text = graphic.getTextBlock();

        if (text != null && text.getValue() != null) {
            ocr_text.setClickable(true);

            String ocrtext = ocr_text.getText().toString();

            if (!ocrtext.isEmpty()) {
                ocrtext = ocrtext + "\n" + text.getValue();
                ocr_text.setText(ocrtext);
            } else {
                ocr_text.setText(text.getValue());
            }

        } else {
            Log.d(TAG, "text data is null");
        }
    } else {
        Log.d(TAG, "no text detected");
    }
    return text != null;
}
 
Example #14
Source File: TextProcessor.java    From camerakit-android with MIT License 5 votes vote down vote up
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {

    SparseArray<TextBlock> detectedItems = detections.getDetectedItems();
    for (int i = 0; i < detectedItems.size(); ++i) {
        TextBlock item = detectedItems.valueAt(i);
        if (item != null && item.getValue() != null) {
            CameraKitTextDetect event = new CameraKitTextDetect(new CameraKitTextBlock(item));
            mEventDispatcher.dispatch(event);
            callback.callback(event);
        }
    }
}
 
Example #15
Source File: OcrDetectorProcessor.java    From android-vision with Apache License 2.0 5 votes vote down vote up
/**
 * Called by the detector to deliver detection results.
 * If your application called for it, this could be a place to check for
 * equivalent detections by tracking TextBlocks that are similar in location and content from
 * previous frames, or reduce noise by eliminating TextBlocks that have not persisted through
 * multiple detections.
 */
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    graphicOverlay.clear();
    SparseArray<TextBlock> items = detections.getDetectedItems();
    for (int i = 0; i < items.size(); ++i) {
        TextBlock item = items.valueAt(i);
        if (item != null && item.getValue() != null) {
            Log.d("OcrDetectorProcessor", "Text detected! " + item.getValue());
            OcrGraphic graphic = new OcrGraphic(graphicOverlay, item);
            graphicOverlay.add(graphic);
        }
    }
}
 
Example #16
Source File: OcrGraphic.java    From Moneycim with MIT License 5 votes vote down vote up
/**
 * Checks whether a point is within the bounding box of this graphic.
 * The provided point should be relative to this graphic's containing overlay.
 * @param x An x parameter in the relative context of the canvas.
 * @param y A y parameter in the relative context of the canvas.
 * @return True if the provided point is contained within this graphic's bounding box.
 */
public boolean contains(float x, float y) {
    TextBlock text = mText;
    if (text == null) {
        return false;
    }
    RectF rect = new RectF(text.getBoundingBox());
    rect.left = translateX(rect.left);
    rect.top = translateY(rect.top);
    rect.right = translateX(rect.right);
    rect.bottom = translateY(rect.bottom);
    return (rect.left < x && rect.right > x && rect.top < y && rect.bottom > y);
}
 
Example #17
Source File: OcrDetectorProcessor.java    From Document-Scanner with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Called by the detector to deliver detection results.
 * If your application called for it, this could be a place to check for
 * equivalent detections by tracking TextBlocks that are similar in lo
 * ion and content from
 * previous frames, or reduce noise by eliminating TextBlocks that have not persisted through
 * multiple detections.
 */
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    mGraphicOverlay.clear();
    SparseArray<TextBlock> items = detections.getDetectedItems();
    for (int i = 0; i < items.size(); ++i) {
        TextBlock item = items.valueAt(i);
        OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
        mGraphicOverlay.add(graphic);
    }
}
 
Example #18
Source File: OcrDetectorProcessor.java    From Moneycim with MIT License 5 votes vote down vote up
/**
 * Called by the detector to deliver detection results.
 * If your application called for it, this could be a place to icon_save for
 * equivalent detections by tracking TextBlocks that are similar in location and content from
 * previous frames, or reduce noise by eliminating TextBlocks that have not persisted through
 * multiple detections.
 */
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    mGraphicOverlay.clear();
    SparseArray<TextBlock> items = detections.getDetectedItems();
    for (int i = 0; i < items.size(); ++i) {

        final TextBlock item = items.valueAt(i);
        OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
        mGraphicOverlay.add(graphic);
        //detectedText += item.getValue();
    }
}
 
Example #19
Source File: OcrDetectorProcessor.java    From OCR-Reader with MIT License 5 votes vote down vote up
/**
 * Called by the detector to deliver detection results.
 * If your application called for it, this could be a place to check for
 * equivalent detections by tracking TextBlocks that are similar in location and content from
 * previous frames, or reduce noise by eliminating TextBlocks that have not persisted through
 * multiple detections.
 */
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    mGraphicOverlay.clear();
    SparseArray<TextBlock> items = detections.getDetectedItems();
    for (int i = 0; i < items.size(); ++i) {
        TextBlock item = items.valueAt(i);
        OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
        mGraphicOverlay.add(graphic);
    }
}
 
Example #20
Source File: ImageData.java    From PrivacyStreams with Apache License 2.0 5 votes vote down vote up
String detectText(UQI uqi) {
    StringBuilder text = new StringBuilder();
    for (TextBlock textBlock : this.detectTextBlocks(uqi)) {
        text.append(textBlock.getValue()).append("\n");
    }
    return text.toString();
}
 
Example #21
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!"};
    }
}
 
Example #22
Source File: OcrTrackerFactory.java    From flutter_mobile_vision with MIT License 5 votes vote down vote up
@Override
public Tracker<TextBlock> create(TextBlock textBlock) {
    OcrGraphic graphic = new OcrGraphic(graphicOverlay, showText);
    try {
        return new OcrGraphicTracker(graphicOverlay, graphic);
    } catch (Exception ex) {
        Log.d("OcrTrackerFactory", ex.getMessage(), ex);
    }
    return null;
}
 
Example #23
Source File: OcrDetectorProcessor.java    From android-vision with Apache License 2.0 5 votes vote down vote up
/**
 * Called by the detector to deliver detection results.
 * If your application called for it, this could be a place to check for
 * equivalent detections by tracking TextBlocks that are similar in location and content from
 * previous frames, or reduce noise by eliminating TextBlocks that have not persisted through
 * multiple detections.
 */
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
    mGraphicOverlay.clear();
    SparseArray<TextBlock> items = detections.getDetectedItems();
    for (int i = 0; i < items.size(); ++i) {
        TextBlock item = items.valueAt(i);
        OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
        mGraphicOverlay.add(graphic);
    }
}
 
Example #24
Source File: OcrGraphic.java    From android-vision with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether a point is within the bounding box of this graphic.
 * The provided point should be relative to this graphic's containing overlay.
 * @param x An x parameter in the relative context of the canvas.
 * @param y A y parameter in the relative context of the canvas.
 * @return True if the provided point is contained within this graphic's bounding box.
 */
public boolean contains(float x, float y) {
    TextBlock text = mText;
    if (text == null) {
        return false;
    }
    RectF rect = new RectF(text.getBoundingBox());
    rect.left = translateX(rect.left);
    rect.top = translateY(rect.top);
    rect.right = translateX(rect.right);
    rect.bottom = translateY(rect.bottom);
    return (rect.left < x && rect.right > x && rect.top < y && rect.bottom > y);
}
 
Example #25
Source File: OcrGraphic.java    From flutter_mobile_vision with MIT License 5 votes vote down vote up
/**
 * @return RectF that represents the graphic's bounding box.
 */
public RectF getBoundingBox() {
    TextBlock textBlock = this.textBlock;
    if (textBlock == null) {
        return null;
    }
    RectF rect = new RectF(textBlock.getBoundingBox());
    rect = translateRect(rect);
    return rect;
}
 
Example #26
Source File: OcrGraphic.java    From android-vision with Apache License 2.0 4 votes vote down vote up
public TextBlock getTextBlock() {
    return textBlock;
}
 
Example #27
Source File: TextDetectionImpl.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void detect(
        SharedBufferHandle frameData, int width, int height, DetectResponse callback) {
    if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(
                ContextUtils.getApplicationContext())
            != ConnectionResult.SUCCESS) {
        Log.e(TAG, "Google Play Services not available");
        callback.call(new TextDetectionResult[0]);
        return;
    }
    // The vision library will be downloaded the first time the API is used
    // on the device; this happens "fast", but it might have not completed,
    // bail in this case. Also, the API was disabled between and v.9.0 and
    // v.9.2, see https://developers.google.com/android/guides/releases.
    if (!mTextRecognizer.isOperational()) {
        Log.e(TAG, "TextDetector is not operational");
        callback.call(new TextDetectionResult[0]);
        return;
    }

    Frame frame = SharedBufferUtils.convertToFrame(frameData, width, height);
    if (frame == null) {
        Log.e(TAG, "Error converting SharedMemory to Frame");
        callback.call(new TextDetectionResult[0]);
        return;
    }

    final SparseArray<TextBlock> textBlocks = mTextRecognizer.detect(frame);

    TextDetectionResult[] detectedTextArray = new TextDetectionResult[textBlocks.size()];
    for (int i = 0; i < textBlocks.size(); i++) {
        detectedTextArray[i] = new TextDetectionResult();
        detectedTextArray[i].rawValue = textBlocks.valueAt(i).getValue();
        final Rect rect = textBlocks.valueAt(i).getBoundingBox();
        detectedTextArray[i].boundingBox = new RectF();
        detectedTextArray[i].boundingBox.x = rect.left;
        detectedTextArray[i].boundingBox.y = rect.top;
        detectedTextArray[i].boundingBox.width = rect.width();
        detectedTextArray[i].boundingBox.height = rect.height();
    }
    callback.call(detectedTextArray);
}
 
Example #28
Source File: OcrGraphic.java    From android-vision with Apache License 2.0 4 votes vote down vote up
public TextBlock getTextBlock() {
    return text;
}
 
Example #29
Source File: OcrGraphic.java    From android-vision with Apache License 2.0 4 votes vote down vote up
public TextBlock getTextBlock() {
    return mText;
}
 
Example #30
Source File: OcrGraphic.java    From flutter_mobile_vision with MIT License 4 votes vote down vote up
public void updateItem(TextBlock textBlock) {
    this.textBlock = textBlock;
    postInvalidate();
}