英文:
Android: How to get a grayscale byte array from ImageCapture on CameraX?
问题
Sure, here's the translated content:
我正在尝试从 Android Java 的 CameraX ImageCapture 使用情况中获取灰度字节数组。到目前为止,我尝试过在 onCaptureSuccess 中转换接收到的字节数组,但没有成功。令人惊讶的是,关于这个过程的文档并不多。Image.getFormat()
返回的格式是 JPEG
。Android 文档只写道它是单平面,“压缩数据,因此行和像素跨距为 0。要解压缩,使用 BitmapFactory#decodeByteArray”。在搜索中,可以通过提取 Y 分量将 JPEG 转换为灰度,就像这篇帖子所建议的那样。但是关于实际如何提取这个分量却没有任何说明。我尝试构造一个新的字节数组,每三个元素取一个,但没有成功。最终,我将字节数组转换为位图:
Bitmap originalBitmap = BitmapFactory.decodeByteArray(originalByteArray, 0, originalByteArray.length);
Bitmap grayScaleBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(grayScaleBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorMatrixColorFilter);
canvas.drawBitmap(originalBitmap, 0, 0, paint);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
grayScaleBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] grayScaleArray = byteArrayOutputStream.toByteArray();
这个方法可以工作,但需要不必要的计算。有没有直接将原始字节数组转换为灰度所需的操作呢?如前所述,它是一个单平面字节数组,看起来像这样:
[-1, -40, -1, -31, -91, 95, 69, 120, 105, 102, 0, 0, 77, 77, 0, 42, 0, ...]
如果对如何工作有帮助的话,欢迎提供帮助。
英文:
I'm trying to get a grayscale byte array from CameraX ImageCapture use case in Android java. What I've tried so far is to convert the byte array received in onCaptureSuccess, with no success. Surprisingly, there is not much documentation about this process. The format returned by Image.getFormat()
is JPEG
. Android documentation only writes that it is a single plane, "Compressed data, so row and pixel strides are 0. To uncompress, use BitmapFactory#decodeByteArray". Searching around, a JPEG can be converted to grayscale by extracting the Y component, as this post suggests. But nothing on how to actually extract this component. I've tried to construct a new byte array, taking one element in three, but with no success. Ultimately, I ended up converting the byte array to bitmap :
Bitmap originalBitmap = BitmapFactory.decodeByteArray(originalByteArray, 0, originalByteArray.length);
Bitmap grayScaleBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(grayScaleBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorMatrixColorFilter);
canvas.drawBitmap(originalBitmap, 0, 0, paint);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
grayScaleBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] grayScaleArray = byteArrayOutputStream.toByteArray();
which works, but requires unnecessary computations. What kind of operations are required to directly convert the original byte array to grayscale ? As previously mentioned, it's a single plane byte array, which looks like this :
[-1, -40, -1, -31, -91, 95, 69, 120, 105, 102, 0, 0, 77, 77, 0, 42, 0, ...]
Any help on understanding how it works would be welcome.
专注分享java语言的经验与见解,让所有开发者获益!
评论