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. tar包和jar包和war包的区别?

    tar:tar是*nix下的打包工具,生成的包通常也用tar作为扩展名,其实tar只是负责打包,不一定有压缩,事实上可以压缩,也可以不压缩,通常你看到xxxx.tar.gz,就表示这个tar包是压缩的 ...

  2. web.xml is missing and <failOnMissingWebXml> is set to true

    这时候需要右击项目-->Java EE Tools-->Generate Deployment Descriptor Stub .然后系统会在src/main/webapp/WEB_INF ...

  3. iOS Push详述,了解一下?

    WeTest 导读 本文主要对iOS Push的在线push.本地push及离线(远程)push进行梳理,介绍了相关逻辑,测试时要注意的要点以及相关工具.小小的Push背后蕴藏着大大的逻辑! Push ...

  4. jQuery 效果 – 滑动

    jQuery 滑动方法可使元素上下滑动. 点击这里,隐藏/显示面板 一寸光阴一寸金,因此,我们为您提供快捷易懂的学习内容. 在这里,您可以通过一种易懂的便利的模式获得您需要的任何知识. 实例 jQue ...

  5. TCP发送源码学习(1)--tcp_sendmsg

    一.tcp_sendmsg()函数分析: int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, size_t ...

  6. Programming In Scala笔记-第十七章、Scala中的集合类型

    本章主要介绍Scala中的集合类型,主要包括:Array, ListBuffer, Arraybuffer, Set, Map和Tuple. 一.序列 序列类型的对象中包含多个按顺序排列好的元素,可以 ...

  7. cocos2dx 3.2之Lua打飞机项目

    1          创建lua打飞机项目 cocos new T32Lua -dE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\projects -l lua 2 ...

  8. springMVC源码分析--HandlerInterceptor拦截器调用过程(二)

    在上一篇博客springMVC源码分析--HandlerInterceptor拦截器(一)中我们介绍了HandlerInterceptor拦截器相关的内容,了解到了HandlerInterceptor ...

  9. x264源代码简单分析:x264命令行工具(x264.exe)

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  10. x264源代码简单分析:概述

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...