Chinaunix首页 | 论坛 | 博客
  • 博客访问: 339490
  • 博文数量: 45
  • 博客积分: 669
  • 博客等级: 上士
  • 技术积分: 675
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-27 17:59
文章分类
文章存档

2015年(5)

2014年(6)

2013年(4)

2012年(30)

分类: 嵌入式

2012-09-18 10:42:23

jpeg图像为解压后即得到位图数据,与上一篇中的bmp图像显示相比,增加的步骤是需要运用jpeg库提供的解码函数,解码得到图像数据,然后再把原始图像显示出来。


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <sys/mman.h>
  8. #include <linux/fb.h>
  9. #include <unistd.h>
  10. #include <jpeglib.h>
  11. #define FB_DEV "/dev/fb0"
  12.  
  13. static int fb_dev;
  14. static struct fb_var_screeninfo fb_var;
  15. static int screen_w, screen_h, screen_bits;
  16. static void *fbmem, *data;
  17. static FILE *infile;
  18. //JPEG的内容
  19. typedef struct jpeg_color
  20. {
  21.     unsigned char r;
  22.     unsigned char g;
  23.     unsigned char b;
  24. }COLOR;;
  25. typedef struct jpeg_pixel_t
  26. {
  27.     unsigned char r;
  28.     unsigned char g;
  29.     unsigned char b;
  30.     unsigned char a;
  31. }PIXEL_T;

  32. //初始化fb0
  33. void fb_init()
  34. {
  35.     //打开fb0设备文件
  36.     fb_dev = open(FB_DEV, O_RDWR);
  37.     if(fb_dev < 0)
  38.     {
  39.         printf("Error:open %s error\n", FB_DEV);
  40.         printf("Usage:[ xxx.jpg]\n");
  41.         exit(1);
  42.     }
  43.     //获取fb0参数
  44.     ioctl(fb_dev, FBIOGET_VSCREENINFO, &fb_var);
  45.     screen_w = fb_var.xres;
  46.     screen_h = fb_var.yres;
  47.     screen_bits = fb_var.bits_per_pixel;
  48.     printf("Framebuffer:%d * %d\n", screen_w, screen_h);
  49.     printf("screen_bits:%d\n", screen_bits);
  50.     fbmem = mmap(0, screen_w * screen_h * screen_bits / 8, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev, 0);
  51.     
  52.     int screensize=screen_w * screen_h * screen_bits / 8;
  53.     int i=0,bgcolor=0;
  54.     for(i=0;i<screensize;i =fb_var.bits_per_pixel/8) //清屏
  55.         {
  56.             memcpy(fbmem i,&bgcolor,fb_var.bits_per_pixel/8);
  57.         }
  58.      
  59.     return;
  60. }

  61. //显示jpg/jpeg格式图片
  62. void display_jpeg(char *jpegfile)
  63. {
  64.     struct jpeg_decompress_struct cinfo;
  65.     struct jpeg_error_mgr jerr;
  66.     int picture_w, picture_h, picture_bits;
  67.     int i, j, x, y;
  68.     unsigned char *buffer, *tmpbuf;
  69.     COLOR picture_color;
  70.     PIXEL_T **fbp;
  71.     
  72.     infile = fopen(jpegfile, "rb" );
  73.     
  74.     fbp = (PIXEL_T **)malloc(sizeof(PIXEL_T *) * screen_w);
  75.     //x, y 代表图片的起始坐标
  76.     for(i = 0, x = 0 * screen_bits / 8, y = 0; i < screen_w; i )
  77.     {
  78.         fbp[i] = (PIXEL_T *)(fbmem (y i) * screen_w * screen_bits / 8 x);
  79.     }
  80.     cinfo.err = jpeg_std_error(&jerr);
  81.     jpeg_create_decompress(&cinfo);
  82.     //指定要解压缩的图像文件
  83.     jpeg_stdio_src(&cinfo, infile);
  84.     //获取图像信息
  85.     jpeg_read_header(&cinfo, TRUE);
  86.     //开始解压
  87.     jpeg_start_decompress(&cinfo);
  88.     picture_w = cinfo.output_width;
  89.     picture_h = cinfo.output_height;
  90.     picture_bits = cinfo.output_components;
  91.   
  92.     printf("图片信息:\n");
  93.     printf("宽度 = %d\n", picture_w);
  94.     printf("高度 = %d\n", picture_h);
  95.     printf("位数 = %d\n", picture_bits);
  96.   
  97.     //获取颜色值
  98.     buffer = (unsigned char *)malloc(picture_w * picture_bits);
  99.     memset(buffer, 0, picture_w * picture_bits);
  100.     for(i = 0; i < picture_h; i )
  101.     {
  102.         tmpbuf = buffer;
  103.         jpeg_read_scanlines(&cinfo, &tmpbuf, 1);
  104.         for(j = 0; j < picture_w; j )
  105.         {
  106.             memcpy(&picture_color, tmpbuf, sizeof(picture_color));
  107.             fbp[i][j].b = picture_color.r;
  108.             fbp[i][j].g = picture_color.g;
  109.             fbp[i][j].r = picture_color.b;
  110.             tmpbuf = picture_bits;
  111.         }
  112.     }
  113.     //完成解压
  114.     jpeg_finish_decompress(&cinfo);
  115.     //释放解压对象
  116.     jpeg_destroy_decompress(&cinfo);
  117.     free(buffer);
  118.     return;
  119. }

  120. //chartolong
  121. long chartolong(char *string, int length)
  122. {
  123.     long number;
  124.     if(length <= 4)
  125.     {
  126.         memset(&number, 0, sizeof(long));
  127.         memcpy(&number, string, length);
  128.     }
  129.     return number;
  130. }

  131. int main(int argc, char* argv[])
  132. {
  133.     if(argc != 2)
  134.     {
  135.         printf("Usage:[ xxx.jpg]\n");
  136.         exit(1);
  137.     }
  138.     //fb0初始化
  139.     fb_init();
  140.    
  141.     display_jpeg(argv[1]);
  142.     fclose(infile);
  143.     return 0;
  144. }

 

 

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