Skip to content Skip to sidebar Skip to footer

Mediaprojection Producing Distorted Screenshot

I managed to get screenshots but the result is like this: Original: This is my code which i took from a few sources: final ImageReader ir = ImageReader.newInstance(width, height,

Solution 1:

It's the width and height of your bitmap that's off--atleast your distorted image looks just like what I was seeing. Here's how I got it working.

int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * imageReader.getWidth();
bitmap = Bitmap.createBitmap(imageReader.getWidth() + rowPadding / pixelStride,
                        imageReader.getHeight(), Bitmap.Config.ARGB_8888);

And it's important to use the imageReaders width/height (not the images).

When creating the imageReader/virtualDisplay objects I use the following method to get the display metrics.

publicstatic DisplayMetrics getScreenMetrics(Context context) {
        WindowManagerwm= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        Displaydisplay= wm.getDefaultDisplay();
        DisplayMetricsdm=newDisplayMetrics();
        display.getMetrics(dm);

        return dm;
    }

...
ImageReaderimageReader= ImageReader.newInstance(displayMetrics.widthPixels, displayMetrics.heightPixels, PixelFormat.RGBA_8888, 2);

I've learned this from trial and error, so I apologize for not being able to give an explanation why.

Solution 2:

I am using Xamarin(.NET) and was able to FINALLY get this working, except for one problem (java code):

finalImageReaderir= ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);

PixelFormat.RGBA_8888 is not a valid parameter as the ImageReader requires an ImageFormatType. The following .NET code solves this:

_mImageReader = ImageReader.NewInstance(_mWidth, _mHeight, (ImageFormatType)Format.Rgba8888, 2);

Notice the casting of the follwing, with Format being of the Android.Graphics.Format enum:

(ImageFormatType)Format.Rgba8888

I hope this helps someone out there using Xamarin as this caused a ton of frustration over the last day!

Post a Comment for "Mediaprojection Producing Distorted Screenshot"