我使用Text Recognizer从图像中提取文本。我用camera2来捕捉图像。
TextRecognizer textRecognizer = TextRecognizerController.getControllerInstance().getTextRecognizerInstance(getContext(), getView());
if (textRecognizer != null && textRecognizer.isOperational()) {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> detectedItems = textRecognizer.detect(frame);
if (detectedItems != null && detectedItems.size() != 0) {
getPresenter().extractText(detectedItems);
} else {
displaySnackbar(getContext().getResources().getString(R.string.Consult_MagicSearch_TextNotRecognizedErrorMessage));
}
}
public void extractText(SparseArray<TextBlock> detectedItems) {
TextBlock item = detectedItems.valueAt(0);
String detectedText = item.getValue();
contract.setTextRecognized(detectedText);
}