[置顶] Android读取/dev/graphics/fb0 屏幕截图

2018阿里云全部产品优惠券(好东东,强烈推荐)
领取地址 https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=82lei0yp

Android屏幕截图有很多方式这里只使用其中一种截图

主要是读取/dev/graphics/fb0,进行转换,复杂点就在如何把读取的数据进行转换。

可以参考一下这篇文章:http://blog.chinaaet.com/detail/28298

下面给出程序代码

推荐:Android平台截图研究 FrameBuffer(/dev/graphics/fb0) 文件内容研究!(含源码工程)

[啊赛]

/**
 * 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);  
                }  

相关推荐