【发布时间】:2016-01-29 19:00:22
【问题描述】:
我设法得到了截图,但结果是这样的:
原文:
这是我从几个来源获取的代码:
final ImageReader ir = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);
VirtualDisplay vd= mediaProjection.createVirtualDisplay("screen", width, height, dpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC, ir.getSurface(), null, null);
final String path=getActivity().getFilesDir().getAbsolutePath()+"/myscreen.jpg";
ir.setOnImageAvailableListener(new OnImageAvailableListener() {
// @Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
FileOutputStream fos = null;
Bitmap bitmap = null;
try {
image = ir.acquireLatestImage();
fos = new FileOutputStream( path);
final Image.Plane[] planes = image.getPlanes();
final Buffer buffer = planes[0].getBuffer().rewind();
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
bitmap.compress(CompressFormat.JPEG, 85, fos);
image.close();
} catch (Exception e) {
e.printStackTrace();
........
........
【问题讨论】:
-
即兴发挥,您的
width计算可能已关闭,因为它似乎没有考虑像素步长和行步长。请参阅this chunk of code 了解我是如何做到的。 -
为什么是
width>>1?顺便说一句,把bitmapWidth=width + rowPadding / pixelStride工作,但现在我在右边有一个 300 像素宽的黑条, -
"为什么是宽度>>1?" ——我没看到,抱歉。 “现在我在右侧有一个 300 像素宽的黑条”——这是因为您要求的尺寸与实际图像的尺寸之间存在差异。如果您继续查看我的代码清单,越过突出显示的部分,您将看到我在哪里将原始位图裁剪为所需的大小,然后使用裁剪的部分。
标签: android android-screen android-mediaprojection