Libjpeg搭建手记

来源:互联网 发布:蘑菇街秒杀软件 编辑:程序博客网 时间:2024/05/21 18:36

搭建环境

Ubuntu16.04
Qt5.7.1

Step1

下载libjpeg。地址:http://libjpeg.sourceforge.net/
下载后解压到本地文件夹

Step2

进入控制台,如下指令进行安装。

sudo ./configuresudo makesudo make install

安装完成后,进行测试
make test
如生成jpg文件,则安装完成。
注,根据网上教程,libjpeg库需要依赖libtool,
sudo apt install libtool
即可

Step 3

安装libjpeg后,默认执行的.so文件位于/usr/local/lib/中,因此需要将其加入环境变量。
加入方法如下:
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib' >> ~/.bashrc
该文件会在每次开启系统时执行,重新启动即可使用。

Step 4

为了在Qt中使用libjpeg,我们需要自动配置依赖库:
sudo ldconfig
即可。至此libjpeg在Qt中即可使用。

测试代码

(来源于他人文章)如可运行,则libjpeg配置成功:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <stddef.h>#include <stdint.h>#include <iostream>using namespace std;extern "C" {#include <jpeglib.h>}int demo(){    FILE *input_file;    input_file=fopen("test.jpg","rb");    struct jpeg_decompress_struct cinfo;//JPEG图像在解码过程中,使用jpeg_decompress_struct类型的结构体来表示,图像的所有信息都存储在结构体中    struct jpeg_error_mgr jerr;//定义一个标准的错误结构体,一旦程序出现错误就会调用exit()函数退出进程    cinfo.err = jpeg_std_error(&jerr);//绑定错误处理结构对象    jpeg_create_decompress(&cinfo);//初始化cinfo结构    jpeg_stdio_src(&cinfo,input_file);//指定解压缩数据源    jpeg_read_header(&cinfo,TRUE);//获取文件信息    jpeg_start_decompress(&cinfo);//开始解压缩    unsigned long width = cinfo.output_width;//图像宽度    unsigned long height = cinfo.output_height;//图像高度    unsigned short depth = cinfo.output_components;//图像深度    unsigned char *src_buff;//用于存取解码之后的位图数据(RGB格式)    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;            //指针偏移一行    }    jpeg_finish_decompress(&cinfo);//解压缩完毕    jpeg_destroy_decompress(&cinfo);// 释放资源    free(src_buff);//释放资源    cout<<width<<" "<<height<<" "<<depth<<endl;    fclose(input_file);}
原创粉丝点击