android - 我们可以设置或编辑 com.google.android.gms.vision.text.Text.TextBlock 的内容吗?

标签 android textblock

根据 OCR 的 Google 文档 TextBlock它包含这些方法。

  • getBoundingBox()

  • getComponents()

  • getCornerPoints()

  • getLanguage()

  • getValue()

有什么方法可以为 TextBlock 设置 setValue() 吗?

提前致谢。

最佳答案

这取决于您究竟需要什么。如果您正在考虑更改由视觉 API 识别的显示文本,那么这是可能的。

在我使用来自 Vision API 的 OCR 示例的情况下,您只需要在 OcrGraphic 类中创建另一个构造函数。

private TextBlock mText;
private String cText;

OcrGraphic(GraphicOverlay overlay, TextBlock text, String caption){
    super(overlay);
    mText = text;
    cText = caption;
    .
    .
    .

    // Redraw the overlay, as this graphic has been added.
    postInvalidate();
}

然后修改之前的构造函数

OcrGraphic(GraphicOverlay overlay, TextBlock text) {
    super(overlay);

    mText = text;
    cText = text.getValue();
    .
    .
    .

    // Redraw the overlay, as this graphic has been added.
    postInvalidate();
}

在绘制方法中,您需要从变量中绘制文本

public void draw(Canvas canvas) {
    TextBlock text = mText;
    if (text == null) {
        return;
    }
    .
    .
    .
    // we replaced text.getValue() with cText that string are set by either constructors
    canvas.drawText(cText, rect.left, rect.bottom, sTextPaint);

}

现在你可以通过这两种方式调用,它会工作,只要在探测器处理器中需要时使用如下:(在 OcrDetectorProcessor 类中的 receiveDetections 方法中)

OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
// Or use new one for your custom text
OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item, textValue);

mGraphicOverlay.add(graphic);

请让我知道您的情况,正是您想要做什么。接受的答案也是正确的,但这是视觉 API 之外的一个技巧。

关于android - 我们可以设置或编辑 com.google.android.gms.vision.text.Text.TextBlock 的内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39510200/

相关文章:

Android:切换加速度计旋转状态设置

android - Android 的重用技巧

WPF 多行 TextBlock 中心对齐问题

wpf - 将图像添加到工具提示中

silverlight - 如何在 Silverlight 中使 TextBlock 的文本加粗?

android - OpenCV,通过单应矩阵扭曲图像

安卓应用内订阅 : Inventory call returns purchase as null even if purchase exists

c# - 处理WPF中的viewbox点击事件

c# - TextBlock 重写样式的奇怪行为

java - 获取格式错误的访问 token "t ​ype":"OAuthException","code":190