赞
踩
最近做一个截屏的功能就直接在网上查找的截屏代码,发现截出来的图不对,APP里是有视频画面的,截出来的图片视频窗口是黑色的。网上大多都是通过 getWindow().getDecorView();来截图但是这种对视频流并不行。后来我用MediaProjectionManager.createScreenCaptureIntent()获取Intent,然后再通过startActivityForResult传入Intent然后在onActivityResult中通过MediaProjectionManager.getMediaProjection(resultCode,data)获取MediaProjection。创建ImageReader,构建VirtualDisplay,最后就是通过ImageReader截图,就可以从ImageReader里获得Image对象转换成bitmap再进行存储。
public class MainActivity extends AppCompatActivity {
private static final int SCREEN_SHOT = 0;
private static final String TAG = "TAG";
MediaProjection mediaProjection;
MediaProjectionManager projectionManager;
VirtualDisplay virtualDisplay;
int mResultCode;
Intent mData;
ImageReader imageReader;
int width;
int height;
int dpi;
Bitmap bitmap;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
width = metric.widthPixels;
height = metric.heightPixels;
dpi = metric.densityDpi;
imageView = (ImageView) findViewById(R.id.image);
projectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == SCREEN_SHOT){
if(resultCode == RESULT_OK){
mResultCode = resultCode;
mData = data;
setUpMediaProjection();
setUpVirtualDisplay();
startCapture();
}
}
}
private void startCapture() {
SystemClock.sleep(1000);
imageName = System.currentTimeMillis() + ".png";
Image image = imageReader.acquireNextImage();
if (image == null) {
Log.e(TAG, "null.");
return;
}
int width = image.getWidth();
int height = image.getHeight();
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;
bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
image.close();
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "DJI" + "/";
File folder = new File(filePath);
if (folder == null || !folder.exists()) {
folder.mkdir();
}
File file = new File(filePath, simpleDateFormat.format(date) + ".png");
FileOutputStream os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
//upload(file);
os.flush();
os.close();
showToast("截图成功!");
}
}
private void setUpVirtualDisplay() {
imageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 1);
mediaProjection.createVirtualDisplay("ScreenShout",
width,height,dpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
imageReader.getSurface(),null,null);
}
private void setUpMediaProjection(){
mediaProjection = projectionManager.getMediaProjection(mResultCode,mData);
}
public void StartScreenShot() {
startActivityForResult(projectionManager.createScreenCaptureIntent(),
SCREEN_SHOT);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。