Android 截取手機屏幕兩種實現方案解析


近期在開發的過程中,遇到了一個須要截取屏幕保存為圖片的需求,詳細為截取webview的視圖保存圖片。


方法1:首先想到的思路是利用SDK提供的View.getDrawingCache()方法:

  public void printScreen(View view) {
        String imgPath = "/sdcard/test.png";
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        if (bitmap != null) {
            try {
                FileOutputStream out = new FileOutputStream(imgPath);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100,
                        out);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

這種方法在非常多情況下都是沒有問題的。比方說截取imageview,TextView,甚至otherview.getRootView();都沒問題。但在WebView上就會出現webview的部分截取完缺少頁面里的一些內容的情況。比方說用webview打開這個(https://miqt.github.io/jellyfish/)界面。截取的圖片就會有問題。詳細表現為網頁中游動的水母沒有顯示在截取的圖片上。


方法2:使用Android系統提供的服務Context.MEDIA_PROJECTION_SERVICE。進行截圖操作。

Demo源代碼:https://github.com/miqt/CapWindow(歡迎star!)

關鍵部分代碼解析:↓

發送截圖請求

 final MediaProjectionManager projectionManager = (MediaProjectionManager)
                getSystemService(Context.MEDIA_PROJECTION_SERVICE);
 Intent intent = projectionManager.createScreenCaptureIntent();
 startActivityForResult(intent, REQUEST_CODE);

接收返回的結果:


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        handleScreenShotIntent(resultCode, data);
    }
    private void handleScreenShotIntent(int resultCode, Intent data) {

        onScreenshotTaskBegan();
        final MediaProjectionManager projectionManager = (MediaProjectionManager)
                getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        final MediaProjection mProjection = projectionManager.getMediaProjection(resultCode, data);
        Point size = Utils.getScreenSize(this);
        final int mWidth = size.x;
        final int mHeight = size.y;
        final ImageReader mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat
                .RGBA_8888, 2);
        final VirtualDisplay display = mProjection.createVirtualDisplay("screen-mirror", mWidth,
                mHeight, DisplayMetrics.DENSITY_MEDIUM,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION, mImageReader.getSurface(),
                null, null);

        mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader mImageReader) {

                Image image = null;
                try {
                    image = mImageReader.acquireLatestImage();
                    if (image != null) {
                        final Image.Plane[] planes = image.getPlanes();
                        if (planes.length > 0) {
                            final ByteBuffer buffer = planes[0].getBuffer();
                            int pixelStride = planes[0].getPixelStride();
                            int rowStride = planes[0].getRowStride();
                            int rowPadding = rowStride - pixelStride * mWidth;


                            // create bitmap
                            Bitmap bmp = Bitmap.createBitmap(mWidth + rowPadding / pixelStride,
                                    mHeight, Bitmap.Config.ARGB_8888);
                            bmp.copyPixelsFromBuffer(buffer);

                            Bitmap croppedBitmap = Bitmap.createBitmap(bmp, 0, 0, mWidth, mHeight);

                            saveBitmap(croppedBitmap);//保存圖片

                            if (croppedBitmap != null) {
                                croppedBitmap.recycle();
                            }
                            if (bmp != null) {
                                bmp.recycle();
                            }
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (image != null) {
                        image.close();
                    }
                    if (mImageReader != null) {
                        mImageReader.close();
                    }
                    if (display != null) {
                        display.release();
                    }

                    mImageReader.setOnImageAvailableListener(null, null);
                    mProjection.stop();

                    onScreenshotTaskOver();
                }

            }
        }, getBackgroundHandler());
    }

這種方法相似使用手機的系統截屏(音量下鍵+電源鍵)。可以完美的吧當前原模原樣的屏幕截取下來,而且改動保存方法的話甚至可以屏幕錄像,但相比於第一種方法。它的缺點是全然和界面上的view沒有關系,而且在調用這個服務的時候,會彈出一個權限確認的彈框。另外須要注意。這一方法僅僅能在Android 5.0的系統設備上適用。

總結:

總而言之。這兩種方法各有利弊,使用的時候要依據自己的實際需求做出選擇。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM