1. 程式人生 > >Android實現截圖方式整理(總結)

Android實現截圖方式整理(總結)

http://www.jb51.net/article/119881.htm

本文介紹了Android 實現截圖方式整理,分享給大家。希望對大家有幫助

可能的需求:

  1. 截自己的屏
  2. 截所有的屏
  3. 帶導航欄截圖
  4. 不帶導航欄截圖
  5. 截圖並編輯選取一部分
  6. 自動擷取某個空間或者佈局
  7. 擷取長圖
  8. 在後臺去截圖

1.只擷取自己應用內部介面

1.1 擷取除了導航欄之外的螢幕

View dView = getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
if (bitmap != null) {
  try {
    // 獲取內建SD卡路徑
    String sdCardPath = Environment.getExternalStorageDirectory().getPath();
    // 圖片檔案路徑
    String filePath = sdCardPath + File.separator + "screenshot.png";
    File file = new File(filePath);
    FileOutputStream os = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    os.flush();
    os.close();
    DebugLog.d("a7888", "儲存完成");
  } catch (Exception e) {
  }
}

1.2 擷取某個控制元件或者區域

兩種方案:

跟上面差不多,只不過view不適用根view,而是使用某個某個控制元件。

View dView = title;
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());

手動draw

View dView = titleTv;
Bitmap bitmap = Bitmap.createBitmap(dView.getWidth(), dView.getHeight(), Bitmap.Config.ARGB_8888);
//使用Canvas,呼叫自定義view控制元件的onDraw方法,繪製圖片
Canvas canvas = new Canvas(bitmap);
dView.draw(canvas);

1.3 擷取帶導航欄的整個螢幕

​ 這一小節會將一些理論上可以,但是實踐會特別複雜,不太推薦使用。可以學習瞭解。

adb 命令

這裡指的不是連線電腦進行adb操控,而是在App內部實現adb命令的操控

在APK中呼叫“adb shell screencap -pfilepath” 命令

該命令讀取系統的framebuffer,需要獲得系統許可權:

(1). 在AndroidManifest.xml檔案中新增

<uses-permissionandroid:name="android.permission.READ_FRAME_BUFFER"/>

(2). 修改APK為系統許可權,將APK放到原始碼中編譯, 修改Android.mk

LOCAL_CERTIFICATE := platform
publicvoid takeScreenShot(){ 
  String mSavedPath = Environment.getExternalStorageDirectory()+File. separator + "screenshot.png" ; 
try {           
      Runtime. getRuntime().exec("screencap -p " + mSavedPath); 
  } catch (Exception e) { 
      e.printStackTrace(); 
  } 

利用系統的隱藏API,實現Screenshot,這部分程式碼是系統隱藏的,需要在原始碼下編譯。

1).修改Android.mk, 新增系統許可權

LOCAL_CERTIFICATE := platform

2).修改AndroidManifest.xml 檔案,新增許可權

<uses-permissionandroid:name="android.permission.READ_FRAME_BUFFER"/>
 public boolean takeScreenShot(String imagePath){
       if(imagePath.equals("" )){
           imagePath = Environment.getExternalStorageDirectory()+File. separator+"Screenshot.png" ;
       }
           
     Bitmap mScreenBitmap;
     WindowManager mWindowManager;
     DisplayMetrics mDisplayMetrics;
     Display mDisplay;
         
     mWindowManager = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);
     mDisplay = mWindowManager.getDefaultDisplay();
     mDisplayMetrics = new DisplayMetrics();
     mDisplay.getRealMetrics(mDisplayMetrics);
                 
     float[] dims = {mDisplayMetrics.widthPixels , mDisplayMetrics.heightPixels };
     mScreenBitmap = Surface. screenshot((int) dims[0], ( int) dims[1]);
           
     if (mScreenBitmap == null) { 
         return false ;
     }
         
    try {
     FileOutputStream out = new FileOutputStream(imagePath);
     mScreenBitmap.compress(Bitmap.CompressFormat. PNG, 100, out);
       
    } catch (Exception e) {
        
        
     return false ;
    }    
              
    return true ;
}

Android本地程式設計(Native Programming)讀取framebuffer

命令列,框架的截圖功能是通過framebuffer來實現的,所以我們先來介紹一下framebuffer。

framebuffer介紹

幀緩衝(framebuffer)是Linux為顯示裝置提供的一個介面,把視訊記憶體抽象後的一種裝置,他允許上層應用程式在圖形模式下直接對顯示緩衝區進行 讀寫操作。這種操作是抽象的,統一的。使用者不必關心物理視訊記憶體的位置、換頁機制等等具體細節。這些都是由Framebuffer裝置驅動來完成的。

linux FrameBuffer 本質上只是提供了對圖形裝置的硬體抽象,在開發者看來,FrameBuffer 是一塊顯示快取,往顯示快取中寫入特定格式的資料就意味著向螢幕輸出內容。所以說FrameBuffer就是一塊白板。例如對於初始化為16 位色的FrameBuffer 來說, FrameBuffer中的兩個位元組代表螢幕上一個點,從上到下,從左至右,螢幕位置與記憶體地址是順序的線性關係。
幀快取有個地址,是在記憶體裡。我們通過不停的向frame buffer中寫入資料, 顯示控制器就自動的從frame buffer中取資料並顯示出來。全部的圖形都共享記憶體中同一個幀快取。

android截圖實現思路

Android系統是基於Linux核心的,所以也存在framebuffer這個裝置,我們要實現截圖的話只要能獲取到framebuffer中的資料,然後把資料轉換成圖片就可以了,android中的framebuffer資料是存放在 /dev/graphics/fb0 檔案中的,所以我們只需要來獲取這個檔案的資料就可以得到當前螢幕的內容。

現在我們的測試程式碼執行時候是通過RC(remote controller)方式來執行被測應用的,那就需要在PC機上來訪問模擬器或者真機上的framebuffer資料,這個的話可以通過android的ADB命令來實現。

各大手機自帶的按鍵組合進行截圖

Android原始碼中對按鍵的捕獲位於檔案PhoneWindowManager.java(alps\frameworks\base\policy\src\com\android\internal\policy\impl)中,這個類處理所有的鍵盤輸入事件,其中函式interceptKeyBeforeQueueing()會對常用的按鍵做特殊處理。

2. 擷取非含當前應用的螢幕部分(最佳官方方案)

​ Android 在5.0 之後支援了實時錄屏的功能。通過實時錄屏我們可以拿到截圖的影象。同時可以通過在Service中處理實現後臺的錄屏。具體的類講解大家自行網上查閱。

大體步驟:

1.初始化一個MediaProjectionManager。

複製程式碼程式碼如下:
MediaProjectionManager mMediaProjectionManager = (MediaProjectionManager)getApplication().getSystemService(Context.MEDIA_PROJECTION_SERVICE);

2.建立intent,並啟動Intent。注意這裡是startActivityForResult

複製程式碼程式碼如下:
startActivityForResult(mMediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);

3.在onActivityResult中拿到Mediaprojection。

mResultCode = resultCode;
mResultData = data;
mMediaProjection = mMediaProjectionManager.getMediaProjection(mResultCode, mResultData);

4.設定VirtualDisplay 將影象和展示的View關聯起來。一般來說我們會將影象展示到SurfaceView,這裡為了為了便於拿到截圖,我們使用ImageReader,他內建有SurfaceView。

mImageReader = ImageReader.newInstance(windowWidth, windowHeight, 0x1, 2); //ImageFormat.RGB_565
mVirtualDisplay = mMediaProjection.createVirtualDisplay("screen-mirror",
        windowWidth, windowHeight, mScreenDensity,       DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
        mImageReader.getSurface(), null, null);

5.通過ImageReader拿到截圖

strDate = dateFormat.format(new java.util.Date());
nameImage = pathImage+strDate+".png";

Image image = mImageReader.acquireLatestImage();
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();

6.注意截圖之後要及時關閉VirtualDisplay ,因為VirtualDisplay 是十分消耗記憶體和電量的。

if (mVirtualDisplay == null) {
      return;
}
mVirtualDisplay.release();
mVirtualDisplay = null;

ps: 具體可以參考Google官方給的demo以及其他開發者寫的Demo

3. 擷取長屏

​ 擷取長屏其實原理就是擷取整個ScrollView或者ListView的檢視,因此實現原理跟上面中提到的擷取某個控制元件的View基本一致。

ScrollView 實現截圖

  /**
   * 擷取scrollview的螢幕
   * **/
  public static Bitmap getScrollViewBitmap(ScrollView scrollView) {
    int h = 0;
    Bitmap bitmap;
    for (int i = 0; i < scrollView.getChildCount(); i++) {
      h += scrollView.getChildAt(i).getHeight();
    }
    // 建立對應大小的bitmap
    bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
        Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    scrollView.draw(canvas);
    return bitmap;
  }

ListView實現截圖

 /**
   * 截圖listview
   * **/
  public static Bitmap getListViewBitmap(ListView listView,String picpath) {
    int h = 0;
    Bitmap bitmap;
    // 獲取listView實際高度
    for (int i = 0; i < listView.getChildCount(); i++) {
      h += listView.getChildAt(i).getHeight();
    }
    Log.d(TAG, "實際高度:" + h);
    Log.d(TAG, "list 高度:" + listView.getHeight());
    // 建立對應大小的bitmap
    bitmap = Bitmap.createBitmap(listView.getWidth(), h,
        Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    listView.draw(canvas);
    return bitmap;
  }

WebView實現截圖

//這是webview的,利用了webview的api
private static Bitmap captureWebView(WebView webView) {
    Picture snapShot = webView.capturePicture();
    Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(),
        snapShot.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    snapShot.draw(canvas);
    return bmp;
  }

有時候我們可能需要去滾動螢幕,然後再滾動到某一個地方時停止截圖,這樣就會去擷取從開始到滾動結束位置的view,而不是整個ScrollView,這個時候就需要進行一些控制,具體原理跟上面講的差不多,可以參考一下下面的實現:

4. 實時截圖

​ 可參考2中Android 在5.0的做法,進行實時錄製。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援指令碼之家。