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 屏幕截图的更多相关文章

  1. 同步手绘板——关于/dev/graphics/fb0权限的获取

    需要先将手机进行root,接着通过代码将/dev/graphics/fb0文件修改为可读的权限

  2. Android 读取Assets下的资源文件

    做Android开发近半年了,东西越学越多,硬盘容量越来越小.很多东西找起来也不方便,为此,我打算从今天起把工作中学到的东西整理起来,写成日记.也希望与广大网友分享我的经验.一同进步.今天主要介绍文件 ...

  3. Android读取自定义View属性

    Android读取自定义View属性 attrs.xml : <?xml version="1.0" encoding="utf-8"?> < ...

  4. Android udev /dev 设备节点权限

    /************************************************************************* * Android udev /dev 设备节点权 ...

  5. Android读取JSON格式数据

    Android读取JSON格式数据 1. 何为JSON? JSON,全称为JavaScript Object Notation,意为JavaScript对象表示法. JSON 是轻量级的文本数据交换格 ...

  6. Android - 读取JSON文件数据

    Android读取JSON文件数据 JSON - JavaScript Object Notation 是一种存储和交换文本信息的语法. JSON对象在花括号中书写.用逗号来分隔值. JSON数组在方 ...

  7. Android 读取assets文件下的txt文件

    android 读取assets文件下的txt文件,解决了读取txt文件的乱码问题: package com.example.com.scrollview; import java.io.Buffer ...

  8. Android读取asserts和raw文件夹下的文件

    Android读取asserts和raw文件夹下的文件 经常需要用到读取“/res/raw”和"/asserts"文件夹下的文件,索性写成工具类方便以后使用. 一.raw文件夹下的 ...

  9. MTK Android 读取SIM卡参数,获取sim卡运营商信息

    android 获取sim卡运营商信息(转)   TelephonyManager tm = (TelephonyManager)Context.getSystemService(Context.TE ...

随机推荐

  1. Java 反射(二)

    作者:郑剑锋链接:https://www.zhihu.com/question/24304289/answer/147529485来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  2. Oracle中打印99乘法表的13种方法

    --实现1: select r1 || '*' || r1 || '=' || r1 * r1 A, decode(r2, '', '', r2 || '*' || r1 || '=' || r2 * ...

  3. ACM Adding Reversed Numbers(summer2017)

    The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancien ...

  4. iOS开源加密相册Agony的实现(七)

    简介 虽然目前市面上有一些不错的加密相册App,但不是内置广告,就是对上传的张数有所限制.本文介绍了一个加密相册的制作过程,该加密相册将包括多密码(输入不同的密码即可访问不同的空间,可掩人耳目).Wi ...

  5. Swift中方法(method)所谓的lazy绑定简介

    我们知道在ruby之类的动态语言中对象方法可以先从类中预先抽取,然后再应用到某个具体对象上.这称为无绑定的method对象,也可以叫做lazy绑定. 下面举个例子: irb(main):004:0&g ...

  6. Swift中关于任意类型的数组

    在Objc中你是不可以把一个非对象类型放入数组的,你必须将其"封箱",然后再放入数组. 在Swift中你可将非对象类型轻松放入数组: let ary = [1,2,3] 你可以明确 ...

  7. Python 3 re模块3个括号相关的语法

    (?aiLmsux) (One or more letters from the set 'a', 'i', 'L', 'm', 's', 'u', 'x'.) The group matches t ...

  8. 微信小程序基础之新建的项目文件图解

    昨天发布的文章,感觉对于学习不够直观,所以今天重点在图标上进行了详细的对应介绍,稍后会尝试开发小程序控件的使用.转载请标注出处,谢谢!

  9. 多线程并发之java内存模型JMM

    多线程概念的引入是人类又一次有效压寨计算机的体现,而且这也是非常有必要的,因为一般运算过程中涉及到数据的读取,例如从磁盘.其他系统.数据库等,CPU的运算速度与数据读取速度有一个严重的不平衡,期间如果 ...

  10. android获取短信并自动填充

    package com.velo.quanquan.util; import java.util.regex.Matcher; import java.util.regex.Pattern; impo ...