1. 程式人生 > >Android截圖(fb0)

Android截圖(fb0)

1.申請獲取root許可權,這一步很重要,不然會沒有作用

private void getPermession() {
    try {
        Process process = Runtime.getRuntime().exec("su");
        // 獲取輸出流
        OutputStream outputStream = process.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        String cmd = "su \nchmod 777 /dev/graphics/fb0 \n"
; dataOutputStream.writeBytes(cmd); dataOutputStream.flush(); dataOutputStream.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } }

2.手機解析度

private void getPhoneProfile() {
    DisplayMetrics dm = new DisplayMetrics();
    Display display = this
.getWindowManager().getDefaultDisplay(); display.getMetrics(dm); screenWidth = dm.widthPixels; // 螢幕寬(畫素,如:480px) screenHeight = dm.heightPixels; // 螢幕高(畫素,如:800p) int pixelformat = display.getPixelFormat(); PixelFormat localPixelFormat1 = new PixelFormat(); PixelFormat.getPixelFormatInfo(pixelformat, localPixelFormat1); deepth = localPixelFormat1.bytesPerPixel;// 位深
Log.e(TAG, "getFB0: "+ deepth); piex = new byte[screenHeight * screenWidth*deepth] ;// 畫素 colors = new int[screenHeight * screenWidth]; }

3.讀取檔案內容

private Bitmap getFB0() {
    Bitmap bitmap = null;
    try {
        FileInputStream buf = null;
        fbFile = new File(FB0FILE1);
        buf = new FileInputStream(fbFile);// 讀取檔案內容
        dStream=new DataInputStream(buf);
        dStream.readFully(piex);
        dStream.close();
        // 將rgb轉為色值,解析fb0
        for (int m = 0; m < colors.length; m++) {
            int b = (piex[m * 4] & 0xFF);
            int g = (piex[m * 4 + 1] & 0xFF);
            int r = (piex[m * 4 + 2] & 0xFF);
            int a = (piex[m * 4 + 3] & 0xFF);
            colors[m] = (a << 24) + (r << 16) + (g << 8) + b;
            if (m < 10) {
                Log.e(TAG, "getFB0: " + piex[m * 4]);
                Log.e(TAG, "getFB0: " + piex[m * 4+ 1]);
                Log.e(TAG, "getFB0: " + piex[m * 4+ 2]);
                Log.e(TAG, "getFB0: " + piex[m * 4+ 3]);
                Log.e(TAG, "getFB0: " + b);
                Log.e(TAG, "getFB0: " + g);
                Log.e(TAG, "getFB0: " + r);
                Log.e(TAG, "getFB0: " + a);
            }
        }


        // 得到螢幕bitmap
        bitmap = Bitmap.createBitmap(colors, screenWidth, screenHeight,
                Bitmap.Config.RGB_565);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

4.儲存截圖

public static void savePic(Bitmap b,String strFileName){
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(strFileName);
        if (null != fos)
        {
            b.compress(Bitmap.CompressFormat.PNG, 50, fos);
            fos.flush();
            fos.close();
        }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

遺留問題
fdb0解析成bitmap不滿足需求,需要對手機的系統配置分析之後對應解析fdb0