生活随笔
收集整理的這篇文章主要介紹了
framebuffer 保存 bmp图片格式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近需要完成一個從framebuffer中進行讀取,然后將內存的東西保存為bmp圖片格式,我的其他博客內容對framebuffer進行詳細的講解,以及bmp的格式進行詳細的講解。
??? 之前從網上看到了一些保存bmp圖片的代碼,在本地執行都會出現問題,本人就進行了自己編寫,可以指定文件的文件名和文件類型
?? 也可以從github中獲取源碼:
直接看代碼:https://github.com/Guazhen/Framebuffer_shot
[cpp]?view plaincopy
#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>??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;???????DWORD?biWidth;???????DWORD?biHeight;??????WORD?biPlanes;???????WORD?biBitCount;??????DWORD?biCompression;??????DWORD?biSizeImage;??????DWORD?biXPelsPerMeter;??????DWORD?biYPelsPerMeter;??????DWORD?biClrUsed;??????DWORD?biClrImportant;??}?__attribute__((packed))?BITMAPINFOHEADER;????int?output_file;??int?type_file;????struct?fb_fix_screeninfo?finfo;??struct?fb_var_screeninfo?vinfo;????static?const?struct?option?long_options[]=????{?????????{"output",1,NULL,'o'},?????????{"t",1,NULL,'t'},???????????{NULL,0,NULL,0}????};?????static?void?usage(void)????{????????fprintf(stderr,????????????????"imax6?[option]...\n"????????????????"??-o|--output???????????????Output?the?filename.\n"????????????????"??-t|--type???????????????Output?the?type?of?thefilename.\n"??????????????"??-h|--help???????????????help?information.\n"?????????????);????};??????static?void?image_bmp(?const?char?*filename)??{??????printf("starting?bmp..\n");??????char?tmpbufilename[126]?=?{0};??????if(?NULL?!=?filename)??????{??????????strcpy(tmpbufilename,?filename);??????????????????????????strcat(tmpbufilename,".bmp");??????}else??????{??????????strcpy(tmpbufilename,"screen.bmp");??????}????????????FILE?*fp;??????BITMAPFILEHEADER????bmfh;??????????BITMAPINFOHEADER????bmih;????????????((unsigned?char?*)&bmfh.bfType)[0]?=?'B';??????????((unsigned?char?*)&bmfh.bfType)[1]?=?'M';????????????bmfh.bfSize?=??sizeof(BITMAPFILEHEADER)?+?sizeof(BITMAPINFOHEADER)?+?vinfo.yres?*?vinfo.xres?*?4;??????????bmfh.bfReserved1?=?0;??????????bmfh.bfReserved2?=?0;??????????bmfh.bfOffBits?=?sizeof(BITMAPFILEHEADER)?+?sizeof(BITMAPINFOHEADER);????????????bmih.biSize?=?sizeof(BITMAPINFOHEADER);????????????bmih.biWidth?=?vinfo.xres;??????????bmih.biHeight?=?vinfo.yres;??????????bmih.biPlanes?=?1;??????????bmih.biBitCount?=?32;??????????bmih.biCompression?=?0;??????????bmih.biSizeImage?=?0;???????????bmih.biXPelsPerMeter?=?0;???????????bmih.biYPelsPerMeter?=?0;??????????bmih.biClrUsed?=?0;???????????bmih.biClrImportant?=?0;?????????printf("tmpbufilename?=?%s\n",tmpbufilename);????????????FILE*?image_file?=?fopen(tmpbufilename,"a");??????if(?NULL?==?image_file)??????{??????????printf("image?fopen?fail\n");??????}????????????fwrite(&bmfh,?sizeof(BITMAPFILEHEADER),1,image_file);??????????fwrite(&bmih,?sizeof(BITMAPINFOHEADER),1,image_file);????????????FILE?*raw_file?=?fopen(?"test.raw","rb");??????if(?NULL?==?raw_file)?????????{??????????printf("rawfile?fopen?fail..\n");??????}?????????????????????????????????????????int?ch?=?getc(raw_file);????????????????????????????int?x,?y;??????for(?y?=?vinfo.yres?-?1;?y?>=?0;?y--)??????{??????????for(x?=?0;?x?<?vinfo.xres;?x++)??????????{????????????????????????????long?location?=?(x?+?vinfo.xoffset)?*?(vinfo.bits_per_pixel/8)?+?(y?+?vinfo.yoffset)?*?finfo.line_length;??????????????fseek(raw_file,?location,?SEEK_SET);??????????????ch?=?fgetc(raw_file);??????????????fputc(ch,image_file);????????????????ch?=?fgetc(raw_file);??????????????fputc(ch,image_file);????????????????ch?=?fgetc(raw_file);??????????????fputc(ch,image_file);????????????????ch?=?fgetc(raw_file);??????????????fputc(ch,image_file);???????????}??????}????????????????????????????????fp?=?popen("rm?./test.raw","r");??????????pclose(fp);????????fclose(raw_file);??????fclose(image_file);??????printf("ending?bmp\n");??}????int?main(int?argc,?char?**argv)??{??????int?opt?=?0;??????int?options_index?=?0;??????char?*tmp?=?NULL;??????char?filename[126]?=?{0};??????char?type[126]?=?{0};????????if(argc?==?1)??????{??????????usage();??????????return?2;?????????}????????????while((opt=getopt_long(argc,argv,"o:t:h?",long_options,&options_index))!=EOF?)????????{????????????switch(opt)????????????{??????????????????case?'o':???????????????????output_file=1;??????????????????sprintf(filename,"%s",optarg);??????????????????break;????????????????case?'t':???????????????????type_file=1;??????????????????sprintf(type,"%s",optarg);??????????????????break;?????????????????case?'h':????????????????case?'?':???????????????????usage();return?2;break;????????????}????????}??????????????int?fb_fd?=?open("/dev/fb0",O_RDWR);??????if(fb_fd?==?-1)??????{??????????printf("open?fail..\n");??????????return?-1;??????}??????????ioctl(fb_fd,?FBIOGET_VSCREENINFO,?&vinfo);??????ioctl(fb_fd,?FBIOGET_FSCREENINFO,?&finfo);????????long?screensize?=?vinfo.yres_virtual?*?finfo.line_length;??????FILE?*fp?=?popen("bash?./test.sh","r");???????pclose(fp);??????????????if(?output_file?&&?type_file?)??????{??????????if(?!strcmp(type,"bmp"))??????????????{??????????????image_bmp(filename);??????????}else?if(?!strcmp(type,"png"))??????????{??????????????printf("png?type?=?%s\n",type);??????????}??????????else??????????{??????????????printf("unkown\n");??????????}????????????}?????????else??????{??????????usage();return?2;??????}????????close(fb_fd);????????return?0;??} ?
[cpp]?view plaincopy
#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>??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;???????DWORD?biWidth;???????DWORD?biHeight;??????WORD?biPlanes;???????WORD?biBitCount;??????DWORD?biCompression;??????DWORD?biSizeImage;??????DWORD?biXPelsPerMeter;??????DWORD?biYPelsPerMeter;??????DWORD?biClrUsed;??????DWORD?biClrImportant;??}?__attribute__((packed))?BITMAPINFOHEADER;????int?output_file;??int?type_file;????struct?fb_fix_screeninfo?finfo;??struct?fb_var_screeninfo?vinfo;????static?const?struct?option?long_options[]=????{?????????{"output",1,NULL,'o'},?????????{"t",1,NULL,'t'},???????????{NULL,0,NULL,0}????};?????static?void?usage(void)????{????????fprintf(stderr,????????????????"imax6?[option]...\n"????????????????"??-o|--output???????????????Output?the?filename.\n"????????????????"??-t|--type???????????????Output?the?type?of?thefilename.\n"??????????????"??-h|--help???????????????help?information.\n"?????????????);????};??????static?void?image_bmp(?const?char?*filename)??{??????printf("starting?bmp..\n");??????char?tmpbufilename[126]?=?{0};??????if(?NULL?!=?filename)??????{??????????strcpy(tmpbufilename,?filename);??????????????????????????strcat(tmpbufilename,".bmp");??????}else??????{??????????strcpy(tmpbufilename,"screen.bmp");??????}????????????FILE?*fp;??????BITMAPFILEHEADER????bmfh;??????????BITMAPINFOHEADER????bmih;????????????((unsigned?char?*)&bmfh.bfType)[0]?=?'B';??????????((unsigned?char?*)&bmfh.bfType)[1]?=?'M';????????????bmfh.bfSize?=??sizeof(BITMAPFILEHEADER)?+?sizeof(BITMAPINFOHEADER)?+?vinfo.yres?*?vinfo.xres?*?4;??????????bmfh.bfReserved1?=?0;??????????bmfh.bfReserved2?=?0;??????????bmfh.bfOffBits?=?sizeof(BITMAPFILEHEADER)?+?sizeof(BITMAPINFOHEADER);????????????bmih.biSize?=?sizeof(BITMAPINFOHEADER);????????????bmih.biWidth?=?vinfo.xres;??????????bmih.biHeight?=?vinfo.yres;??????????bmih.biPlanes?=?1;??????????bmih.biBitCount?=?32;??????????bmih.biCompression?=?0;??????????bmih.biSizeImage?=?0;???????????bmih.biXPelsPerMeter?=?0;???????????bmih.biYPelsPerMeter?=?0;??????????bmih.biClrUsed?=?0;???????????bmih.biClrImportant?=?0;?????????printf("tmpbufilename?=?%s\n",tmpbufilename);????????????FILE*?image_file?=?fopen(tmpbufilename,"a");??????if(?NULL?==?image_file)??????{??????????printf("image?fopen?fail\n");??????}????????????fwrite(&bmfh,?sizeof(BITMAPFILEHEADER),1,image_file);??????????fwrite(&bmih,?sizeof(BITMAPINFOHEADER),1,image_file);????????????FILE?*raw_file?=?fopen(?"test.raw","rb");??????if(?NULL?==?raw_file)?????????{??????????printf("rawfile?fopen?fail..\n");??????}?????????????????????????????????????????int?ch?=?getc(raw_file);????????????????????????????int?x,?y;??????for(?y?=?vinfo.yres?-?1;?y?>=?0;?y--)??????{??????????for(x?=?0;?x?<?vinfo.xres;?x++)??????????{????????????????????????????long?location?=?(x?+?vinfo.xoffset)?*?(vinfo.bits_per_pixel/8)?+?(y?+?vinfo.yoffset)?*?finfo.line_length;??????????????fseek(raw_file,?location,?SEEK_SET);??????????????ch?=?fgetc(raw_file);??????????????fputc(ch,image_file);????????????????ch?=?fgetc(raw_file);??????????????fputc(ch,image_file);????????????????ch?=?fgetc(raw_file);??????????????fputc(ch,image_file);????????????????ch?=?fgetc(raw_file);??????????????fputc(ch,image_file);???????????}??????}????????????????????????????????fp?=?popen("rm?./test.raw","r");??????????pclose(fp);????????fclose(raw_file);??????fclose(image_file);??????printf("ending?bmp\n");??}????int?main(int?argc,?char?**argv)??{??????int?opt?=?0;??????int?options_index?=?0;??????char?*tmp?=?NULL;??????char?filename[126]?=?{0};??????char?type[126]?=?{0};????????if(argc?==?1)??????{??????????usage();??????????return?2;?????????}????????????while((opt=getopt_long(argc,argv,"o:t:h?",long_options,&options_index))!=EOF?)????????{????????????switch(opt)????????????{??????????????????case?'o':???????????????????output_file=1;??????????????????sprintf(filename,"%s",optarg);??????????????????break;????????????????case?'t':???????????????????type_file=1;??????????????????sprintf(type,"%s",optarg);??????????????????break;?????????????????case?'h':????????????????case?'?':???????????????????usage();return?2;break;????????????}????????}??????????????int?fb_fd?=?open("/dev/fb0",O_RDWR);??????if(fb_fd?==?-1)??????{??????????printf("open?fail..\n");??????????return?-1;??????}??????????ioctl(fb_fd,?FBIOGET_VSCREENINFO,?&vinfo);??????ioctl(fb_fd,?FBIOGET_FSCREENINFO,?&finfo);????????long?screensize?=?vinfo.yres_virtual?*?finfo.line_length;??????FILE?*fp?=?popen("bash?./test.sh","r");???????pclose(fp);??????????????if(?output_file?&&?type_file?)??????{??????????if(?!strcmp(type,"bmp"))??????????????{??????????????image_bmp(filename);??????????}else?if(?!strcmp(type,"png"))??????????{??????????????printf("png?type?=?%s\n",type);??????????}??????????else??????????{??????????????printf("unkown\n");??????????}????????????}?????????else??????{??????????usage();return?2;??????}????????close(fb_fd);????????return?0;??} ?
總結
以上是生活随笔為你收集整理的framebuffer 保存 bmp图片格式的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。