考慮所有意見截圖我有以下佈局安卓上顯示
- SurfaceHolder
- SurfaceView
- CameraPreview
- 視圖(傻眼鏡)
所有的工具軟件我想要做的就是拍攝這張照片/快照並對其進行調查,似乎是瘋狂的/複雜的
我第一次拍了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);
}