TextRecognizer 只能检测 TextBlocks

TextRecognizer can only detect TextBlocks

我开始是按照 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。 有什么方法可以检测线吗?

本教程 (https://codelabs.developers.google.com/codelabs/mobile-vision-ocr/#6) 说 "The engine puts all the text it recognizes in a TextBlock into one complete sentence, even if it sees the sentence broken over multiple lines."

"You can get the Lines from a TextBlock by calling getComponents, and then you can iterate over each line to get the location and values of the text within it. This lets you put the text in the place it actually appears."

// 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);
}

根据 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);

使用这个:

List<Line> lines = (List<Line>) text.getComponents();
for(Line elements : lines) {
  Log.i("current lines ", ": " + elements.getValue());
}

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

   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() + ", ";
                        }
                    }