Android读取/dev/graphics/fb0 屏幕截图
Android屏幕截图有很多方式这里只使用其中一种截图
主要是读取/dev/graphics/fb0,进行转换,复杂点就在如何把读取的数据进行转换。
可以参考一下这篇文章:http://blog.chinaaet.com/detail/28298
下面给出程序代码
/**
* ScreenShotFb.java
* 版权所有(C) 2014
* 创建者:cuiran 2014-4-3 下午4:55:23
*/
package com.ghyf.mplay.util;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.ghyf.mplay.value.ConstantValue;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Display;
/**
* FrameBuffer中获取Android屏幕截图
* @author cuiran
* @version 1.0.0
*/
public class ScreenShotFb {
private static final String TAG="ScreenShotFb";
final static String FB0FILE1 = "/dev/graphics/fb0";
static File fbFile;
//程序入口
public static void shoot(){
try {
/************ 创建锁对象 ************/
final Object lock = new Object();
synchronized (lock) {
long start=System.currentTimeMillis();
Bitmap bitmap=getScreenShotBitmap();
long end=System.currentTimeMillis();
LogUtil.i(TAG, "getScreenShotBitmap time is :"+(end-start)+" ms");
String filePath= ConstantValue.ROOT_SDCARD_DIR+"/s.png";
// String filePath= ConstantValue.ROOT_SDCARD_DIR+"/screens/"+System.currentTimeMillis()+".png";
ScreenShotFb.savePic(bitmap,filePath);
}
}catch (Exception e) {
LogUtil.e(TAG, "Exception error",e);
}
}
//保存到sdcard
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) {
LogUtil.e(TAG, "FileNotFoundException error",e);
} catch (IOException e) {
LogUtil.e(TAG, "IOException error",e);
}
LogUtil.i(TAG, "savePic success");
}
public static void init(Activity activity){
try {
DisplayMetrics dm = new DisplayMetrics();
Display display = activity.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);
int deepth = localPixelFormat1.bytesPerPixel;// 位深
LogUtil.i(TAG, "deepth="+deepth);
piex = new byte[screenHeight * screenWidth*deepth] ;// 像素
colors = new int[screenHeight * screenWidth];
}catch(Exception e){
LogUtil.e(TAG, "Exception error",e);
}
}
static DataInputStream dStream=null;
static byte[] piex=null;
static int[] colors =null;
static int screenWidth;
static int screenHeight;
public static synchronized Bitmap getScreenShotBitmap() {
FileInputStream buf = null;
try {
fbFile = new File(FB0FILE1);
buf = new FileInputStream(fbFile);// 读取文件内容
dStream=new DataInputStream(buf);
dStream.readFully(piex);
dStream.close();
// 将rgb转为色值
for(int i=0;i<piex.length;i+=2)
{
colors[i/2]= (int)0xff000000 +
(int) (((piex[i+1]) << (16))&0x00f80000)+
(int) (((piex[i+1]) << 13)&0x0000e000)+
(int) (((piex[i]) << 5)&0x00001A00)+
(int) (((piex[i]) << 3)&0x000000f8);
}
// 得到屏幕bitmap
return Bitmap.createBitmap(colors, screenWidth, screenHeight,
Bitmap.Config.RGB_565);
} catch (FileNotFoundException e) {
LogUtil.e(TAG, "FileNotFoundException error",e);
} catch (IOException e) {
LogUtil.e(TAG, "IOException error",e);
}catch (Exception e) {
LogUtil.e(TAG, "Exception error",e);
}
finally {
if(buf!=null){
try {
buf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
}
调用时候需要先init 然后在shoot
看到很多朋友咨询得到的数据花屏和数组越界
这里跟设备的设备的位深和像素有关 需要修改这些代码。
// 将rgb转为色值
for(int i=0;i<piex.length;i+=2)
{
colors[i/2]= (int)0xff000000 +
(int) (((piex[i+1]) << (16))&0x00f80000)+
(int) (((piex[i+1]) << 13)&0x0000e000)+
(int) (((piex[i]) << 5)&0x00001A00)+
(int) (((piex[i]) << 3)&0x000000f8);
}
Android读取/dev/graphics/fb0 屏幕截图的更多相关文章
- 同步手绘板——关于/dev/graphics/fb0权限的获取
需要先将手机进行root,接着通过代码将/dev/graphics/fb0文件修改为可读的权限
- Android 读取Assets下的资源文件
做Android开发近半年了,东西越学越多,硬盘容量越来越小.很多东西找起来也不方便,为此,我打算从今天起把工作中学到的东西整理起来,写成日记.也希望与广大网友分享我的经验.一同进步.今天主要介绍文件 ...
- Android读取自定义View属性
Android读取自定义View属性 attrs.xml : <?xml version="1.0" encoding="utf-8"?> < ...
- Android udev /dev 设备节点权限
/************************************************************************* * Android udev /dev 设备节点权 ...
- Android读取JSON格式数据
Android读取JSON格式数据 1. 何为JSON? JSON,全称为JavaScript Object Notation,意为JavaScript对象表示法. JSON 是轻量级的文本数据交换格 ...
- Android - 读取JSON文件数据
Android读取JSON文件数据 JSON - JavaScript Object Notation 是一种存储和交换文本信息的语法. JSON对象在花括号中书写.用逗号来分隔值. JSON数组在方 ...
- Android 读取assets文件下的txt文件
android 读取assets文件下的txt文件,解决了读取txt文件的乱码问题: package com.example.com.scrollview; import java.io.Buffer ...
- Android读取asserts和raw文件夹下的文件
Android读取asserts和raw文件夹下的文件 经常需要用到读取“/res/raw”和"/asserts"文件夹下的文件,索性写成工具类方便以后使用. 一.raw文件夹下的 ...
- MTK Android 读取SIM卡参数,获取sim卡运营商信息
android 获取sim卡运营商信息(转) TelephonyManager tm = (TelephonyManager)Context.getSystemService(Context.TE ...
随机推荐
- 混合式应用开发之Cordova+vue(1)
一.Cordova创建应用 cordova create oneApp Cordova创建应用出错 Cordova安装时不能使用cnpm 应该使用npm,cnpm虽然快但是后期出的错绝对比这省下来的时 ...
- 原生JS实现圆周运动
<body> <div id="ball" style="width:20px; height:20px; background:red; border ...
- exp和imp的工作原理
--1.exp和imp的输入都是名字和值对: 如:exp parameter_name=value 或exp parameter_name=(value1,value2,value3..) --2.e ...
- 排序算法的C语言实现(上 比较类排序:插入排序、快速排序与归并排序)
总述:排序是指将元素集合按规定的顺序排列.通常有两种排序方法:升序排列和降序排列.例如,如整数集{6,8,9,5}进行升序排列,结果为{5,6,8,9},对其进行降序排列结果为{9,8,6,5}.虽然 ...
- java.lang.ClassCastException: oracle.sql.CLOB cannot be cast to oracle.sql.CLOB
错误现象: [framework] 2016-05-26 11:34:53,590 -INFO [http-bio-8080-exec-7] -1231863 -com.dhcc.base.db.D ...
- SSA-一种适合中小型企业的新型服务架构
写在前面 好久好久没写了,最近刚换了工作,花了几天的时候熟悉了项目,接着就是功能的完善,随后就是对新项目的基础架构搭建. 看过Po主博客的都知道,Po主一直致力于推广.Net Core在微服务架构上的 ...
- python笔记五(条件判断/循环/break和continue)
一 条件判断 if <条件判断1>: <执行1> elif <条件判断2>: <执行2> elif <条件判断3>: <执行3> ...
- 通讯协议序列化解读(一) Protobuf详解教程
前言:说到JSON可能大家很熟悉,是目前应用最广泛的一种序列化格式,它使用起来简单方便,而且拥有超高的可读性.但是在越来越多的应用场景里,JSON冗长的缺点导致它并不是一种最优的选择. 一.常用序列化 ...
- CSAPP缓冲区溢出攻击实验(下)
CSAPP缓冲区溢出攻击实验(下) 3.3 Level 2: 爆竹 实验要求 这一个Level的难度陡然提升,我们要让getbuf()返回到bang()而非test(),并且在执行bang()之前将g ...
- activity的启动模式和栈管理
在学习Android的过程中,Intent是我们最常用Android用于进程内或进程间通信的机制,其底层的通信是以Binder机制实现的,在物理层则是通过共享内存的方式实现的. Intent ...