Android读取/dev/graphics/fb0 屏幕截图

来源:互联网 发布:ipad 抓包淘宝旧版本 编辑:程序博客网 时间:2024/06/03 22:46

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);            }       // 得到屏幕bitmapreturn 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 blocke.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);  
                }  

3 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果手机音乐里的歌删掉了怎么办 上课学生把粉笔擦藏起来了怎么办 手机没法解压10g压缩包怎么办 苹果id被锁邮箱也忘记密码怎么办 手机号注册的微信忘记密码怎么办 qq号注册微信忘记密码怎么办 云课堂密码忘记手机号更改了怎么办 孩子做事才要奖励怎么办的教案 员工做事效率低家庭情况不好怎么办 安全云课堂人脸识别失败怎么办 酷我k歌里音乐太少怎么办 酷狗里面的歌没有了怎么办 第一试用网登录密码忘记了怎么办 第一试用网登录密码忘了怎么办 微博登录直播间帐号被锁定怎么办 硫酸不小心弄到皮肤上怎么办 小米平板进水了开不了机怎么办 U盘被弹出后再插没有显示怎么办 视频从U盘传下来不能播放怎么办 微博账号忘了登录名怎么办 迅雷应版权方要求无法下载怎么办? 微信账号忘记密码上面有钱怎么办 蘑菇街账号忘记了里面有钱怎么办 手机注册的百度云帐号换号怎么办 网易云音乐sd卡写入失败怎么办? 玩客云涌u盘满了怎么办 手机安装杜比音效失败死机该怎么办 网易云音乐下载额度用完了怎么办 苹果手机付费app连续扣费怎么办 oppoO粉卡的流量用没了怎么办 qq邮箱里的文件过期了怎么办 163邮箱里的文件过期了怎么办 做人事的给员工漏交社保了怎么办 公司漏交了员工一个月的社保怎么办 小米4c能充电不能开机怎么办 小米手机关机开不开机该怎么办 小米5s关机开不开机怎么办 老板跑路了员工社保还挂着怎么办 红米5连wifi很慢怎么办 电脑百度网盘下载速度慢怎么办 新手机与旧手机同步了怎么办