【android】android2.2截屏研究(1)

一、调用api接口view.getDrawingCache()获取抓取当前屏幕的截图

调用接口view.getDrawingCache()获取当前绘制缓存中的位图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.screenshoot;
  
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
  
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
  
public class ScreenShot {
     // 获取指定Activity的截屏,保存到png文件
     private static Bitmap takeScreenShot(Activity activity) {
         // View是你需要截图的View
         View view = activity.getWindow().getDecorView();
         view.setDrawingCacheEnabled( true );
         view.buildDrawingCache();
         Bitmap b1 = view.getDrawingCache();
  
         // 获取状态栏高度
         Rect frame = new Rect();
         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
         int statusBarHeight = frame.top;
         Log.i( "TAG" , "" + statusBarHeight);
  
         // 获取屏幕长和高
         int width = activity.getWindowManager().getDefaultDisplay().getWidth();
         int height = activity.getWindowManager().getDefaultDisplay()
                 .getHeight();
         // 去掉标题栏
         // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
         Bitmap b = Bitmap.createBitmap(b1, 0 , statusBarHeight, width, height
                 - statusBarHeight);
         view.destroyDrawingCache();
         return b;
     }
  
     // 保存到sdcard
     private static void savePic(Bitmap b, String strFileName) {
         FileOutputStream fos = null ;
         try {
             fos = new FileOutputStream(strFileName);
             if ( null != fos) {
                 b.compress(Bitmap.CompressFormat.PNG, 90 , fos);
                 fos.flush();
                 fos.close();
             }
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
  
     // 程序入口
     public static void shoot(Activity a,String b) {
         //ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
         ScreenShot.savePic(ScreenShot.takeScreenShot(a), b);
     }
}

 

方案缺点:

1、只能抓取静态view。对于正在播放视频(采用surfaceview)或者动态壁纸等等,抓屏结果为黑屏。这是因为surfaceview不同于view,底层实现是不一样的。SurfaceView和View最本质的区别在于,surfaceView是在一个新起的单独线程中可以重新绘制画面而View必须在UI的主线程中更新画面

2、从点击按键开始抓屏到形成正确的png图片,需要一段时间(不同STB性能,不同线程数量,不同时间间隔),不能够达到实时输出png图片的目的

 

二、直接读取/dev/graphics/fb0,并转换成png格式图片

/dev/graphics/fb0是Android平台下的framebuffer设备。对此方法,网络上有很多的相关文档(大部分都是提问):

http://wiseideal.iteye.com/blog/1250175

?
1
2
#adb pull /dev/graphics/fb0 fb0
#ffmpeg -vframes 1 -vcodec rawvideo -f rawvideo -pix_fmt rgb32 -s 1280x720 -i fb0 -f image2 -vcodec png image.png

上面两行只是使用adb.exe以及ffmpeg.exe将fb0(rgb16文件)输出为image.png文件。至于在代码中加入转换格式以及保存文件,可参考上面的链接(Runtime.getRuntime().exec("cat /dev/graphics/fb0"),以及调用busybox相关命令),这些代码有待研究。

对于目前的不同STB,使用上述两行命令后,得出的image.png文件为花屏(页面看不清楚,如同DDMS上抓屏工具得出的图片一样),怀疑是否平台对framebuffer等做过处理。

方案缺点:

1、需要root权限---移植性不高

2、如果需要调用Runtime.getRuntime().exec等相关命令,有可能造成STB死机

3、对于某些盒子,因为硬件或者底层的限制,不能正确得到相关图片(使用Android自带的截屏工具DDMS中Screen Capture都不可以)

 

三、surfaceview -> canvas-〉bitmap

http://topic.youkuaiyun.com/u/20110519/13/37eada57-1645-42da-a195-ccc4108d3282.html

需要深入研究

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值