libjpeg库的安装、移植、交叉编译环境、编解码。

来源:互联网 发布:淘宝零食视频 编辑:程序博客网 时间:2024/05/20 03:08

libjpeg库的安装、移植、交叉编译环境、编解码

 

本为分libjpeg安装、使用、移植到ARM、交叉编译及只想来介绍。Libjpeg库为开源库,全部由C语言编写,

可以实现jpeg图片的编解码,库很小,便于移植。但是功能有限,只能实现jpeg图片的编解码。

库下载:从http://libjpeg.sourceforge.net/网站上可以下载;

1.安装

Linux下安装:解压完之后到达根目录下

             1./configure  //如果不可以先加权:chmod +x configure;

                              或者sudo ./configure

             2make      

             3make install //进行安装

             编译代码:gcc –o目标文件源程序文件–ljpeg

             如果出现llibjpeg.so.9文件链接到/uer/lib库文件加下。

                  我电脑上该文件安装到/usr/local/lib

                   使用语句ln–s /usr/local/lib/ llibjpeg.so.9 /usr/lib

                   最后一句可以参考但是不能照搬,要看绝对路径。

 

2、库的使用

我这里使用了一个sample.jpg的图片,然后读取出他的信息,然后就继续调用该库生成一幅灰度图像,名字为test.jpg.的代码段如下;

   #include    #include    #include    #include    #include   int mkjpeg(unsigned int width,unsigned int height,unsigned char *buffer,char *filename);   int main() {     struct jpeg_decompress_struct cinfo;  //定义jpeg工作项目     struct jpeg_error_mgr jerr;      //定义错去处理项目     FILE * infile;                  //打开jpeg文件指针      //绑定标准错误处理结构       cinfo.err = jpeg_std_error(&jerr);         //初始化JPEG对象       jpeg_create_decompress(&cinfo);      //指定图像文件       if ((infile = fopen("sample.jpg", "rb")) == NULL)       {            perror("fopen fail");            return 0;       }       jpeg_stdio_src(&cinfo, infile);       //读取图像信息          (void) jpeg_read_header(&cinfo, TRUE);      //设定解压缩参数,此处我们将图像尺寸不变,     cinfo.scale_num=1;       cinfo.scale_denom=1;       cinfo.out_color_space = JCS_GRAYSCALE;  //输出为灰度图像,若为彩色则为:JCS_RGB;      (void) jpeg_start_decompress(&cinfo);  //开始解压;上面那些参数要在该函数之前,jpeg_read_header()之后。    unsigned int  width  = cinfo.output_width;//图像宽度    unsigned int  height = cinfo.output_height;//图像高度    unsigned short depth  = cinfo.output_components;//图像深度    printf("%3d--%3d--%d",width,height,depth);    //unsigned char *src_buff;//用于存取解码之后的位图数据(RGB格式)     unsigned char *src_buff;          src_buff = (unsigned char  *)malloc(width * height*depth );//分配位图数据空间     memset(src_buff, 0, sizeof(unsigned char) *width * height*depth );//清0    JSAMPARRAY buffer;//用于存取一行数据    buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, width*depth, 1);//分配一行数据空间    unsigned char *point = src_buff;    while(cinfo.output_scanline < height)//逐行读取位图数据    {        jpeg_read_scanlines(&cinfo, buffer, 1);    //读取一行jpg图像数据到buffer        memcpy(point, *buffer, width*depth);    //将buffer中的数据逐行给src_buff        point += width * depth;            //指针偏移一行    }    point = src_buff;    mkjpeg(width,height,point,"test.jpg");    }//定义图片编码函数int mkjpeg(unsigned int width,unsigned int height,unsigned char *buffer,char *filename){    struct jpeg_compress_struct jcs;   //和上面一样;    struct jpeg_error_mgr jem;         //和上面一样;    FILE *fp;      JSAMPROW row_pointer[1]; // 一行位图    int row_stride; // 每一行的字节数        jcs.err = jpeg_std_error(&jem);    jpeg_create_compress(&jcs);        fp = fopen(filename,"wb");    if (fp==NULL)    {    return 1;    }    jpeg_stdio_dest(&jcs, fp);        jcs.image_width = width; // 位图的宽和高,单位为像素    jcs.image_height = height;    jcs.input_components = 1; // 在此为1,表示灰度图, 如果是彩色位图,则为3    jcs.in_color_space = JCS_GRAYSCALE;//JCS_GRAYSCALE表示灰度图,JCS_RGB表示彩色图像        jpeg_set_defaults(&jcs);    jpeg_set_quality (&jcs, 20, TRUE); //中间数值为图像压缩率,1-100;100画质最好,1最低;        jpeg_start_compress(&jcs, TRUE); //默认设置        row_stride = width * jcs.input_components;      while (jcs.next_scanline < jcs.image_height)    {    row_pointer[0] = &buffer[jcs.next_scanline * row_stride];    jpeg_write_scanlines(&jcs, row_pointer, 1);    }        jpeg_finish_compress(&jcs);        jpeg_destroy_compress(&jcs);    fclose(fp);        return 0;    }                

使用:   gcc –o a jpeg.c –ljpeg

生成名字为a的可执行文件。


3、移植到ARM

继续使用上卖弄下载的库

加压并进入该文件夹;

使用命令  

 ./configure CC=arm-linux-gcc LD=arm-linux-ld --host=arm-linux 

--prefix=/root/Desktop/jpeglib  --enable-shared --enable-static

 

    CC =你的交叉编译工具,后面类似。

     在最后–prefix后面加上你要解压的位置。我这里即将库安装到在桌面并且新建一个jpeglib文件夹。

     然后执行

     make

     make install

     完成安装;

   

4、ARM下使用

     建立一个文件夹,将jpeg文件夹下的libinclude拷贝到该文件夹下,将你编写的jpeg.c也拷贝到文件夹下,这个时候需要使用Makefile来编译文件。

模板如下:

   INCLUDE=-I./include/

LIBS=./lib/libjpeg.a -ljpeg -lm -ldl

all:jpeg-test

jpeg-test:

       arm-hisiv300-linux-gcc-o jpeg-test jpeg3.c $(INCLUDE) $(LIBS)

clean:

       rm-rfv jpeg-test

 最后将lib下的文件拷贝到ARM下lib文件夹即可使用。

 

 


原创粉丝点击