Android捕获屏幕到ImageReader的Surface

时间:2014-12-20 15:41:52

标签: android

我目前正在学习一个github项目ScreenCapture的代码,它可以捕获屏幕并在surfaceview中显示图像,这是项目https://github.com/Charlesjean/android-ScreenCapture。我尝试使用以下代码替换SurfaceView表面和ImageReader对象的表面:

 mImgReader = ImageReader.newInstance(mWidth, mHeight, ImageFormat.JPEG, 5);
    mSurface = mImgReader.getSurface();// mSurfaceView.getHolder().getSurface();
    mImgReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader reader) {
            Log.i(TAG, "in OnImageAvailable");

        }
    }, mHandler);

并像这样创建VirtualDisplay:

mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
              mWidth, mHeight, mScreenDensity,
              DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
                      DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
              mSurface, new VirtualDisplay.Callback() {
                  @Override
                  public void onResumed() {
                      Log.i(TAG, "onResumed");
                      super.onResumed();
                  }

                  @Override
                  public void onPaused() {
                      Log.i(TAG, "onPaused");
                      super.onPaused();
                  }
              }, mHandler);

但永远不会调用onImageAvailable方法,有没有人对此有任何经验?我无法弄清楚为什么这不起作用。

2 个答案:

答案 0 :(得分:5)

感谢Simon,我通过将图像格式更改为PixelFormat.RGBA_8888解决了这个问题,但是当你做我做的事情时还需要注意其他一些要点,我在这里发布以防万一它会帮助未来。

Image.Plane的数据缓冲区与Bitmap所需的数据缓冲区不完全相同:1。我们用于创建ImageReader的图像格式为PixelFormat.RGBA_8888 ,因此Image.Plane的缓冲区首先放置R(ed)通道,然后放置G(reen)通道,依此类推。为了将此缓冲区转换为位图,我们需要像这样创建位图bitmap = Bitmap.createBitmap(metrics,width, height, Bitmap.Config.ARGB_8888);
,并且位图缓冲区需要将Alpha通道放在第一个位置。
2.我们从Image.Plane获得的缓冲区每行都有一些填充,我个人认为硬件设备使用它来加速缓冲区操作或对齐。因此,为了复制这个缓冲区,我们需要删除这些填充。

要理解这两点,请参阅代码打击:

 public void onImageAvailable(ImageReader reader) {
            Log.i(TAG, "in OnImageAvailable");
            FileOutputStream fos = null;
            Bitmap bitmap = null;
            Image img = null;
            try {
                img = reader.acquireLatestImage();
                if (img != null) {
                    Image.Plane[] planes = img.getPlanes();
                    if (planes[0].getBuffer() == null) {
                        return;
                    }
                    int width = img.getWidth();
                    int height = img.getHeight();
                    int pixelStride = planes[0].getPixelStride();
                    int rowStride = planes[0].getRowStride();
                    int rowPadding = rowStride - pixelStride * width;
                    byte[] newData = new byte[width * height * 4];

                    int offset = 0;
                    bitmap = Bitmap.createBitmap(metrics,width, height, Bitmap.Config.ARGB_8888);
                    ByteBuffer buffer = planes[0].getBuffer();
                    for (int i = 0; i < height; ++i) {
                        for (int j = 0; j < width; ++j) {
                            int pixel = 0;
                            pixel |= (buffer.get(offset) & 0xff) << 16;     // R
                            pixel |= (buffer.get(offset + 1) & 0xff) << 8;  // G
                            pixel |= (buffer.get(offset + 2) & 0xff);       // B
                            pixel |= (buffer.get(offset + 3) & 0xff) << 24; // A
                            bitmap.setPixel(j, i, pixel);
                            offset += pixelStride;
                        }
                        offset += rowPadding;
                    }
                    String name = "/myscreen" + count + ".png";
                    count++;
                    File file = new File(Environment.getExternalStorageDirectory(), name);
                    fos = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    Log.i(TAG, "image saved in" + Environment.getExternalStorageDirectory() + name);
                    img.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != fos) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != bitmap) {
                    bitmap.recycle();
                }
                if (null != img) {
                    img.close();
                }

            }
        }

答案 1 :(得分:-1)

aiohttp.web.run_app