public static int getWindowHeight(Context context) { return context.getResources().getDisplayMetrics().heightPixels; } public static int getWindowWidth(Context context) { return context.getResources().getDisplayMetrics().widthPixels; } VirtualDisplay mVirtualDisplay; private void parseData(Intent data) { MediaProjection mMediaProjection = mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK, data); @SuppressLint("WrongConstant") ImageReader mImageReader = ImageReader.newInstance( getWindowWidth(this), getWindowHeight(this), PixelFormat.RGBA_8888, 1); mVirtualDisplay = mMediaProjection.createVirtualDisplay("screen-mirror", getWindowWidth(this), getWindowHeight(this), Resources.getSystem().getDisplayMetrics().densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Image image = mImageReader.acquireLatestImage(); int width = getWindowWidth(AiClassActivity.this); int height = getWindowHeight(AiClassActivity.this); 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(); saveBitmap(bitmap); if (mVirtualDisplay == null) { return; } mVirtualDisplay.release(); mVirtualDisplay = null; if (currenttransition != null) { doSnapshot(currenttransition); } } }, 300); }
Android截屏
最新推荐文章于 2024-03-27 14:11:38 发布