如何从Linux帧缓冲区获取RGB像素值? [英] How to get RGB pixel values from Linux framebuffer?

查看:295
本文介绍了如何从Linux帧缓冲区获取RGB像素值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Linux以最有效的方式获取屏幕像素的RGB值.因此,我决定使用C( fb.h )中的帧缓冲库来访问帧缓冲设备(/dev/fb0 )并直接从中读取.

I want to get RGB values of screen pixels in the most efficient way, using Linux. So I decided to use the framebuffer library in C (fb.h) to access the framebuffer device (/dev/fb0) and read from it directly.

这是代码:

#include <stdint.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

int main() {

    int fb_fd;
    struct fb_fix_screeninfo finfo;
    struct fb_var_screeninfo vinfo;
    uint8_t *fb_p;

    /* Open the frame buffer device */
    fb_fd = open("/dev/fb0", O_RDWR);
    if (fb_fd < 0) {
        perror("Can't open /dev/fb0\n");
        exit(EXIT_FAILURE);
    }

    /* Get fixed info */
    if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo) < 0) {
        perror("Can't get fixed info\n");
        exit(EXIT_FAILURE);
    }

    /* Get variable info */
    if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &vinfo) < 0) {
        perror("Can't get variable info\n");
        exit(EXIT_FAILURE);
    }

    /* To access to the memory, it can be mapped*/
    fb_p = (uint8_t *) mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0);
    if (fb_p == MAP_FAILED) {
        perror("Can't map memory\n");
        exit(EXIT_FAILURE);
    }

    /* Print each byte of the frame buffer */
    for (int i = 0; i < finfo.smem_len; i++) {
        printf("%d\n", *(fb_p + i));

        // for (int j = 0; j < 500000000; j++);       /* Delay */
    }

    munmap(fb_p, 0);
        close(fb_fd);

    return 0;
}

但是当我打印值时,我没有得到我所期望的...

But when I print the values, I don't get what I was expecting...

如果我使用 grabc 之类的工具选择像素(0,0)的RGB值,则会得到以下信息:

If I pick the RGB value of the pixel (0, 0) with a tool like grabc, I get this:

#85377e
133,55,126

但是带有我的代码的第一批印刷品是:

But the first printings with my code are:

126
145
198
...

看起来我已经很好地获得了第一个像素的第一个值,对应于蓝色,但其余的是错误的.

It looks like I am obtaining well the first value of the first pixel, corresponding to blue, but the rest is wrong.

推荐答案

在一些使用fbdev的罕见越野车中,您可能需要将mmap返回的指针对准下一个

In some rare buggy device using fbdev you may need to align the mmap returned pointer to the next system page in order to get the first pixel correctly, and by doing so you may also need to mmap one more page.

我不认为这是您的情况,似乎您正在尝试使用linux fbdev获取xserver桌面像素,除非您的xserver配置为使用fbdev驱动程序(即使如此,我不确定工作)将无法工作. 如果要读取桌面像素是您的目标,则应使用一些屏幕截图工具.

I don't think that's your case, it seems like you're trying to get the xserver desktop pixels by using linux fbdev, unless your xserver is configured to use the fbdev driver (and even so I'm not sure would work) isn't gonna work. If reading the desktop pixels is your target you should look into some screenshot tool.

此外,fbdev驱动程序通常是非常基本的驱动程序,它是低级别的访问,但映射和读取像素没有任何效率,Xlib或任何更高级别的图形系统可能会知道并支持您的图形卡加速,并且效率更高(也许使用DMA将整个帧缓冲区读取到系统内存中而不加载cpu).

Beside, fbdev drivers are usually really basic, is a low-level access but there's nothing efficient about mmapping and reading pixels, the Xlib or any higher level graphic system may be aware and support your graphic card accelerations and be far more efficient (perhaps using DMA to read the whole framebuffer to your system memory without loading the cpu).

这篇关于如何从Linux帧缓冲区获取RGB像素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆