Android 截屏的三种方法

第一种 调用“su”命令执行截屏

执行“su”命令,需要设置为系统程序,android:sharedUserId="android.uid.system",需要root权限。
方法如下:

/**
     * 屏幕截图
     * 适用于lanucher版
     */
    public static void shotScreen() {
        //adb截图方法
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.e("whh0914", "开始屏幕截图...");
                String filepath = "/sdcard/screenShot.png";
                try {
                    RootCmdUtils.execRootCmdSilent("screencap -p " + filepath);
                } catch (Exception e) {
                    Log.e("whh0914", "屏幕截图出现异常:" + e.toString());             
                }
            }
        }).start();
    }

/**
     * 执行命令但不关注结果输出
     */
    public static int execRootCmdSilent(String cmd) {
        int result = -1;
        DataOutputStream dos = null;

        try {
            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());

            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

否则会报
java.io.IOException: Cannot run program : error=13, Permission denied

优缺点:命令行简单方便,对于复杂的页面或存在帧流(surfaceView等控件)也能轻松获取截屏,但是运行到手机上,由于硬件限制,系统权限基本难拿到,容易出异常而导致方法失效,因此该方法依硬件权限按需取

第二种 原生的截图方法,获取View的缓存作为截屏结果

    /**
     * 屏幕截图
     */
    public static void screenShot1(Activity activity, ScreenShotReqBean screenShotReqBean) {
        String filepath = "/sdcard/screenShot.png";
        Bitmap bitmap = null;
        try {
            Log.e("whh0914", "111开始屏幕截图...");

            //截图
            activity.getWindow().getDecorView().setDrawingCacheEnabled(true);
            bitmap = activity.getWindow().getDecorView().getDrawingCache();

            //保存图片
            FileOutputStream fos = new FileOutputStream(filepath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);
        } catch (Exception e) {
            Log.e("whh0914", "111屏幕截图出现异常:" + e.toString());
        }
    }

优缺点:原生的截图方法,依赖于界面的内容,如果界面中包含webView、surfaceView等控件,截图出来该控件区域出现一片黑,无法达到截图到帧的效果。因此该方法按界面控件按需取。

第三种 通过 MediaProjectionManager 获取截图

    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);
        }
    }
//保存bitmap文件
public static void saveBitmap(Bitmap bitmap, String filePath) {
        File f = new File(filePath);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

优缺点:完美地解决了方法一、方法二存在的问题,完美!无需root权限,不用考虑界面中surfaceView等控件渲染问题。但是只能获取到当前APP内部正在运行Activity的截图,如果需要多个界面截图,最好在BaseActivity使用。如果需要获取APP外部界面截图,可通过读取framebuffer内容,解析图片。

每天进步一点点。。。(2020-07-09)

你可能感兴趣的:(Android 截屏的三种方法)