Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1212296
  • 博文数量: 261
  • 博客积分: 4196
  • 博客等级: 上校
  • 技术积分: 3410
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-17 17:05
文章分类

全部博文(261)

文章存档

2018年(1)

2017年(22)

2016年(2)

2015年(8)

2014年(27)

2013年(40)

2012年(161)

分类: LINUX

2013-06-28 11:34:51

保存屏幕信息:dd if=/dev/fb0 of=fbfile  或: cp /dev/fb0 fbfile

恢复屏幕信息:dd if=fbfile of=/dev/fb0  或: cat fbfile > /dev/fb0


原文地址:http://blog.chinaunix.net/uid-2282111-id-2113342.html

最近要写有关于QT的文档了,需要大量截图,由于目前QT是在2410开发平台上跑,在PC下懒得再编译QT的lib出来,所以如何将2410开发板上的LCD的图像截下来成了一个不大不小的问题。

反正以后可能会经常用到,索性写一个可以在2410开发板,Linxu平台下,截取LCD屏幕内容的小程序,呵呵~~

 

程 序主要是利用/dev/fb/0这个设备节点。/dev/fb/0代表了LCD的FrameBuffer缓冲区驱动程序,操作该驱动,即相当于操作LCD 的FrameBuffer。所以,由此出发,通过读取/dev/fb/0的内容,可以很容易就得到LCD屏幕上当前正在显示的内容。

由于2410的FrameBuffer是16bit的RGB565格式,所以在利用/dev/fb/0拿到屏幕图像的原始数据后,还需要做一个小小的转换,将其变为普通的bmp文件的形式。

在2410平台下,针对640*480的屏幕,代码如下:

#include
#include
#include

static unsigned char sg_BHeader[] = {
    0x42, 0x4D, 0x36, 0x58, 0x02, 0x00, 0x00, 0x00,  0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
    0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0xF0, 0x00,  0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#define RGB565TO1555(rgb) ((unsigned short)((unsigned short)(rgb & 0x001f) | ((unsigned short)(rgb & 0xffe0) >> 1)))

void SaveBMPFile(unsigned char *raw, char *filename)
{
    unsigned short *p = (unsigned short *)raw;
    typedef unsigned int UINT;
    typedef unsigned char UCHAR;
    UINT m_Width = 640, m_Height = 480;
    UINT i, j;
    int bmp = open(filename, O_WRONLY | O_CREAT);
    if(bmp < 0)
        return;
    sg_BHeader[0x02] = (UCHAR)(m_Width * m_Height * 2 + 0x36) & 0xff;
    sg_BHeader[0x03] = (UCHAR)((m_Width * m_Height * 2 + 0x36) >> 8) & 0xff;
    sg_BHeader[0x04] = (UCHAR)((m_Width * m_Height * 2 + 0x36) >> 16) & 0xff;
    sg_BHeader[0x05] = (UCHAR)((m_Width * m_Height * 2 + 0x36) >> 24) & 0xff;
    sg_BHeader[0x12] = (UCHAR)m_Width & 0xff;
    sg_BHeader[0x13] = (UCHAR)(m_Width >> 8) & 0xff;
    sg_BHeader[0x14] = (UCHAR)(m_Width >> 16) & 0xff;
    sg_BHeader[0x15] = (UCHAR)(m_Width >> 24) & 0xff;
    sg_BHeader[0x16] = (UCHAR)m_Height & 0xff;
    sg_BHeader[0x17] = (UCHAR)(m_Height >> 8) & 0xff;
    sg_BHeader[0x18] = (UCHAR)(m_Height >> 16) & 0xff;
    sg_BHeader[0x19] = (UCHAR)(m_Height >> 24) & 0xff;
    write(bmp, sg_BHeader, sizeof(sg_BHeader));
    for(i = 0; i < m_Height; i++)
    {
        unsigned short *c = p + (m_Height - 1 - i) * m_Width;
        unsigned short cc;
        for(j = 0; j < m_Width; j++)
        {
            cc = RGB565TO1555(*(c + j));
//            cc = *(c + j);
            write(bmp, &cc, 2);
        }
    }
    close(bmp);
}

int main(int argc, char *argv[])
{
    unsigned char buf[640*480*2];
    char *filename = "/mnt/yaffs/screenshot.bmp";
    int fb;
    fb = open("/dev/fb0", O_RDONLY);
    if(fb < 0)
        exit(1);
    if(argc == 2)
        filename = argv[1];
    printf("reading screen...\n");
    read(fb, buf, 640*480*2);
    close(fb);
    printf("saving screen...\n");
    SaveBMPFile(buf, filename);
    printf("file %s created successfully\n", filename);
    exit(0);
}

阅读(3225) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~