Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 從FrameBuffer中讀取界面數據實現截圖

Android 從FrameBuffer中讀取界面數據實現截圖

編輯:關於Android編程

要實現從應用層截系統屏幕的功能 , 首先你的應用需要有讀取系統文件的權限 ,如何通過process = Runtime.getRuntime().exec(su); 的方式得到權限有在“Android底層事件注入,控制系統的觸摸、點擊、各個按鈕觸發”博客中提到 http://blog.csdn.net/fanjunjian1991/article/details/44648287 ,各位看官可前去瞧瞧。

 

這次截屏功能實現的方式主要讀取系統FrameBuffer裡的內容,利用/dev/graphics/fb0這個設備節點。【/dev/graphics/fb0代表了LCD的FrameBuffer緩沖區驅動程序,操作該驅動,即相當於操作LCD 的FrameBuffer。所以,由此出發,通過讀取/dev/graphics/fb0的內容,可以很容易就得到LCD屏幕上當前正在顯示的內容。】

讀取文件中數據的代碼如下:

 

	byte[] piex = new byte[width * height * deepth];
		InputStream stream = new FileInputStream(new File(file_name));
		DataInputStream dStream = new DataInputStream(stream);
		dStream.readFully(piex);
		dStream.close();
		stream.close();

然後把讀到的二進制數組轉換為整形數組:

 

 

int[] data = convertToColor(piex);
piex = null;

public static int[] convertToColor(byte[] piex) throws Exception {
		switch (deepth) {
		case 2:
			return convertToColor_2byte(piex);
		case 3:
			return convertToColor_3byte(piex);
		case 4:
			return convertToColor_4byte(piex);
		default:
			throw new Exception(Deepth Error!);
		}
	}
public static int[] convertToColor_2byte(byte[] piex) {
		int[] colors = new int[width * height];
		int len = piex.length;
		for (int i = 0; i < len; i += 2) {
			int colour = (piex[i+1] & 0xFF) << 8 | (piex[i] & 0xFF);
			int r = ((colour & 0xF800) >> 11)*8;
			int g = ((colour & 0x07E0) >> 5)*4;
			int b = (colour & 0x001F)*8;
			int a = 0xFF;
			colors[i / 2] = (a << 24) + (r << 16) + (g << 8) + b;
		}
		return colors;
	}

	public static int[] convertToColor_3byte(byte[] piex) {
		int[] colors = new int[width * height];
		int len = piex.length;
		for (int i = 0; i < len; i += 3) {
			int r = (piex[i] & 0xFF);
			int g = (piex[i + 1] & 0xFF);
			int b = (piex[i + 2] & 0xFF);
			int a = 0xFF;
			colors[i / 3] = (a << 24) + (r << 16) + (g << 8) + b;
		}
		return colors;
	}

	public static int[] convertToColor_4byte(byte[] piex) {
		int[] colors = new int[width * height];
		int len = piex.length;
		for (int i = 0; i < len; i += 4) {
			int r = (piex[i] & 0xFF);
			int g = (piex[i + 1] & 0xFF);
			int b = (piex[i + 2] & 0xFF);
			int a = (piex[i + 3] & 0xFF);
			colors[i / 4] = (a << 24) + (r << 16) + (g << 8) + b;
		}
		return colors;
	}

最後把整形數組轉換為位圖:

 

 

Bitmap bm = Bitmap.createBitmap(data, width, height, Bitmap.Config.RGB_565);
	    data = null;

 

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved