Android 从FrameBuffer中读取界面数据实现截图做手游直播

要实现从应用层截系统屏幕的功能 , 首先你的应用需要有读取系统文件的权限 ,如何通过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屏幕上当前正在显示的内容。】

读取文件中数据的代码如下:

 

<span style="white-space:pre">	</span>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();

 

然后把读到的byte[]数组转换为rgb数组:

 

int[] data = convertToColor(piex);
piex = null;
</pre><pre name="code" class="html">public static int[] convertToColor(byte[] piex) throws Exception {
<span style="white-space:pre">		</span>switch (deepth) {
<span style="white-space:pre">		</span>case 2:
<span style="white-space:pre">			</span>return convertToColor_2byte(piex);
<span style="white-space:pre">		</span>case 3:
<span style="white-space:pre">			</span>return convertToColor_3byte(piex);
<span style="white-space:pre">		</span>case 4:
<span style="white-space:pre">			</span>return convertToColor_4byte(piex);
<span style="white-space:pre">		</span>default:
<span style="white-space:pre">			</span>throw new Exception("Deepth Error!");
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值