官方原话: Manages the retrieval of certain types of {@link MediaProjection} tokens. 这个类通过
Context#getSystemService
中MEDIA_PROJECTION_SERVICE
获取,他的功能就是获取MediaProjection
官方原话:A token granting applications the ability to capture screen contents and/or record system audio. The exact capabilities granted depend on the type of MediaProjection.在这个类中我们能获取到屏幕的内容
官方原话:The ImageReader class allows direct application access to image data rendered into a {@link android.view.Surface} 通过这个类我们可以把
Surface
转换成图片
@TargetApi(Build.VERSION_CODES.LOLLIPOP) public void requestCapturePermission() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //5.0 之后才允许使用屏幕截图 return; } MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE); startActivityForResult( mediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION); }
这里必须使用
startActivityForResult
因为在createScreenCaptureIntent()
方法中会返回用户授权截取屏幕的结果,用户根据下面弹窗允许或者拒绝
用户选择后在Activity 的
onActivityResult
中操作返回的结果data@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_MEDIA_PROJECTION: if (resultCode == RESULT_OK && data != null) { FloatWindowsService.setResultData(data); startService(new Intent(getApplicationContext(), FloatWindowsService.class)); } break; } }
private void createImageReader() { mImageReader = ImageReader.newInstance(mScreenWidth, mScreenHeight, PixelFormat.RGBA_8888, 2); }
private void startScreenShot() { mFloatView.setVisibility(View.GONE); Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { //获取当前屏幕内容 startVirtual(); } }, 5); handler.postDelayed(new Runnable() { public void run() { //生成图片保存到本地 startCapture(); } }, 30); }
public void startVirtual() { if (mMediaProjection != null) { virtualDisplay(); } else { setUpMediaProjection(); virtualDisplay(); } }
public void setUpMediaProjection() { if (mResultData == null) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); startActivity(intent); } else {
//mResultData是在Activity中用户授权后返回的结果
mMediaProjection = getMediaProjectionManager().getMediaProjection(Activity.RESULT_OK, mResultData); } }
private void virtualDisplay() {
mVirtualDisplay = mMediaProjection.createVirtualDisplay("screen-mirror",
mScreenWidth, mScreenHeight, mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mImageReader.getSurface(), null, null);
}
@Override protected Bitmap doInBackground(Image... params) { if (params == null || params.length < 1 || params[0] == null) { return null; } Image image = params[0]; 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 = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888); bitmap.copyPixelsFromBuffer(buffer); bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height); image.close();
###源码
###APK