2

I am trying to take screenshots of my Android device (Samsung Galaxy Tab S5e) using a service. I am using the code from here.

In the code linked, ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2); is used. Note the PixelFormat.RGBA_8888. Now, this won't allow me to compile my project and I am greeted with the error: Error: Must be one of: ImageFormat.UNKNOWN, ImageFormat.RGB_565... etc.

So I tried changing PixelFormat.RGBA_8888 to ImageFormat.JPEG and it compiles. However, my app now crashes with the message:

RGBA override BLOB format buffer should have height == width

I've tried changing PixelFormat.RGBA_8888 to 0x4, 0x1, ImageFormat.RGB_565 and a few others. Doing so often results in an exception with the message:

The producer output buffer format 0x1 doesn't match the ImageReader's configured buffer format 0x4.

This is somehow linked with the format described in onImageAvailable(ImageReader reader).

I've seen the following SO post, and it seems the correct format is device-specific, but I've tried them all and the error is one of the above.

I'm at a total loss (and I'm a Java/Android newb), so could really use some help.

pookie
  • 3,796
  • 6
  • 49
  • 105

1 Answers1

2

Create image reader as follows,

 ImageReader imageReader =ImageReader.newInstance(width,
            height,
            PixelFormat.RGBA_8888,
            MAX_IMAGE_COUNT);

Then on the image available listener create bitmap as,

          try {
                image = reader.acquireLatestImage();
                if (image != null && mImagesProduced == 0){
                    Image.Plane[] planes = image.getPlanes();
                    Buffer imageBuffer = planes[0].getBuffer().rewind();

                    int pixelStride = planes[0].getPixelStride();
                    int rowStride = planes[0].getRowStride();
                    int rowPadding = rowStride - pixelStride * width;

                    // create bitmap
                    bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height,
                            Bitmap.Config.ARGB_8888);
                    bitmap.copyPixelsFromBuffer(imageBuffer);

                    saveImage(context, bitmap);
                }

Hope it works

Abhijith Brumal
  • 1,652
  • 10
  • 14