1

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.

ceng
  • 128
  • 3
  • 17
moun
  • 19
  • 1
  • 5

0 Answers0