Skip to content

Instantly share code, notes, and snippets.

@ingenious3

ingenious3/MainActivity.java Secret

Created Dec 9, 2018
Embed
What would you like to do?
on device text-recognition (MLKit-TextRecognition repository)
private void runTextRecognition() {
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(((GlideBitmapDrawable)imageView.getDrawable()).getBitmap());
FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance()
.getOnDeviceTextRecognizer();
detector.processImage(image)
.addOnSuccessListener(
new OnSuccessListener<FirebaseVisionText>() {
@Override
public void onSuccess(FirebaseVisionText texts) {
processTextRecognitionResult(texts);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
}
private void processTextRecognitionResult(FirebaseVisionText texts) {
List<FirebaseVisionText.TextBlock> blocks = texts.getTextBlocks();
if (blocks.size() == 0) {
showToast("No text found");
return;
}
mGraphicOverlay.clear();
imageView.setImageDrawable(getResources().getDrawable(R.drawable.no_image));
for (int i = 0; i < blocks.size(); i++) {
List<FirebaseVisionText.Line> lines = blocks.get(i).getLines();
for (int j = 0; j < lines.size(); j++) {
List<FirebaseVisionText.Element> elements = lines.get(j).getElements();
for (int k = 0; k < elements.size(); k++) {
Graphic textGraphic = new TextGraphic(mGraphicOverlay, elements.get(k));
mGraphicOverlay.add(textGraphic);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.