Bitmap from ImageReader always blank when trying to capture screen

我是研究僧i 提交于 2019-11-28 10:26:28

问题


Using the MediaProjection API for screen capture, I create an ImageReader and feed use it as a point of access for captured screens as below:

mImageReader = ImageReader.newInstance(mWidth, mHeight, ImageFormat.JPEG, 2);

and

mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader reader) {
                    Image image = null;
                    Bitmap bitmap = null;
                    image = mImageReader.acquireLatestImage();
                    Image.Plane[] planes = image.getPlanes();
                    Buffer buffer = planes[0].getBuffer().rewind();
                    bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
                    bitmap.copyPixelsFromBuffer(buffer);
                }
            });

But the resulting image is always a blank transparent image and all pixels in the resulting bitmap are set to #00000000 :(

I've been stuck on this for a while and could really use some help so any advice is welcome. (Also I've already tried these posts but its all the same result - this, this and this)

Edit: I pass the image reader surface in this line:

DisplayMetrics metrics = getResources().getDisplayMetrics();
int density = metrics.densityDpi;
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
mWidth = size.x;
mHeight = size.y;

mMediaProjection.createVirtualDisplay("screencap", mWidth, mHeight, density, flags, mImageReader.getSurface(), null, mHandler);

来源:https://stackoverflow.com/questions/31687259/bitmap-from-imagereader-always-blank-when-trying-to-capture-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!