How to crop image to a set number of bytes?


How to crop image to a set number of bytes?
I am coding an app in Android Studio where I take a picture and I pass the picture into an image recognition model. The problem is, the recognition only takes 150528 bytes, and the number of bytes in the image depends on the image quality. Is there a way in Java to crop the top and bottom of the image as evenly as possible depending on the quality to make it a set number of bytes each time?
This is my code for this part so far.
int width = 100;
int height = 200;
final ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG,1);
List<Surface> outputSurface = new ArrayList<>(2);
outputSurface.add(reader.getSurface());
outputSurface.add(new Surface(textureView.getSurfaceTexture()));
As you can see, I'm restricting the size of the image captured, but the number of bytes depends on the quality so I need to actually just crop the image. How can I do this?
stackoverflow.com/questions/15789049/crop-a-bitmap-image/…
– Stephen C
1 hour ago
IMO, it is highly unlikely that image recognition engine cares about the compressed size. The recognition will be performed on the uncompressed data.
– Stephen C
1 hour ago
Getting this error message:
com.google.firebase.ml.common.FirebaseMLException: Input 0 should have 150528 bytes, but found 26573 bytes
– Ruby
26 mins ago
com.google.firebase.ml.common.FirebaseMLException: Input 0 should have 150528 bytes, but found 26573 bytes
1 Answer
1
You can crop/ resize image with the following
byte thumb_byte_data;
Uri resultUri = ImageUri;
//getting imageUri and store in file. and compress to bitmap
File file_path = new File(resultUri.getPath());
try
Bitmap thumb_bitmap = new Compressor(this)
.setMaxHeight(200)
.setMaxWidth(200)
.setQuality(75)
.compressToBitmap(file_path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumb_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
thumb_byte_data = baos.toByteArray();
catch (IOException e)
e.printStackTrace();
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you sure it’s compressed bytes that need to go into the recognition? Also, it probably expects a certain image size (width x height) not just number of byes?
– Cris Luengo
1 hour ago