我有一个具有以下布局的应用程序
- 表面支架
- 表面视图
- 相机预览
- 查看(傻眼镜)
我想做的就是拍一张照片/快照并查看它,看起来很疯狂/很复杂
我先拍了 SurfaceView 的快照,得到一个黑色方块,看来这个方法不正确
现在我正在使用 MediaProjection / ImageReader?
更新:所以我确实得到了一张带有波浪线的图像,它崩溃了,因为我试图停止 MediaProjection。如前所述,我只想拍一张照片
private void saveImage()
{
final Handler mHandler = new Handler();
final ImageReader mImageReader = ImageReader.newInstance(cameraSurface.getWidth(), cameraSurface.getHeight(), PixelFormat.RGBA_8888, 2);
mProjection.createVirtualDisplay("screen-mirror", cameraSurface.getWidth(), cameraSurface.getHeight(), mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
FileOutputStream fos = null;
Bitmap bitmap = null;
try {
image = mImageReader.acquireLatestImage();
fos = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/myscreen.jpg");
final Image.Plane[] planes = image.getPlanes();
final Buffer buffer = planes[0].getBuffer().rewind();
bitmap = Bitmap.createBitmap(cameraSurface.getWidth(), cameraSurface.getHeight(), Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
//MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Title" , "Desc");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos!=null) {
try {
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
if (bitmap!=null)
bitmap.recycle();
if (image!=null)
image.close();
if (mProjection!=null) {
mProjection.stop();
mImageReader.close();
}
}
}
}, mHandler);
}