/** * Draws the text block annotations for position, size, and raw value on the supplied canvas. */ @Override public void draw(Canvas canvas) { if (textBlock == null) { return; } // Draws the bounding box around the TextBlock. RectF rect = new RectF(textBlock.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 = textBlock.getComponents(); for(Text currentText : textComponents) { float left = translateX(currentText.getBoundingBox().left); float bottom = translateY(currentText.getBoundingBox().bottom); canvas.drawText(currentText.getValue(), left, bottom, textPaint); } } }
/** * 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) { if (textBlock == null) { return false; } RectF rect = new RectF(textBlock.getBoundingBox()); rect = translateRect(rect); return rect.contains(x, y); }
/** * 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; }
TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build(); textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay)); if (!textRecognizer.isOperational()) {
private void inspectFromBitmap(Bitmap bitmap) { TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build(); try { if (!textRecognizer.isOperational()) { new AlertDialog. Builder(this). SparseArray<TextBlock> origTextBlocks = textRecognizer.detect(frame); List<TextBlock> textBlocks = new ArrayList<>(); for (int i = 0; i < origTextBlocks.size(); i++) { if (textBlock != null && textBlock.getValue() != null) { detectedText.append(textBlock.getValue()); detectedText.append("\n"); textRecognizer.release();
TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build(); textRecognizer.setProcessor(new OcrDetectorProcessor(graphicOverlay)); if (!textRecognizer.isOperational()) {
/** * Draws the text block annotations for position, size, and raw value on the supplied canvas. */ @Override public void draw(Canvas canvas) { TextBlock text = mText; if (text == null) { return; } // Draws the bounding box around the TextBlock. 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); canvas.drawRect(rect, sRectPaint); // 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); canvas.drawText(currentText.getValue(), left, bottom, sTextPaint); } } }
/** * 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); }
/** * 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); } } }
@SuppressLint("InlinedApi") private void createCameraSource(boolean autoFocus, boolean useFlash) { Context context = getApplicationContext(); TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build(); textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay, tvOcrResult)); if (textRecognizer.isOperational()) { mCameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer) .setFacing(CameraSource.CAMERA_FACING_BACK) .setRequestedPreviewSize(1280, 1024) .setRequestedFps(2.0f) .setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null) .setFocusMode(autoFocus ? Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE : null) .build(); } }
@Override public void draw(Canvas canvas) { if (mText == null) { return; } RectF rect = new RectF(mText.getBoundingBox()); rect.left = translateX(rect.left); rect.top = translateY(rect.top); rect.right = translateX(rect.right); rect.bottom = translateY(rect.bottom); canvas.drawRect(rect, sRectPaint); List<? extends Text> textComponents = mText.getComponents(); for(Text currentText : textComponents) { float left = translateX(currentText.getBoundingBox().left); float bottom = translateY(currentText.getBoundingBox().bottom); canvas.drawText(currentText.getValue(), left, bottom, sTextPaint); } } }
@Override public int compare(TextBlock o1, TextBlock o2) { int diffOfTops = o1.getBoundingBox().top - o2.getBoundingBox().top; int diffOfLefts = o1.getBoundingBox().left - o2.getBoundingBox().left; if (diffOfTops != 0) { return diffOfTops; } return diffOfLefts; } });
/** * 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; }
TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build(); textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay)); if (!textRecognizer.isOperational()) {
/** * Draws the text block annotations for position, size, and raw value on the supplied canvas. */ @Override public void draw(Canvas canvas) { TextBlock text = mText; if (text == null) { return; } // Draws the bounding box around the TextBlock. 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); canvas.drawRect(rect, sRectPaint); // 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); canvas.drawText(currentText.getValue(), left, bottom, sTextPaint); } } }
public boolean contains(float x, float y) { if (mText == null) { return false; } RectF rect = new RectF(mText.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); }
@Override public void receiveDetections(Detector.Detections<TextBlock> detections) { ocrGraphicOverlay.clear(); SparseArray<TextBlock> items = detections.getDetectedItems(); final StringBuilder sb = new StringBuilder(); 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()); sb.append(item.getValue()+"\n"); } OcrGraphic graphic = new OcrGraphic(ocrGraphicOverlay, item); ocrGraphicOverlay.add(graphic); } tvOcrResult.post(new Runnable() { @Override public void run() { tvOcrResult.setText(sb.toString()); } }); }
barcodeValue = (TextView) findViewById(R.id.text_value); TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build(); textRecognizer.setProcessor(new Detector.Processor<TextBlock>() { @Override public void release() { if (!textRecognizer.isOperational()) { Log.w("MainActivity", "Detector dependencies are not yet available.");
/** * 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); }
/** * 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; }