我正在使用MediaProjection录制我的屏幕,如下所示
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);
问题是捕获的图像包含如下图像的黑框.
编辑
上述问题可以通过位图操作来解决.
不过,我现在正在寻找可以应用到解决方案MediaProjection
或SurfaceView
中ImageReader
实现设备记录.
如果您自己无法控制图像,可以通过执行类似的操作来修改它,假设您的位图称为图像.
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();