OCR 的文本对齐问题
Text alignment issue with OCR
我创建了一个从图像中扫描文本并将其写入文本视图的应用程序,它成功地处理了单列数据,但是在多列的情况下,它随机读取列并且打印方式与 table查看原图出现。我使用 google 视觉 api 并将文本解析为 textblock(SparseArray<Textblock>)
.
类型的 SparseArray
扫描图片输出截图:
Frame frame=new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> textBlocks = recognizer.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() + ", ";
}
}
}
if (textBlocks.size() == 0) {
etresult.setText("Scan Failed: Found nothing to scan");
} else {
etresult.setText(etresult.getText() + "Blocks: " + "\n");
etresult.setText(etresult.getText() + blocks + "\n");
saveToInternalStorage(bitmap);
}
通过上面的代码后,我可以看到 OCR 应该扫描图像并检测文本而不是像表格及其列那样的文本对齐方式。但是你可以
通过使用边界框识别对齐方式,边界框给出左上角、右上角、左下角、右下角坐标。请仔细阅读 Google 云视觉 api 文档。 https://cloud.google.com/vision/docs/ocr
我创建了一个从图像中扫描文本并将其写入文本视图的应用程序,它成功地处理了单列数据,但是在多列的情况下,它随机读取列并且打印方式与 table查看原图出现。我使用 google 视觉 api 并将文本解析为 textblock(SparseArray<Textblock>)
.
扫描图片输出截图:
Frame frame=new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> textBlocks = recognizer.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() + ", ";
}
}
}
if (textBlocks.size() == 0) {
etresult.setText("Scan Failed: Found nothing to scan");
} else {
etresult.setText(etresult.getText() + "Blocks: " + "\n");
etresult.setText(etresult.getText() + blocks + "\n");
saveToInternalStorage(bitmap);
}
通过上面的代码后,我可以看到 OCR 应该扫描图像并检测文本而不是像表格及其列那样的文本对齐方式。但是你可以 通过使用边界框识别对齐方式,边界框给出左上角、右上角、左下角、右下角坐标。请仔细阅读 Google 云视觉 api 文档。 https://cloud.google.com/vision/docs/ocr