使用 MediaProjection 截屏时额外填充 API
extra padding while taking screenshot using MediaProjection API
我正在使用以下代码获取屏幕截图。我得到了屏幕截图,但图像上附有黑色填充。这里使用网上找到的截图和去填充的代码示例:
// requesting permission
MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(
Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(projectionManager.createScreenCaptureIntent(), REQUEST_SCREENSHOT);
// requesting screenshot on successful permission (in onActivityResult)
if (requestCode == REQUEST_SCREENSHOT && resultCode == RESULT_OK) {
MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
final MediaProjection mProjection = projectionManager.getMediaProjection(resultCode, data);
Display mDisplay = getWindowManager().getDefaultDisplay();
Point size = new Point();
mDisplay.getSize(size);
final int width = size.x;
final int height = size.y;
final ImageReader mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);
mProjection.createVirtualDisplay("screen-mirror", width, height,
getResources().getDisplayMetrics().densityDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, mImageReader.getSurface(),
null, null);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
final Image image = imageReader.acquireLatestImage();
if (image != null) {
mImageReader.setOnImageAvailableListener(null, null);
mProjection.stop();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
// create bitmap
Bitmap bmp = Bitmap.createBitmap(
width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(buffer);
image.close();
ImageView iv = findViewById(R.id.iv);
iv.setImageBitmap(bmp);
}
}
}, null);
}
这是显示我得到的内容的屏幕截图。我正在将位图加载到 Activity 中的 ImageView 中。
首先,您得到的尺寸不正确。
你应该改为:
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getRealSize(size);
然后你应该根据你从缓冲区得到的位图创建另一个位图
Bitmap newBitmap = Bitmap.createBitmap(bmp, 0, 0, mWidth, mHeight);
bmp.recycle();
ImageView iv = findViewById(R.id.iv);
iv.setImageBitmap(newBitmap);
我正在使用以下代码获取屏幕截图。我得到了屏幕截图,但图像上附有黑色填充。这里使用网上找到的截图和去填充的代码示例:
// requesting permission
MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(
Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(projectionManager.createScreenCaptureIntent(), REQUEST_SCREENSHOT);
// requesting screenshot on successful permission (in onActivityResult)
if (requestCode == REQUEST_SCREENSHOT && resultCode == RESULT_OK) {
MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
final MediaProjection mProjection = projectionManager.getMediaProjection(resultCode, data);
Display mDisplay = getWindowManager().getDefaultDisplay();
Point size = new Point();
mDisplay.getSize(size);
final int width = size.x;
final int height = size.y;
final ImageReader mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);
mProjection.createVirtualDisplay("screen-mirror", width, height,
getResources().getDisplayMetrics().densityDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, mImageReader.getSurface(),
null, null);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
final Image image = imageReader.acquireLatestImage();
if (image != null) {
mImageReader.setOnImageAvailableListener(null, null);
mProjection.stop();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
// create bitmap
Bitmap bmp = Bitmap.createBitmap(
width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(buffer);
image.close();
ImageView iv = findViewById(R.id.iv);
iv.setImageBitmap(bmp);
}
}
}, null);
}
这是显示我得到的内容的屏幕截图。我正在将位图加载到 Activity 中的 ImageView 中。
首先,您得到的尺寸不正确。 你应该改为:
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getRealSize(size);
然后你应该根据你从缓冲区得到的位图创建另一个位图
Bitmap newBitmap = Bitmap.createBitmap(bmp, 0, 0, mWidth, mHeight);
bmp.recycle();
ImageView iv = findViewById(R.id.iv);
iv.setImageBitmap(newBitmap);