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