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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值