Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android mediaprojection screenshot contains black frame

I am working on recording my screen with MediaProjection as follows

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
displayWidth = size.x;
displayHeight = size.y;

imageReader = ImageReader.newInstance(displayWidth, displayHeight, ImageFormat.JPEG, 5);

int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;

DisplayMetrics metrics = getResources().getDisplayMetrics();
int density = metrics.densityDpi;

mediaProjection.createVirtualDisplay("test", displayWidth, displayHeight, density, flags, 
      imageReader.getSurface(), null, projectionHandler);

Image image = imageReader.acquireLatestImage();
byte[] data = getDataFromImage(image);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

Problem is that captured images contains black frame like image below.

enter image description here

EDIT

The above issue can be solved with bitmap operations.

However, I am now looking for a solution that can be applied to MediaProjection or to SurfaceView of ImageReader to implement device recording.

like image 897
AMD Avatar asked Dec 09 '15 05:12

AMD


1 Answers

I had a similar issue. The following code exhibits this problem.

final DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

final int width = metrics.widthPixels;
final int height = metrics.heightPixels;
final int densityDpi = metrics.densityDpi;
final int MAX_IMAGES = 10;

mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, MAX_IMAGES);

mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCaptureTest",
        width, height, densityDpi,
        DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
        mImageReader.getSurface(), null, null);

Replacing this:

getWindowManager().getDefaultDisplay().getMetrics(metrics);

With this:

getWindowManager().getDefaultDisplay().getRealMetrics(metrics);

Fixes it. The problem is that the decorations around the image corrupt the actual resolution of the screen. getMetrics() returns a height (or width in landscape) that is not accurte, and has the home, back, etc, buttons subtracted. The actual display area available for developers is (1440 x 2326... or something like that). But of course, the captured image is going to be the full 1440 X 2560 screen resolution.

like image 69
ChrisCM Avatar answered Oct 04 '22 16:10

ChrisCM