遇到一个需求,在读取/dev/fb0的原始数据并保存为bmp,png图片,并通过命令行格式进行保存.
写的不好,大家可以指点出来,一起讨论.
也将代码上传到github上了,地址:
https://github.com/Guazhen/Framebuffer_shot/blob/master/framework_shot.c
root@imx6qsabresd:~# ./imx6_shot -h
usage: ./imx6_shot [-hpb] [FILENAME]
imx6 [option]...
-p|--png Save the file as a png.
-b|--bmp Save the file as a bmp
-h|--help help information.
If FILENAME ends with .png it will be saved as a png.
If FILENAME ends with .bmp it will be saved as a bmp.
If FILENAME is not given, the default will be saved as a png.
实现代码如下:
#include <linux/fb.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include <strings.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/vt.h>
#include <png.h>
#include <zlib.h>
static int Blue = 0;
static int Green = 1;
static int Red = 2;
static int Alpha = 3;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef unsigned long LONG;
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} __attribute__((packed)) BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
DWORD biSize; /*info header size in bytes*/
DWORD biWidth; /*widht of image*/
DWORD biHeight;/*height of image*/
WORD biPlanes;/*number of colour planes*/
WORD biBitCount;/*bits per pixel*/
DWORD biCompression;/*compression type*/
DWORD biSizeImage;/*image size meter*/
DWORD biXPelsPerMeter;/*pixels per meter*/
DWORD biYPelsPerMeter;/*pexels per meter*/
DWORD biClrUsed;/*number of colour*/
DWORD biClrImportant;/*important colour*/
} __attribute__((packed)) BITMAPINFOHEADER;
int png_file;
int bmp