AMD
AMD

Reputation: 1722

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.

Upvotes: 11

Views: 4182

Answers (3)

MobA11y
MobA11y

Reputation: 18860

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.

Upvotes: 8

Radu Ionescu
Radu Ionescu

Reputation: 3532

Based on your comments I think this is what you are looking for

    Bitmap bitmap; //field
    Bitmap  croppedBitmap; // field
    Image image;// field
    Handler mHandler; //field


    new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            mHandler = new Handler();
            Looper.loop();
        }
    }.start();

    imageAvailableListener = new ImageReader.OnImageAvailableListener {
    @Override
    public void onImageAvailable(ImageReader reader) {

        try {
            image = reader.acquireLatestImage();
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            Rect rect = image.getCropRect();
            croppedBitmap =  Bitmap.createBitmap(bitmap,rect.left,rect.top,rect.width(),rect.height());

            \\Do whatever here...


            image.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

               if (bitmap!=null) {
                   bitmap.recycle();
               }

              if (croppedBitmap!=null) {
                   bitmap.recycle();
               }

              if (image!=null) {
                   image.close();
               }
           }
        }
    }
    imageReader.setOnImageAvailableListener(imageAvailableListener, mHandler);

Upvotes: 1

Uday Nayak
Uday Nayak

Reputation: 429

If you do not have control over the image yourself, you can modify it by doing something like, assuming your Bitmap is called image.

Bitmap imageWithBG = Bitmap.createBitmap(image.getWidth(), image.getHeight(),image.getConfig());  // Create another image the same size
imageWithBG.eraseColor(Color.BLACK);  // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG);  // create a canvas to draw on the new image
canvas.drawBitmap(image, 0f, 0f, null); // draw old image on the background
image.recycle(); 

Upvotes: 4

Related Questions