1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > framebuffer 保存 bmp图片格式

framebuffer 保存 bmp图片格式

时间:2021-05-05 15:50:37

相关推荐

framebuffer 保存 bmp图片格式

最近需要完成一个从framebuffer中进行读取,然后将内存的东西保存为bmp图片格式,我的其他博客内容对framebuffer进行详细的讲解,以及bmp的格式进行详细的讲解。

之前从网上看到了一些保存bmp图片的代码,在本地执行都会出现问题,本人就进行了自己编写,可以指定文件的文件名和文件类型

也可以从github中获取源码:

直接看代码:/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>typedefunsignedshortWORD;typedefunsignedintDWORD;typedefunsignedlongLONG;typedefstructtagBITMAPFILEHEADER{WORDbfType;DWORDbfSize;WORDbfReserved1;WORDbfReserved2;DWORDbfOffBits;}__attribute__((packed))BITMAPFILEHEADER;typedefstructtagBITMAPINFOHEADER{DWORDbiSize;/*infoheadersizeinbytes*/DWORDbiWidth;/*widhtofimage*/DWORDbiHeight;/*heightofimage*/WORDbiPlanes;/*numberofcolourplanes*/WORDbiBitCount;/*bitsperpixel*/DWORDbiCompression;/*compressiontype*/DWORDbiSizeImage;/*imagesizemeter*/DWORDbiXPelsPerMeter;/*pixelspermeter*/DWORDbiYPelsPerMeter;/*pexelspermeter*/DWORDbiClrUsed;/*numberofcolour*/DWORDbiClrImportant;/*importantcolour*/}__attribute__((packed))BITMAPINFOHEADER;intoutput_file;inttype_file;structfb_fix_screeninfofinfo;structfb_var_screeninfovinfo;staticconststructoptionlong_options[]={{"output",1,NULL,'o'},{"t",1,NULL,'t'},{NULL,0,NULL,0}};staticvoidusage(void){fprintf(stderr,"imax6[option]...\n""-o|--outputOutputthefilename.\n""-t|--typeOutputthetypeofthefilename.\n""-h|--helphelpinformation.\n");};staticvoidimage_bmp(constchar*filename){printf("startingbmp..\n");chartmpbufilename[126]={0};if(NULL!=filename){strcpy(tmpbufilename,filename);strcat(tmpbufilename,".bmp");}else{strcpy(tmpbufilename,"screen.bmp");}FILE*fp;BITMAPFILEHEADERbmfh;BITMAPINFOHEADERbmih;((unsignedchar*)&bmfh.bfType)[0]='B';((unsignedchar*)&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;/*说明图像的大小,以字节为单位。当用BI_RGB格式时,总设置为0*/bmih.biXPelsPerMeter=0;/*缺省值*/bmih.biYPelsPerMeter=0;bmih.biClrUsed=0;/*说明位图实际使用的调色板索引数,0:使用所有的调色板索引*/bmih.biClrImportant=0;/*说明对图像显示有重要影响的颜色索引的数目,如果是0,表示都重要*/printf("tmpbufilename=%s\n",tmpbufilename);FILE*image_file=fopen(tmpbufilename,"a");if(NULL==image_file){printf("imagefopenfail\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("rawfilefopenfail..\n");}/*fgetc是一种计算机语言中的函数。意为从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节。格式:intfgetc(FILE*stream);*///intch=fgetc(raw_file);intch=getc(raw_file);/*for(y=0;y<vinfo.yres;y+){for(x=0;x<vinfo.xres;x++){longlocation=(x+vinfo.xoffset)*(vinfo.bits_per_pixel/8)+(y+vinfo.yoffset)*finfo.line_length;write(image_file,fbp+location,4);}}*/intx,y;for(y=vinfo.yres-1;y>=0;y--){for(x=0;x<vinfo.xres;x++){/*字节数*/longlocation=(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);}}/*while(ch!=EOF){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("endingbmp\n");}intmain(intargc,char**argv){intopt=0;intoptions_index=0;char*tmp=NULL;charfilename[126]={0};chartype[126]={0};if(argc==1){usage();return2;}/*解析命令行参数*/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();return2;break;}}intfb_fd=open("/dev/fb0",O_RDWR);if(fb_fd==-1){printf("openfail..\n");return-1;}ioctl(fb_fd,FBIOGET_VSCREENINFO,&vinfo);ioctl(fb_fd,FBIOGET_FSCREENINFO,&finfo);longscreensize=vinfo.yres_virtual*finfo.line_length;FILE*fp=popen("bash./test.sh","r");pclose(fp);//bmpif(output_file&&type_file){if(!strcmp(type,"bmp")){image_bmp(filename);}elseif(!strcmp(type,"png")){printf("pngtype=%s\n",type);}else{printf("unkown\n");}}else{usage();return2;}close(fb_fd);return0;}

[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>typedefunsignedshortWORD;typedefunsignedintDWORD;typedefunsignedlongLONG;typedefstructtagBITMAPFILEHEADER{WORDbfType;DWORDbfSize;WORDbfReserved1;WORDbfReserved2;DWORDbfOffBits;}__attribute__((packed))BITMAPFILEHEADER;typedefstructtagBITMAPINFOHEADER{DWORDbiSize;/*infoheadersizeinbytes*/DWORDbiWidth;/*widhtofimage*/DWORDbiHeight;/*heightofimage*/WORDbiPlanes;/*numberofcolourplanes*/WORDbiBitCount;/*bitsperpixel*/DWORDbiCompression;/*compressiontype*/DWORDbiSizeImage;/*imagesizemeter*/DWORDbiXPelsPerMeter;/*pixelspermeter*/DWORDbiYPelsPerMeter;/*pexelspermeter*/DWORDbiClrUsed;/*numberofcolour*/DWORDbiClrImportant;/*importantcolour*/}__attribute__((packed))BITMAPINFOHEADER;intoutput_file;inttype_file;structfb_fix_screeninfofinfo;structfb_var_screeninfovinfo;staticconststructoptionlong_options[]={{"output",1,NULL,'o'},{"t",1,NULL,'t'},{NULL,0,NULL,0}};staticvoidusage(void){fprintf(stderr,"imax6[option]...\n""-o|--outputOutputthefilename.\n""-t|--typeOutputthetypeofthefilename.\n""-h|--helphelpinformation.\n");};staticvoidimage_bmp(constchar*filename){printf("startingbmp..\n");chartmpbufilename[126]={0};if(NULL!=filename){strcpy(tmpbufilename,filename);strcat(tmpbufilename,".bmp");}else{strcpy(tmpbufilename,"screen.bmp");}FILE*fp;BITMAPFILEHEADERbmfh;BITMAPINFOHEADERbmih;((unsignedchar*)&bmfh.bfType)[0]='B';((unsignedchar*)&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;/*说明图像的大小,以字节为单位。当用BI_RGB格式时,总设置为0*/bmih.biXPelsPerMeter=0;/*缺省值*/bmih.biYPelsPerMeter=0;bmih.biClrUsed=0;/*说明位图实际使用的调色板索引数,0:使用所有的调色板索引*/bmih.biClrImportant=0;/*说明对图像显示有重要影响的颜色索引的数目,如果是0,表示都重要*/printf("tmpbufilename=%s\n",tmpbufilename);FILE*image_file=fopen(tmpbufilename,"a");if(NULL==image_file){printf("imagefopenfail\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("rawfilefopenfail..\n");}/*fgetc是一种计算机语言中的函数。意为从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节。格式:intfgetc(FILE*stream);*///intch=fgetc(raw_file);intch=getc(raw_file);/*for(y=0;y<vinfo.yres;y+){for(x=0;x<vinfo.xres;x++){longlocation=(x+vinfo.xoffset)*(vinfo.bits_per_pixel/8)+(y+vinfo.yoffset)*finfo.line_length;write(image_file,fbp+location,4);}}*/intx,y;for(y=vinfo.yres-1;y>=0;y--){for(x=0;x<vinfo.xres;x++){/*字节数*/longlocation=(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);}}/*while(ch!=EOF){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("endingbmp\n");}intmain(intargc,char**argv){intopt=0;intoptions_index=0;char*tmp=NULL;charfilename[126]={0};chartype[126]={0};if(argc==1){usage();return2;}/*解析命令行参数*/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();return2;break;}}intfb_fd=open("/dev/fb0",O_RDWR);if(fb_fd==-1){printf("openfail..\n");return-1;}ioctl(fb_fd,FBIOGET_VSCREENINFO,&vinfo);ioctl(fb_fd,FBIOGET_FSCREENINFO,&finfo);longscreensize=vinfo.yres_virtual*finfo.line_length;FILE*fp=popen("bash./test.sh","r");pclose(fp);//bmpif(output_file&&type_file){if(!strcmp(type,"bmp")){image_bmp(filename);}elseif(!strcmp(type,"png")){printf("pngtype=%s\n",type);}else{printf("unkown\n");}}else{usage();return2;}close(fb_fd);return0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。