识别图像中的文本(ML套件)

时间:2019-08-27 14:36:18

标签: android firebase firebase-mlkit

我尝试创建可识别图像中文字的应用程序。我正在使用ML Kit。(取自github示例)几乎可以正常工作。看图片。为什么单词周围的框太大而位于底部(需要在文本和框wrap_content的顶部)。我该如何解决?抱歉,我的英语))

我在RelativeLayout中的布局

public class TextGraphic extends GraphicOverlay.Graphic {

private static final int TEXT_COLOR = Color.RED;
private static final int TEXT_COLOR_INPUT = Color.GREEN;
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;

public TextGraphic(GraphicOverlay overlay, FirebaseVisionText.Element element, String inputText) {
    super(overlay);

    this.element = element;

    rectPaint = new Paint();
    textPaint = new Paint();
    if (inputText.equals(element.getText().toLowerCase())) {
        rectPaint.setColor(TEXT_COLOR_INPUT);
        textPaint.setColor(TEXT_COLOR_INPUT);
    } else {
        rectPaint.setColor(TEXT_COLOR);
        textPaint.setColor(TEXT_COLOR);
    }
    rectPaint.setStyle(Paint.Style.STROKE);
    rectPaint.setStrokeWidth(STROKE_WIDTH);

    textPaint.setTextSize(TEXT_SIZE);
    postInvalidate();
}

@Override
public void draw(Canvas canvas) {
    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);
}

}

GraphicOverlay类取自github enter link description here

enter image description here

0 个答案:

没有答案