免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6706 | 回复: 1
打印 上一主题 下一主题

[图形界面] 分享一个把framebuffer当前内容存储成BMP图片的小程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-28 13:01 |只看该作者 |倒序浏览
写操作手册的时候可能会用到,抓出来的图片肯定比拿相机对着显示器照的效果要真实

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <linux/fb.h>
  8. #include <sys/ioctl.h>


  9. typedef unsigned char BYTE;
  10. typedef unsigned short WORD;
  11. typedef unsigned int DWORD;
  12. typedef long LONG;


  13. typedef struct tagBITMAPFILEHEADER {
  14.   WORD  bfType;
  15.   DWORD bfSize;
  16.   WORD  bfReserved1;
  17.   WORD  bfReserved2;
  18.   DWORD bfOffBits;
  19. }__attribute__((packed)) BITMAPFILEHEADER, *PBITMAPFILEHEADER;

  20. typedef struct tagBITMAPINFOHEADER {
  21.   DWORD biSize;
  22.   LONG  biWidth;
  23.   LONG  biHeight;
  24.   WORD  biPlanes;
  25.   WORD  biBitCount;
  26.   DWORD biCompression;
  27.   DWORD biSizeImage;
  28.   LONG  biXPelsPerMeter;
  29.   LONG  biYPelsPerMeter;
  30.   DWORD biClrUsed;
  31.   DWORD biClrImportant;
  32. }__attribute__((packed)) BITMAPINFOHEADER, *PBITMAPINFOHEADER;

  33. typedef struct tagRGBQUAD {
  34.   BYTE rgbBlue;
  35.   BYTE rgbGreen;
  36.   BYTE rgbRed;
  37.   BYTE rgbReserved;
  38. }__attribute__((packed)) RGBQUAD;


  39. #define FRAME_BUFFER_PATH                "/dev/fb0"


  40. int main(int argc, char *argv[])
  41. {  
  42.         int i;
  43.         int img_fd, fb_fd;
  44.         int data_size;
  45.         char *img_buf;
  46.         struct fb_var_screeninfo var_info;
  47.         BITMAPFILEHEADER file_head;
  48.         BITMAPINFOHEADER info_head;
  49.         //RGBQUAD rgb_quad;
  50.        
  51.         if (argc != 2)
  52.         {
  53.                 printf("usage %s bmp_file\n", argv[0]);
  54.                 return -1;
  55.         }       
  56.        
  57.         /*open files*/
  58.         fb_fd = open(FRAME_BUFFER_PATH, O_RDWR);
  59.         if (img_fd < 0)
  60.         {
  61.                 perror("open framebuff");
  62.                 return -1;
  63.         }
  64.         if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &var_info) < 0)
  65.         {
  66.                 perror("ioctl FBIOGET_VSCREENINFO");
  67.                 close(img_fd);
  68.                 return 0;
  69.         }
  70.         printf("xres %d, yres %d\n", var_info.xres, var_info.yres);
  71.        
  72.         img_fd = open(argv[1], O_RDWR | O_CREAT, 0644);
  73.         if (img_fd < 0)
  74.         {
  75.                 perror("open image");
  76.                 close(img_fd);
  77.                 return -1;
  78.         }
  79.        
  80.         data_size = var_info.xres*var_info.yres*(var_info.bits_per_pixel/8);
  81.         /*initialize bmp structs*/
  82.         file_head.bfType = 0x4d42;
  83.         file_head.bfSize = sizeof(file_head) + sizeof(info_head) + data_size;
  84.         file_head.bfReserved1 = file_head.bfReserved2 = 0;
  85.         file_head.bfOffBits = sizeof(file_head) + sizeof(info_head);
  86.        
  87.         info_head.biSize = sizeof(info_head);
  88.         info_head.biWidth = var_info.xres;
  89.         info_head.biHeight = var_info.yres;
  90.         info_head.biPlanes = 1;
  91.         info_head.biBitCount = var_info.bits_per_pixel;
  92.         info_head.biCompression = 0;
  93.         info_head.biSizeImage = 0;
  94.         info_head.biXPelsPerMeter = 0;
  95.         info_head.biYPelsPerMeter = 0;
  96.         info_head.biClrUsed = 0;
  97.         info_head.biClrImportant = 0;
  98.        
  99.         img_buf = (char *)malloc(data_size);
  100.         if (img_buf == NULL)
  101.         {
  102.                 printf("malloc failed!\n");
  103.                 close(fb_fd);
  104.                 close(img_fd);
  105.                 return -1;
  106.         }
  107.   
  108.   /*read img data and */
  109.   read(fb_fd, img_buf, data_size);
  110.   
  111.   write(img_fd, &file_head, sizeof(file_head));
  112.   write(img_fd, &info_head, sizeof(info_head));
  113.   
  114.   /*revese img and write to file*/
  115.   for (i = 0; i < var_info.yres; i++)
  116.   {
  117.           write(img_fd, img_buf + var_info.xres*(var_info.yres-i-1)*(var_info.bits_per_pixel/8),
  118.                   var_info.xres*(var_info.bits_per_pixel/8));
  119.   }
  120.   
  121.   close(fb_fd);
  122.         close(img_fd);
  123.         return 0;
  124. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2013-10-03 18:05 |只看该作者
为什么截图是模糊和错位的?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP