public class TextGraphic extends Graphic { private static final String TAG = "TextGraphic"; private static final int TEXT_COLOR = Color.RED; private static final float TEXT_SIZE = 54.0f; private static final float STROKE_WIDTH = 4.0f; private final Paint rectPaint; private final Paint textPaint; private final FirebaseVisionText.Element element; TextGraphic(GraphicOverlay overlay, FirebaseVisionText.Element element) { super(overlay); this.element = element; rectPaint = new Paint(); rectPaint.setColor(TEXT_COLOR); rectPaint.setStyle(Paint.Style.STROKE); rectPaint.setStrokeWidth(STROKE_WIDTH); textPaint = new Paint(); textPaint.setColor(TEXT_COLOR); textPaint.setTextSize(TEXT_SIZE); // Redraw the overlay, as this graphic has been added. postInvalidate(); } /** * Draws the text block annotations for position, size, and raw value on the supplied canvas. */ @Override public void draw(Canvas canvas) { Log.d(TAG, "on draw text graphic"); if (element == null) { throw new IllegalStateException("Attempting to draw a null text."); } // Draws the bounding box around the TextBlock. RectF rect = new RectF(element.getBoundingBox()); canvas.drawRect(rect, rectPaint); // Renders the text at the bottom of the box. canvas.drawText(element.getText(), rect.left, rect.bottom, textPaint); } }