【问题标题】:How to crop image to a set number of bytes?如何将图像裁剪为设定的字节数?
【发布时间】:2019-01-05 16:12:06
【问题描述】:

我正在编写一个应用程序,我在其中拍照并将图片传递给图像识别模型。问题是,识别只需要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()));

如您所见,我限制了捕获图像的大小,但字节数取决于质量,因此我实际上只需要裁剪图像。我怎样才能做到这一点?

【问题讨论】:

  • 你确定是压缩字节需要进入识别吗?此外,它可能需要一定的图像尺寸(宽 x 高),而不仅仅是字节数?
  • IMO,图像识别引擎不太可能关心压缩后的大小。将在未压缩的数据上执行识别。
  • 收到此错误消息:com.google.firebase.ml.common.FirebaseMLException: Input 0 should have 150528 bytes, but found 26573 bytes

标签: java android image-processing


【解决方案1】:

您可以使用以下方法裁剪/调整图像大小

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

【讨论】:

  • 您的Compressor 课程中有什么内容?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 2011-10-12
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多