TextRecognizer只能检测TextBlocks

时间:2016-08-04 14:55:06

标签: android google-vision android-vision

我开始按照Text API教程来检测TextBlocks,它工作得很好。但我现在想要检测文本行,并遇到了问题。

UserDatabase db = new UserDatabase(getApplicationContext());
db.putDataInUserTable(*all the parameters);

textRecognizer.setProcessor只能使用TextBlock。 有没有办法检测线?

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

使用这个:

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

答案 2 :(得分:0)

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

“您可以通过调用LinesTextBlock获取getComponents,然后您可以遍历每一行以获取其中文本的位置和值。你把文字放在它实际出现的地方。“

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

答案 3 :(得分: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);