我正在构建一个Android应用,该应用可以扫描打印的文档并检测文本,并将其显示为在文档中显示的。>
有什么办法格式化文本?或者有没有库/ API ?我至少希望句子逐行排列。
您可以在此屏幕截图中清楚地看到可怕的结果:
这是我的代码:
private void recognizeText (Bitmap photoBitmap) {
photo = findViewById(R.id.photo);
photo.setImageBitmap(photoBitmap);
recognizedText = findViewById(R.id.recognized_text);
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
try {
if (!textRecognizer.isOperational()) {
Toast.makeText(getApplicationContext(), "Could not recognize text", Toast.LENGTH_SHORT).show();
} else {
photo = findViewById(R.id.photo);
Frame frame = new Frame.Builder().setBitmap(photoBitmap).build();
SparseArray<TextBlock> words = textRecognizer.detect(frame);
StringBuilder stringBuilder = new StringBuilder();
String word;
for (int i=0; i<words.size(); ++i) {
TextBlock wordBlocks = words.valueAt(i);
if (wordBlocks.toString().contains(".")) {
word = wordBlocks.getValue().replace(".", ".\n");
} else {
word = wordBlocks.getValue();
}
stringBuilder.append(word);
}
recognizedText.setText(stringBuilder.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}