Android使用MediaProjectionManager截屏,OnImageAvailable无法启动

时间:2018-07-13 08:34:19

标签: java android mediaprojectionmanager

我正在开发一个android项目,其中在其他应用程序的顶部也有一个叠加按钮,当单击此按钮时,应该截图。覆盖按钮有效,但屏幕截图部分无效。我不知道为什么,但是似乎OnImageAvailable没有启动。这是一个单独的项目,我仅在其中测试屏幕截图部分:

public class MainActivity extends Activity {

private static final int REQUEST_CODE= 100;

private MediaProjectionManager mProjectionManager;
private MediaProjection mProjection;
private ImageReader mImageReader;
private Handler mHandler = new Handler(Looper.getMainLooper());
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // call for the projection manager
    mProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

    imageView = (ImageView) findViewById(R.id.imageView);

    // start projection
    final Button startButton = (Button)findViewById(R.id.button);
    startButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startProjection();
        }
    });

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

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {

        mProjection = mProjectionManager.getMediaProjection(resultCode, data);

        if (mProjection != null) {
            final DisplayMetrics metrics = getResources().getDisplayMetrics();
            final int density = metrics.densityDpi;
            final int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
            final Display display = getWindowManager().getDefaultDisplay();
            final Point size = new Point();
            display.getSize(size);
            final int width = size.x;
            final int height = size.y;

            mImageReader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 2);
            mProjection.createVirtualDisplay("screencap", width, height, density, flags, mImageReader.getSurface(), null, mHandler);
            mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {

                @Override
                public void onImageAvailable(ImageReader reader) {
                    Image image = null;
                    Bitmap bitmap = null;

                    try {
                        image = mImageReader.acquireLatestImage();
                        if (image != null) {
                            final Image.Plane[] planes = image.getPlanes();

                            int width = image.getWidth();
                            int height = image.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;
                            }

                            imageView.setImageBitmap(bitmap);
                        }

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

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

                        if (image!=null)
                            image.close();

                    }
                }

            }, mHandler);
        }
    }

    super.onActivityResult(requestCode, resultCode, data);
}

private void startProjection() {
    startActivityForResult(mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
}

}

1 个答案:

答案 0 :(得分:0)

我认为您应该设置其他标志。 您是在告诉MediaProjection仅显示其自身内容,但没有在其表面上绘制任何内容。 要制作屏幕截图,您必须使用“ DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR”标志来复制不同的显示。