FrameBuffer的使用

本文介绍了如何使用Linux下的帧缓冲区接口(/dev/fb0)进行屏幕截图、打印及显示设备参数的操作。通过示例代码展示了打开帧缓冲设备、获取固定和可变屏幕信息结构、读取屏幕分辨率等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:查找驱动

 ls -l /dev/fb0
crw-rw---- 1 root video 29, 0 Jul  7 16:27 /dev/fb0

如果没有这个文件也可以找找其他的比如:/dev/fb1,/dev/fb2...如果找不到这些文件,那就得重新编译内核了。

第二步:打印到控制台

sudo cat /dev/fb0 > sreensnap  

ls -l sreensnap 

-rw------- 1 suri suri 7372800 Jul  7 16:53 screensnap

注:清除屏幕命令:clear

第三步:运行下面程序查看设备参数(如果分辨率不是1024*768,请看下一篇:修改分辨率)

#include <unistd.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <sys/stat.h>
  5 #include <fcntl.h>
  6 #include <linux/fb.h>
  7 #include <sys/mman.h>
  8 #include <sys/ioctl.h>
  9
 10 int main()
 11 {
 12         int fp=0;
 13         struct fb_var_screeninfo vinfo;
 14         struct fb_fix_screeninfo finfo;
 15
 16         fp = open("/dev/fb0", O_RDWR);
 17         if (fp < 0){
 18                 printf("Error: Can not open framebuffer device\n");
 19                 exit(1);
 20         }
 21
 22         if (ioctl(fp, FBIOGET_FSCREENINFO, &finfo)){
 23                 printf("Error reading variable information\n");
 24                 exit(2);
 25         }
 26
 27         if (ioctl(fp, FBIOGET_VSCREENINFO, &vinfo)){
 28                 printf("Error reading variable information\n");
 29                 exit(3);
 30         }
 31
 32         printf("The men is: %d\n", finfo.smem_len);
 33         printf("The line_length is: %d\n", finfo.line_length);
 34         printf("The xres is: %d\n", vinfo.xres);
 35         printf("The yres is: %d\n", vinfo.yres);
 36         printf("bits_per_pixel is: %d\n", vinfo.bits_per_pixel);
 37         close(fp);
 38
 39         return 0;
 40 }
把上面的程序编译以后运行,在我的机器(lenovo Y450,ubuntu 12.10)上的结果如下:
The men is: 7372800
The line_length is: 4096
The xres is: 1024
The yres is: 768
bits_per_pixel is: 32
u32 smem_len 是这个/dev/fb0的大小,也就是内存大小。
__u32 line_length 是屏幕上一行的点在内存中占有的空间,不是一行上的点数。
在fb_var_screeninfo 中有
__u32 xres ,__u32 yres 是x和y方向的分辨率,就是两个方向上的点数。
__u32 bits_per_pixel 是每一点占有的内存空间。
注:打印一点的程序

  1 #include <unistd.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <fcntl.h>
  5 #include <sys/stat.h>
  6 #include <linux/fb.h>
  7 #include <sys/mman.h>
  8 #include <sys/ioctl.h>
  9
 10 int main(void)
 11 {
 12         int fp=0;
 13         struct fb_var_screeninfo vinfo;
 14         struct fb_fix_screeninfo finfo;
 15         long screensize=0;
 16         char *fbp=0;
 17         int x=0, y=0;
 18         long location=0;
 19
 20         fp = open("/dev/fb0", O_RDWR);
 21         if (fp < 0){
 22                 printf("Error: Can not open framebuffer device\n");
 23                 exit(1);
 24         }
 25
 26         if (ioctl(fp, FBIOGET_FSCREENINFO, &finfo)){
 27                 printf("Error reading fixed information'\n");
 28                 exit(2);
 29         }
 30
 31         if (ioctl(fp, FBIOGET_VSCREENINFO, &vinfo)){
 32                 printf("Error reading variable information\n");
 33                 exit(3);
 34         }
 35
 36         screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
 37         /*这就是把fp所指的文件中从开始到screensize大小的内容给映射出来,得到一个指向这块空间的指针*/
 38         fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fp, 0);
 39         if ((int)fbp == -1){
 40                 printf("Error: failed to map framebuffer device to memory./n");
 41                 exit(4);
 42         }
 43         /*这是你想画的点的位置坐标,(0,0)点在屏幕左上角*/
 44         x = 100;
 45         y = 100;
 46         location = x * (vinfo.bits_per_pixel / 8) + y * finfo.line_length;
 47
 48         /*直接赋值来改变屏幕上某点的颜色*/
 49         *(fbp + location) = 15;           /* 蓝色的色深 */
 50         *(fbp + location + 1) = 255; /* 绿色的色深*/
 51         *(fbp + location + 2) = 20;   /* 绿色的色深*/
 52         *(fbp + location + 3) = 1;     /* 是否透明*/
 53         munmap(fbp, screensize);  /* 解除映射 */
 54         close(fp);
 55
 56         return 0;
 57 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值