android上用C语言读取fb0实现截屏,并保存为rgb565的bmp_第1页
android上用C语言读取fb0实现截屏,并保存为rgb565的bmp_第2页
android上用C语言读取fb0实现截屏,并保存为rgb565的bmp_第3页
android上用C语言读取fb0实现截屏,并保存为rgb565的bmp_第4页
android上用C语言读取fb0实现截屏,并保存为rgb565的bmp_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、android上用C语言读取fb0实现截屏,并保存为rgb565的bmp工程源码:      android上用C语言读取fb0实现截屏,保存为bmp图片,支持16位(rgb565)、24位(rbg888)两种格式数据,并在android2.2和4.0模拟器上验证通过。截屏实现主要有两个方面的工作,读取屏幕数据和生成图片。1.读取屏幕数据只读方式打开显存设备 /dev/graphics/fb0,再通过mmap用共享方式(MAP_SHARED)映射到一片内存中,其他地方就可以直接以内存的方式读取屏幕数据了。要注意的是,fb0的虚拟显示设备尺寸,大于实际屏幕

2、显示设备的尺寸,mmap映射时需将安装虚拟尺寸进行映射,否则,截屏可能失败。针对显存设备的主要代码:java view plaincopy1. /* 2.     created:    2012/02/07 3.     filename:   myfb.c 4.     author:      5. &

3、#160;    6.     purpose:     7. */  8. #ifndef WIN32  9. /-  10.   11. #include <stdio.h>  12. #include <stdlib.h>  13. #include <unistd.h&g

4、t;  14. #include <fcntl.h>  15. #include <sys/mman.h>  16. #include <sys/stat.h>  17. #include <sys/types.h>  18.   19. #include <linux/fb.h>  20. #include <linux/kd.h

5、>  21.   22. struct FB   23.     unsigned short *bits;  24.     unsigned size;  25.     int fd;  26.     struct fb_fix_scr

6、eeninfo fi;  27.     struct fb_var_screeninfo vi;  28. ;  29.   30. int fb_bpp(struct FB *fb)  31.   32.     if (fb)   33.     

7、60;   return fb->vi.bits_per_pixel;  34.       35.     return 0;  36.   37.   38. int fb_width(struct FB *fb)  39.   40.     if

8、 (fb)   41.         return fb->vi.xres;  42.       43.     return 0;  44.   45.   46. int fb_height(struct FB *fb) &#

9、160;47.   48.     if (fb)   49.         return fb->vi.yres;  50.       51.     return 0;  52.   53.   54. i

10、nt fb_size(struct FB *fb)  55.   56.     if (fb)   57.         unsigned bytespp = fb->vi.bits_per_pixel / 8;  58.      

11、60;  return (fb->vi.xres * fb->vi.yres * bytespp);  59.       60.     return 0;  61.   62.   63. int fb_virtual_size(struct FB *fb)  64. &#

12、160; 65.     if (fb)   66.         unsigned bytespp = fb->vi.bits_per_pixel / 8;  67.         return (fb->vi.xres_virtual *&

13、#160;fb->vi.yres_virtual * bytespp);  68.       69.     return 0;  70.   71.   72. void * fb_bits(struct FB *fb)  73.   74.     unsi

14、gned short * bits = NULL;  75.     if (fb)   76.         int offset, bytespp;  77.         bytespp = fb->vi.bits

15、_per_pixel / 8;  78.   79.         /* HACK: for several of our 3d cores a specific alignment 80.         * is required s

16、o the start of the fb may not be an integer number of lines 81.         * from the base.  As a result we are storing the additiona

17、l offset in 82.         * xoffset. This is not the correct usage for xoffset, it should be added 83.         * to each l

18、ine, not just once at the beginning */  84.         offset = fb->vi.xoffset * bytespp;  85.         offset += fb->vi.xres *&

19、#160;fb->vi.yoffset * bytespp;  86.         bits = fb->bits + offset / sizeof(*fb->bits);  87.       88.     return bits;  89.

20、   90.   91. void fb_update(struct FB *fb)  92.   93.     if (fb)   94.         fb->vi.yoffset = 1;  95.       &

21、#160; ioctl(fb->fd, FBIOPUT_VSCREENINFO, &fb->vi);  96.         fb->vi.yoffset = 0;  97.         ioctl(fb->fd, FBIOPUT_VSCREENINFO, &fb->vi);&

22、#160; 98.       99.   100.   101. static int fb_open(struct FB *fb)  102.   103.     if (NULL = fb)   104.         

23、return -1;  105.       106.       107.     fb->fd = open("/dev/graphics/fb0", O_RDONLY);  108.     if (fb->fd < 0)  

24、 109.         printf("open("/dev/graphics/fb0") failed!n");  110.         return -1;  111.       112.   113.   

25、0; if (ioctl(fb->fd, FBIOGET_FSCREENINFO, &fb->fi) < 0)   114.         printf("FBIOGET_FSCREENINFO failed!n");  115.         goto fa

26、il;  116.       117.     if (ioctl(fb->fd, FBIOGET_VSCREENINFO, &fb->vi) < 0)   118.         printf("FBIOGET_VSCREENINFO failed!n");

27、  119.         goto fail;  120.       121.   122.     fb->bits = mmap(0, fb_virtual_size(fb), PROT_READ, MAP_SHARED, fb->fd, 0); 

28、 123.     if (fb->bits = MAP_FAILED)   124.         printf("mmap() failed!n");  125.         goto fail;  126.   

29、    127.   128.     return 0;  129.   130. fail:  131.     close(fb->fd);  132.     return -1;  133.   134.   135. static 

30、void fb_close(struct FB *fb)  136.   137.     if (fb)   138.         munmap(fb->bits, fb_virtual_size(fb);  139.         close(

31、fb->fd);  140.       141.   142.   143. static struct FB g_fb;  144. struct FB * fb_create(void)  145.   146.     memset(&g_fb, 0, sizeof(struc

32、t FB);  147.     if (fb_open(&g_fb)   148.         return NULL;  149.       150.     return &g_fb;  151.   

33、152.   153. void fb_destory(struct FB *fb)  154.   155.     fb_close(fb);  156.   157.   158. /-  159. #endif/#ifndef WIN32  2.生成图片这里生成的图片是bmp格式的,可以根据设备像素的位数自动生成16位(rgb565)、24位(rbg

34、888)两种图片。主要工作是要正确填充bmp的文件头信息,24位(rbg888)较简单。16位(rgb565)复杂一点,biCompression成员的值必须是BI_BITFIELDS,原来调色板的前三个位置被三个DWORD变量占据,称为红、绿、蓝掩码,在565格式下,它们则分别为:0xF800、0x07E0、0x001F。另外,需要主要的是windows要求文件大小必须是4的倍数,文件大小需要做下面的处理才能正确显示。java view plaincopy1. head->bfSize = head->bfOffBits + si

35、ze;  2. head->bfSize = (head->bfSize + 3) & 3;  3. size = head->bfSize - head->bfOffBits;  生成图片的主要代码:java view plaincopy1. /* 2.     created:    2012/02/0

36、7 3.     filename:   savebmp.c 4.     author:      5.      6.     purpose:     7. */  8.   9. #include <std

37、lib.h>  10. #include <stdio.h>  11. #include <memory.h>  12.   13.   14. /-  15. /* 16. 位图文件的组成 17. 结构名称 符 号 18. 位图文件头 (bitmap-file header) BITMAPFILEHEADER bmfh 19.

38、 位图信息头 (bitmap-information header) BITMAPINFOHEADER bmih 20. 彩色表(color table) RGBQUAD aColors 21. 图象数据阵列字节 BYTE aBitmapBits 22. */  23. typedef struct bmp_header   24.   25.    

39、60;short twobyte           /两个字节,用来保证下面成员紧凑排列,这两个字符不能写到文件中  26.     /14B  27.     char bfType2          /!文件的类型,该值必需是0x4D42,也

40、就是字符'BM'  28.     unsigned int bfSize     /!说明文件的大小,用字节为单位  29.     unsigned int bfReserved1;/保留,必须设置为0  30.     unsigned int bfOffBits  

41、/!说明从文件头开始到实际的图象数据之间的字节的偏移量,这里为14B+sizeof(BMPINFO)  31. BMPHEADER;  32.   33. typedef struct bmp_info  34.   35.     /40B  36.     unsigned int biSize     

42、    /!BMPINFO结构所需要的字数  37.     int biWidth                 /!图象的宽度,以象素为单位  38.     int biHeight     

43、           /!图象的宽度,以象素为单位,如果该值是正数,说明图像是倒向的,如果该值是负数,则是正向的  39.     unsigned short biPlanes     /!目标设备说明位面数,其值将总是被设为1  40.     unsigned short 

44、biBitCount   /!比特数/象素,其值为1、4、8、16、24、或32  41.     unsigned int biCompression  /说明图象数据压缩的类型  42. #define BI_RGB        0L    /没有压缩  43. #define BI

45、_RLE8       1L    /每个象素8比特的RLE压缩编码,压缩格式由2字节组成(重复象素计数和颜色索引);  44. #define BI_RLE4       2L    /每个象素4比特的RLE压缩编码,压缩格式由2字节组成  45. #define BI_BITFIELDS  3L

46、    /每个象素的比特由指定的掩码决定。  46.     unsigned int biSizeImage    /图象的大小,以字节为单位。当用BI_RGB格式时,可设置为0  47.     int biXPelsPerMeter         /水平分辨率,用象素/米

47、表示  48.     int biYPelsPerMeter         /垂直分辨率,用象素/米表示  49.     unsigned int biClrUsed      /位图实际使用的彩色表中的颜色索引数(设为0的话,则说明使用所有调色板项)  50.  

48、;   unsigned int biClrImportant /对图象显示有重要影响的颜色索引的数目,如果是0,表示都重要。  51. BMPINFO;  52.   53. typedef struct tagRGBQUAD   54.     unsigned char rgbBlue;  55.    

49、0;unsigned char rgbGreen;  56.     unsigned char rgbRed;  57.     unsigned char rgbReserved;  58.  RGBQUAD;  59.   60. typedef struct tagBITMAPINFO   

50、61.     BMPINFO    bmiHeader;  62.     /RGBQUAD    bmiColors1;  63.     unsigned int rgb3;  64.  BITMAPINFO;  65.   66. static int

51、 get_rgb888_header(int w, int h, BMPHEADER * head, BMPINFO * info)  67.   68.     int size = 0;  69.     if (head && info)   70

52、.         size = w * h * 3;  71.         memset(head, 0, sizeof(* head);  72.         memset(info, 0, siz

53、eof(* info);  73.         head->bfType0 = 'B'  74.         head->bfType1 = 'M'  75.         head->bf

54、OffBits = 14 + sizeof(* info);  76.         head->bfSize = head->bfOffBits + size;  77.         head->bfSize = (head->bfSize +&#

55、160;3) & 3;/windows要求文件大小必须是4的倍数  78.         size = head->bfSize - head->bfOffBits;  79.           80.        

56、0;info->biSize = sizeof(BMPINFO);  81.         info->biWidth = w;  82.         info->biHeight = -h;  83.        

57、0;info->biPlanes = 1;  84.         info->biBitCount = 24;  85.         info->biCompression = BI_RGB;  86.        

58、60;info->biSizeImage = size;  87.   88.         printf("rgb888:%dbit,%d*%d,%dn", info->biBitCount, w, h, head->bfSize);  89.       90.   

59、0; return size;  91.   92.   93. static int get_rgb565_header(int w, int h, BMPHEADER * head, BITMAPINFO * info)  94.   95.     int size = 0; 

60、60;96.     if (head && info)   97.         size = w * h * 2;  98.         memset(head, 0, sizeof(* head);&#

61、160; 99.         memset(info, 0, sizeof(* info);  100.         head->bfType0 = 'B'  101.         head->bfType1 

62、= 'M'  102.         head->bfOffBits = 14 + sizeof(* info);  103.         head->bfSize = head->bfOffBits + size;  104.  

63、;       head->bfSize = (head->bfSize + 3) & 3;  105.         size = head->bfSize - head->bfOffBits;  106.      

64、60;    107.         info->bmiHeader.biSize = sizeof(info->bmiHeader);  108.         info->bmiHeader.biWidth = w;  109.      &#

65、160;  info->bmiHeader.biHeight = -h;  110.         info->bmiHeader.biPlanes = 1;  111.         info->bmiHeader.biBitCount = 16;  112.  &

66、#160;      info->bmiHeader.biCompression = BI_BITFIELDS;  113.         info->bmiHeader.biSizeImage = size;  114.   115.         info-&

67、gt;rgb0 = 0xF800;  116.         info->rgb1 = 0x07E0;  117.         info->rgb2 = 0x001F;  118.   119.        &

68、#160;printf("rgb565:%dbit,%d*%d,%dn", info->bmiHeader.biBitCount, w, h, head->bfSize);  120.       121.     return size;  122.   123.   124. static int save_bm

69、p_rgb565(FILE * hfile, int w, int h, void * pdata)  125.   126.     int success = 0;  127.     int size = 0;  128.     BMPHEA

70、DER head;  129.     BITMAPINFO info;  130.       131.     size = get_rgb565_header(w, h, &head, &info);  132.     if (size >

71、 0)   133.         fwrite(head.bfType, 1, 14, hfile);  134.         fwrite(&info, 1, sizeof(info), hfile);  135.      

72、60;  fwrite(pdata, 1, size, hfile);  136.         success = 1;  137.       138.   139.     return success;  140.   141.

73、  142. static int save_bmp_rgb888(FILE * hfile, int w, int h, void * pdata)  143.   144.     int success = 0;  145.     int size = 0;&#

74、160; 146.     BMPHEADER head;  147.     BMPINFO info;  148.       149.     size = get_rgb888_header(w, h, &head, &info);  150.  

75、;   if (size > 0)   151.         fwrite(head.bfType, 1, 14, hfile);  152.         fwrite(&info, 1, sizeof(info), hfile); &#

76、160;153.         fwrite(pdata, 1, size, hfile);  154.         success = 1;  155.       156.       157.   

77、  return success;  158.   159.   160. int save_bmp(const char * path, int w, int h, void * pdata, int bpp)  161.   162.     int success =

78、60;0;  163.     FILE * hfile = NULL;  164.   165.     do   166.       167.         if (path = NULL | 

79、;w <= 0 | h <= 0 | pdata = NULL)   168.             printf("if (path = NULL | w <= 0 | h <= 0 |

80、60;pdata = NULL)n");  169.             break;  170.           171.   172.         remove(path); 

81、0;173.         hfile = fopen(path, "wb");  174.         if (hfile = NULL)   175.             pr

82、intf("open(%s) failed!n", path);  176.             break;  177.           178.   179.         switch

83、 (bpp)  180.           181.         case 16:  182.             success = save_bmp_rgb565(hfile, w, h

84、, pdata);  183.             break;  184.         case 24:  185.             success = save

85、_bmp_rgb888(hfile, w, h, pdata);  186.             break;  187.         default:  188.             printf("error: not support format!n");  189.    

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论