package hu.autsoft.androidapidemos.ocr; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import com.google.android.gms.vision.text.Text; import com.google.android.gms.vision.text.TextBlock; import java.util.List; public class OcrGraphic extends GraphicOverlay.Graphic { private int mId; private static final int TEXT_COLOR = Color.WHITE; private static Paint sRectPaint; private static Paint sTextPaint; private final TextBlock mText; OcrGraphic(GraphicOverlay overlay, TextBlock text) { super(overlay); mText = text; if (sRectPaint == null) { sRectPaint = new Paint(); sRectPaint.setColor(TEXT_COLOR); sRectPaint.setStyle(Paint.Style.STROKE); sRectPaint.setStrokeWidth(4.0f); } if (sTextPaint == null) { sTextPaint = new Paint(); sTextPaint.setColor(TEXT_COLOR); sTextPaint.setTextSize(54.0f); } postInvalidate(); } public int getId() { return mId; } public void setId(int id) { this.mId = id; } public TextBlock getTextBlock() { return mText; } 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 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); } } }