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
分享到:
相关推荐
代码示例,自己研究的通过读取/dev/graphics/fb0文件解析出系统界面的图片 需要root系统才能使用 可以截取android系统所有界面的图片,需要做系统截图的可以看看
通过/dev/graphics/fb0 获取安卓屏幕分辨率,代码简单易懂。
alogcat android app 读取/显示logcat信息。
写了一个demo,放到源码下面编译,对dev/ttyMT1进行操作,在系统的init.rc里面要把dev/ttyMT1的权限改一下才能读写。
带OTG功能安卓设备读取USB外设信息 可以通过AS调试读出 , 过滤关键字 "Li_Debug" , 一次显示使用USB外设 格式举例 厂商/VendorId: 0x10C4 产品/ProductId: 0xEA60 设备/DeviceId: 0x7D2 接口数/InterfaceCount: 1 ...
代码如下:// 读取sdcard文件private void sdcardRead(){String fileName = “/sdcard/my_sdcard.txt”;// assets下文件//String fileName = “/sdcard/test/my_sdcard_test.txt”;// sdcard下子目录文件String ret =...
读取/proc/cmdline 文件中的标志位信息。。
android C语言读取fb0实现截屏,并保存为rgb565的bmp。 pwd /cygdrive/e/myprj/screenshot/jni adb push ../libs/armeabi/save /data/local adb shell cd /data/local chmod 777 save ./save 截屏保存到/mnt/sdcard/...
android读取doc/docx/xls转换为html,亲测可用。费了好大力气,终于把它搞定了!
linux平台下,读取/proc/cpuinfo获取CPU信息demo
android 屏幕录制,Android 录屏的解决方案是通过读取 /dev/graphics/fb0 里面的 frame buffer,然后再通过各种开源编码器转为视频,但必须要 root 才行,而且有些手机即便你 root 还是不能成功,获取到的 ...
本人门外汉一个。博客所提及的一些东西均百度得来 (如果哪里不对请指出) 先记录一下我碰到的问题。折磨了我好多天 做一个比较特殊的功能 安卓触摸事件注入,(模拟安卓触摸操作) 我碰到的问题如下面这个链接(权限...
串口通讯: ①在JNI定义了几个函数,简单的打开串口(串口/dev/ttyS2,波特率57600),关闭串口,读取串口数据,向串口中写数据。 ②给Java中暴露了几个native接口。 ③利用Runnable + Handler实现循环从串口中读取数...
pcf8563_i2c1_r8_ruoge_ov2640通过给RTC驱动增加设备节点读取秒钟成功+直接读取I2C1获取秒钟值20160626_2201.7z http://blog.csdn.net/21cnbao/article/details/7919055 在Android源码树中添加userspace I2C读写...
Linux下I.MX6U开发板的摄像头应用程序。...通过该程序,能够实现摄像头数据显示在LCD上,通过该程序,可以掌握Linux应用开发中v4l2框架的摄像头驱动函数,以及对/dev/video0,/dev/fb0的打开、读取和关闭等操作。
使用PhoenixSuit将sun8iw6p1_android_perf3_v1_0_uart0.img这个固件刷机之后,可以看见显示器满屏幕了。 设置菜单里面的WIFI/BT等等选项的打开部分按钮缺失(感觉是拉伸的)。 使用360手机助手(安装之后是竖屏)...
android 使用SharedPreferences对数据存储/删除/读取/文件删除
YUV 文件读取,一个c ++ 写的可以正确读取YUV 4:2:2的YUV 文件格式.
C# 编写的操作Excel读取/写入动态库(COM/NPOI两种方式),个人推荐使用NPOI方式,因为COM方式存在无法关闭Excel进程的情况(个人体验COM方式效率有点低)。。。已设置0分,资源自取。
android/linux 读取寄存器值