-1

I am coding an app 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?

Ruby
  • 413
  • 1
  • 4
  • 16
  • 1
    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 Jul 29 '18 at 02:10
  • https://stackoverflow.com/questions/15789049/crop-a-bitmap-image/15789801#15789801 – Stephen C Jul 29 '18 at 02:10
  • 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 Jul 29 '18 at 02:14
  • Getting this error message: `com.google.firebase.ml.common.FirebaseMLException: Input 0 should have 150528 bytes, but found 26573 bytes` – Ruby Jul 29 '18 at 02:56

1 Answers1

0

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();
}
olajide
  • 972
  • 1
  • 13
  • 26