Java 如何将图像裁剪到设置的字节数?

Java 如何将图像裁剪到设置的字节数?,java,android,image-processing,Java,Android,Image Processing,我正在编写一个应用程序,在这个应用程序中,我拍摄了一张照片,并将照片传递到一个图像识别模型中。问题是,识别只需要150528字节,图像中的字节数取决于图像质量。在Java中是否有一种方法可以根据图像的质量尽可能均匀地裁剪图像的顶部和底部,使其每次都有一个固定的字节数 这是到目前为止我对这部分的代码 int width = 100; int height = 200; final ImageReader reader = ImageReader.newInstance(width,height,I

我正在编写一个应用程序,在这个应用程序中,我拍摄了一张照片,并将照片传递到一个图像识别模型中。问题是,识别只需要150528字节,图像中的字节数取决于图像质量。在Java中是否有一种方法可以根据图像的质量尽可能均匀地裁剪图像的顶部和底部,使其每次都有一个固定的字节数

这是到目前为止我对这部分的代码

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()));
int-width=100;
整数高度=200;
最终ImageReader=ImageReader.newInstance(宽度、高度、ImageFormat.JPEG,1);
List outputSurface=new ArrayList(2);
add(reader.getSurface());
添加(新表面(textureView.getSurfaceTexture());

如您所见,我限制了捕获图像的大小,但字节数取决于质量,因此我需要实际裁剪图像。我该怎么做

您可以使用以下命令裁剪/调整图像大小

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();
}

你确定需要进入识别的是压缩字节吗?此外,它可能期望特定的图像大小(宽度x高度),而不仅仅是“是”的数量?在我看来,图像识别引擎不太可能关心压缩的大小。将对未压缩的数据执行识别。获取此错误消息:
com.google.firebase.ml.common.FirebaseMLException:输入0应具有150528字节,但找到26573字节
压缩程序类中有什么?