Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save doomSDey/d87c6bfc8788fb1368879f38d8e109c3 to your computer and use it in GitHub Desktop.
Save doomSDey/d87c6bfc8788fb1368879f38d8e109c3 to your computer and use it in GitHub Desktop.
...
//Image Analysis Function
//Set static size according to your device or write a dynamic function for it
ImageAnalysis imageAnalysis =
new ImageAnalysis.Builder()
.setTargetResolution(new Size(720, 1488))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build();
imageAnalysis.setAnalyzer(executor, new ImageAnalysis.Analyzer() {
@SuppressLint("UnsafeExperimentalUsageError")
@Override
public void analyze(@NonNull ImageProxy image) {
//changing normal degrees into Firebase rotation
int rotationDegrees = degreesToFirebaseRotation(image.getImageInfo().getRotationDegrees());
if (image == null || image.getImage() == null) {
return;
}
//Getting a FirebaseVisionImage object using the Image object and rotationDegrees
final Image mediaImage = image.getImage();
FirebaseVisionImage images = FirebaseVisionImage.fromMediaImage(mediaImage, rotationDegrees);
//Getting bitmap from FirebaseVisionImage Object
Bitmap bmp=images.getBitmap();
//Getting the values for cropping
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = bmp.getHeight();
int width = bmp.getWidth();
int left, right, top, bottom, diameter;
diameter = width;
if (height < width) {
diameter = height;
}
int offset = (int) (0.05 * diameter);
diameter -= offset;
left = width / 2 - diameter / 3;
top = height / 2 - diameter / 3;
right = width / 2 + diameter / 3;
bottom = height / 2 + diameter / 3;
xOffset = left;
yOffset = top;
//Creating new cropped bitmap
Bitmap bitmap = Bitmap.createBitmap(bmp, left, top, boxWidth, boxHeight);
//initializing FirebaseVisionTextRecognizer object
FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance()
.getOnDeviceTextRecognizer();
//Passing FirebaseVisionImage Object created from the cropped bitmap
Task<FirebaseVisionText> result = detector.processImage(FirebaseVisionImage.fromBitmap(bitmap))
.addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
@Override
public void onSuccess(FirebaseVisionText firebaseVisionText) {
// Task completed successfully
// ...
textView=findViewById(R.id.text);
//getting decoded text
String text=firebaseVisionText.getText();
//Setting the decoded text in the texttview
textView.setText(text);
//for getting blocks and line elements
for (FirebaseVisionText.TextBlock block: firebaseVisionText.getTextBlocks()) {
String blockText = block.getText();
for (FirebaseVisionText.Line line: block.getLines()) {
String lineText = line.getText();
for (FirebaseVisionText.Element element: line.getElements()) {
String elementText = element.getText();
}
}
}
image.close();
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Task failed with an exception
// ...
Log.e("Error",e.toString());
image.close();
}
});
}
});
Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, imageAnalysis,preview);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment