Android 截屏与录屏

原创
2022/04/17 11:55
阅读数 1.7K

1、全屏截屏

    public static final int EVENT_SCREENSHOT = 22;//截图事件
    private MediaProjectionManager mediaProjectionManager;
    private MediaProjection mediaProjection;
    private Image image;
    public void takeScreenShot() {
        mediaProjectionManager = (MediaProjectionManager)
                getApplication().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), EVENT_SCREENSHOT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("whh0914", "onActivityResult...requestCode=" + requestCode + ",resultCode=" + resultCode);
        if (requestCode == EVENT_SCREENSHOT) {
            super.onActivityResult(requestCode, resultCode, data);
            Log.e("whh0914", "captureScreen...");
            DisplayMetrics displayMetrics = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
            int width = displayMetrics.widthPixels;
            int height = displayMetrics.heightPixels;
            Log.e("whh0914", "displayMetrics width=" + width + ", height=" + height);
            ImageReader mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2);
            mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
            VirtualDisplay virtualDisplay = mediaProjection.createVirtualDisplay("screen-mirror", width, height,
                    displayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        image = mImageReader.acquireLatestImage();
                        if (image != null) {
                            final Image.Plane[] planes = image.getPlanes();
                            final ByteBuffer buffer = planes[0].getBuffer();
                            int width = image.getWidth();
                            int height = image.getHeight();
                            Log.e("whh0914", "image width=" + width + ", height=" + height);
                            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.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), false);
                            if (bitmap != null) {
                                Log.e("whh0914", "屏幕截图成功!");
                                saveBitmap(bitmap, "/sdcard/screenShot.png");
                            }
                            bitmap.recycle();
                        }
                    } catch (Exception e) {
                        Log.e("whh0914", "截图出现异常:" + e.toString());
                    } finally {
                        if (image != null) {
                            image.close();
                        }
                        if (mImageReader != null) {
                            mImageReader.close();
                        }
                        if (virtualDisplay != null) {
                            virtualDisplay.release();
                        }
                        //必须代码,否则出现BufferQueueProducer: [ImageReader] dequeueBuffer: BufferQueue has been abandoned
                        mImageReader.setOnImageAvailableListener(null, null); 
                        mediaProjection.stop();
                    }
                }
            }, 100);
        }
    }

 

2、针对特定的Surface截屏

PixelCopy工具类

 PixelCopy.request(window, bitmap, new PixelCopy.OnPixelCopyFinishedListener() {
        @Override
        public void onPixelCopyFinished(int copyResult){
            if (PixelCopy.SUCCESS == copyResult) {
                // onSuccessCallback(bitmap)
            } else {
                // onErrorCallback()
            }
        }
    }, new Handler());

3、SurfaceControl截屏

应用内截屏.需要权限android.permission.READ_FRAME_BUFFER

当然,每个版本的SurfaceControl不一样

Display display = ((WindowManager)captureView.getContext()
     .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getRealMetrics(displayMetrics);

Bitmap bitmap = SurfaceControl.screenshot(displayMetrics.widthPixels,
     displayMetrics.heightPixels);

4、TextureView截屏

private Bitmap getScreenshot(){
    mPlayerLayout.buildDrawingCache();
    Bitmap content = mTextureView.getBitmap();
    Bitmap layout  = mPlayerLayout.getDrawingCache();
    Bitmap screenshot = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_4444);
    // 把两部分拼起来,先把视频截图绘制到上下左右居中的位置,再把播放器的布局元素绘制上去。
    Canvas canvas = new Canvas(screenshot);
    canvas.drawBitmap(content, (layout.getWidth()-content.getWidth())/2, (layout.getHeight()-content.getHeight())/2, new Paint());
    canvas.drawBitmap(layout, 0, 0, new Paint());
    canvas.save();
    canvas.restore();
    return screenshot;
}

5、对视频资源截屏

private Bitmap getScreenshot(){
    mPlayerLayout.buildDrawingCache();
    Bitmap content = surfaceViewCapture();
    Bitmap layout  = mPlayerLayout.getDrawingCache();
    Bitmap screenshot = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_4444);
    // 把两部分拼起来,先把视频截图绘制到上下左右居中的位置,再把播放器的布局元素绘制上去。
    Canvas canvas = new Canvas(screenshot);
    canvas.drawBitmap(screen, (layout.getWidth()-screen.getWidth())/2, (layout.getHeight()-screen.getHeight())/2, new Paint());
    canvas.drawBitmap(layout, 0, 0, new Paint());
    canvas.save();
    canvas.restore();
    return screenshot;
}

private Bitmap surfaceViewCapture(){
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(Environment.getExternalStorageDirectory()+"/video.mp4");
    return mmr.getFrameAtTime(mMediaPlayer.getCurrentPosition() * 1000);
}

 

6、屏幕外录制

创建一个Surface

displayManager.createVirtualDisplay() 创建VirtualDisplay

displaManager.getDIsplay获取VirtualDisplay对应的Display

利用Presentation将要录制的内容弹出

 

7、录制主屏幕

创建一个Surface

MediaProjection

 

 

 

 

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部