1

我开始按照 Text API 教程来检测 TextBlocks,效果很好。但是我现在想检测文本行,遇到了一个问题。

// TODO: Create the TextRecognizer
TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build();

// TODO: Set the TextRecognizer's Processor.
textRecognizer.setProcessor(new OcrDetectorProcessor(mGraphicOverlay));

textRecognizer.setProcessor 只能使用 TextBlock。有什么方法可以检测线条吗?

4

4 回答 4

3

点击这里,阅读完整代码。希望对您有所帮助。

   Bitmap bitmap = decodeBitmapUri(this, imageUri);
            if (detector.isOperational() && bitmap != null) {
                Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                SparseArray<TextBlock> textBlocks = detector.detect(frame);
                String blocks = "";
                String lines = "";
                String words = "";
                for (int index = 0; index < textBlocks.size(); index++) {
                    //extract scanned text blocks here
                    TextBlock tBlock = textBlocks.valueAt(index);
                    blocks = blocks + tBlock.getValue() + "\n" + "\n";
                    for (Text line : tBlock.getComponents()) {
                        //extract scanned text lines here
                        lines = lines + line.getValue() + "\n";
                        for (Text element : line.getComponents()) {
                            //extract scanned text words here
                            words = words + element.getValue() + ", ";
                        }
                    }
于 2017-09-21T19:48:26.537 回答
1

使用这个:

List<Line> lines = (List<Line>) text.getComponents();
for(Line elements : lines) {
  Log.i("current lines ", ": " + elements.getValue());
}
于 2016-09-02T10:50:27.217 回答
0

本教程 ( https://codelabs.developers.google.com/codelabs/mobile-vision-ocr/#6 ) 说“引擎将它识别的所有文本TextBlock放入一个完整的句子中,即使它看到句子多行中断。”

“您可以通过调用Lines从 a中获取,然后您可以遍历每一行以获取其中文本的位置和值。这使您可以将文本放在它实际出现的位置。”TextBlockgetComponents

// Break the text into multiple lines and draw each one according to its own bounding box.
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);
}
于 2016-08-06T14:48:52.220 回答
0

根据 Pedro Madeira 的回答,我想出的解决方案是:

    List<? extends Text> textComponents = mText.getComponents();
    for (Text currentText : textComponents) {
        RectF rect = new RectF(currentText.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);
于 2016-08-06T21:47:49.280 回答