libjpeg库的交叉编译

libjpeg库主要用于jpeg格式图片的编解码,其交叉编译过程如下

1.  下载源码

从官方网站http://www.ijg.org/files/ 下载libjpeg库的源码,本次编译过程使用的是jpegsrc.v9a.tar.gz

2. 解压源码

2.1 切换到下载目录,执行

tar -xzvf jpegsrc.v9a.tar.gz

2.2 切换到源码目录

cd jpeg-9a/

3. 交叉编译

3.1 设置交叉编译器的环境变量

export CC=/home/jarvischu/arm-linux-uclibcgnueabi-gcc

3.2 执行configure,其中–prefix 用来指定编译结果的存放位置; –host 用来指明交叉编译

./configure --prefix=/home/jarvischu/jpeg --enable-shared --enable-static --host=arm-unknown-linux

3.3 执行make命令

make

3.4 执行make install 命令

sudo make install

至此,交叉编译结束,编译结果存放在/home/jarvischu/jpeg目录下,该目录包含了编译得到的lib和include

使用libjpeg库实现jpeg图片的缩放(缩略图)

#include

#include "jpeglib.h"

#include

struct my_error_mgr {

struct jpeg_error_mgr pub;/* "public" fields */

jmp_buf setjmp_buffer;/* for return to caller */

};

typedef struct my_error_mgr * my_error_ptr;

/*

* Here's the routine that will replace the standard error_exit method:

*/

METHODDEF(void)

my_error_exit (j_common_ptr cinfo)

{

/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */

my_error_ptr myerr = (my_error_ptr) cinfo->err;

/* Always display the message. */

/* We could postpone this until after returning, if we chose. */

(*cinfo->err->output_message) (cinfo);

/* Return control to the setjmp point */

longjmp(myerr->setjmp_buffer, 1);

}

//读取Jpeg图片的数据并返回,如果出错,返回NULL

unsigned char* ReadJpeg(const char* path, int& width, int& height)

{

FILE *file = fopen( path, "rb" );

if ( file == NULL ){

return NULL;

}

struct jpeg_decompress_struct info; //for our jpeg info

// struct jpeg_error_mgr err; //the error handler

// info.err = jpeg_std_error(&err);

struct my_error_mgr my_err;

info.err = jpeg_std_error(&my_err.pub);

my_err.pub.error_exit = my_error_exit;

/* Establish the setjmp return context for my_error_exit to use. */

if (setjmp(my_err.setjmp_buffer)) {

/* If we get here, the JPEG code has signaled an error.

* We need to clean up the JPEG object, close the input file, and return.

*/

printf("Error occured\n");

jpeg_destroy_decompress(&info);

fclose(file);

return NULL;

}

jpeg_create_decompress( &info ); //fills info structure

jpeg_stdio_src( &info, file ); //void

int ret_Read_Head = jpeg_read_header( &info, 1 ); //int

if(ret_Read_Head != JPEG_HEADER_OK){

printf("jpeg_read_header failed\n");

fclose(file);

jpeg_destroy_decompress(&info);

return NULL;

}

bool bStart = jpeg_start_decompress( &info );

if(!bStart){

printf("jpeg_start_decompress failed\n");

fclose(file);

jpeg_destroy_decompress(&info);

return NULL;

}

int w = width = info.output_width;

int h = height = info.output_height;

int numChannels = info.num_components; // 3 = RGB, 4 = RGBA

unsigned long dataSize = w * h * numChannels;

// read RGB(A) scanlines one at a time into jdata[]

unsigned char *data = (unsigned char *)malloc( dataSize );

if(!data) return NULL;

unsigned char* rowptr;

while ( info.output_scanline < h )

{

rowptr = data + info.output_scanline * w * numChannels;

jpeg_read_scanlines( &info, &rowptr, 1 );

}

jpeg_finish_decompress( &info );

fclose( file );

return data;

}

/*参数为:

*返回图片的宽度(w_Dest),

*返回图片的高度(h_Dest),

*返回图片的位深(bit_depth),

*源图片的RGB数据(src),

*源图片的宽度(w_Src),

*源图片的高度(h_Src)

*/

unsigned char* do_Stretch_Linear(int w_Dest,int h_Dest,int bit_depth,unsigned char *src,int w_Src,int h_Src)

{

int sw = w_Src-1, sh = h_Src-1, dw = w_Dest-1, dh = h_Dest-1;

int B, N, x, y;

int nPixelSize = bit_depth/8;

unsigned char *pLinePrev,*pLineNext;

unsigned char *pDest = new unsigned char[w_Dest*h_Dest*bit_depth/8];

unsigned char *tmp;

unsigned char *pA,*pB,*pC,*pD;

for(int i=0;i<=dh;++i)

{

tmp =pDest + i*w_Dest*nPixelSize;

y = i*sh/dh;

N = dh - i*sh%dh;

pLinePrev = src + (y++)*w_Src*nPixelSize;

//pLinePrev =(unsigned char *)aSrc->m_bitBuf+((y++)*aSrc->m_width*nPixelSize);

pLineNext = (N==dh) ? pLinePrev : src+y*w_Src*nPixelSize;

//pLineNext = ( N == dh ) ? pLinePrev : (unsigned char *)aSrc->m_bitBuf+(y*aSrc->m_width*nPixelSize);

for(int j=0;j<=dw;++j)

{

x = j*sw/dw*nPixelSize;

B = dw-j*sw%dw;

pA = pLinePrev+x;

pB = pA+nPixelSize;

pC = pLineNext + x;

pD = pC + nPixelSize;

if(B == dw)

{

pB=pA;

pD=pC;

}

for(int k=0;k

{

*tmp++ = ( unsigned char )( int )(

( B * N * ( *pA++ - *pB - *pC + *pD ) + dw * N * *pB++

+ dh * B * *pC++ + ( dw * dh - dh * B - dw * N ) * *pD++

+ dw * dh / 2 ) / ( dw * dh ) );

}

}

}

return pDest;

}

bool write_JPEG_file (const char * filename, unsigned char* image_buffer, int quality,int image_height, int image_width)

{

if(filename == NULL || image_buffer == NULL) return false;

struct jpeg_compress_struct cinfo;

struct jpeg_error_mgr jerr;

FILE * outfile;/* target file */

JSAMPROW row_pointer[1];/* pointer to JSAMPLE row[s] */

int row_stride;/* physical row width in image buffer */

cinfo.err = jpeg_std_error(&jerr);

/* Now we can initialize the JPEG compression object. */

jpeg_create_compress(&cinfo);

if ((outfile = fopen(filename, "wb")) == NULL) {

fprintf(stderr, "can't open %s\n", filename);

return false;

}

jpeg_stdio_dest(&cinfo, outfile);

cinfo.image_width = image_width; /* image width and height, in pixels */

cinfo.image_height = image_height;

cinfo.input_components = 3;/* # of color components per pixel */

cinfo.in_color_space = JCS_RGB; /* colorspace of input image */

jpeg_set_defaults(&cinfo);

jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);

jpeg_start_compress(&cinfo, TRUE);

row_stride = image_width * 3;/* JSAMPLEs per row in image_buffer */

while (cinfo.next_scanline < cinfo.image_height) {

row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];

(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);

}

jpeg_finish_compress(&cinfo);

fclose(outfile);

jpeg_destroy_compress(&cinfo);

return true;

}

//生成图片的缩略图(图片的一个缩小版本)

bool generate_image_thumbnail(const char* inputFile, const char* outputFile)

{

if(inputFile == NULL || outputFile == NULL) return false;

//读取jpeg图片像素数组

int w=0,h=0;

unsigned char* buff = ReadJpeg(inputFile,w,h);

if(buff == NULL) {

printf("ReadJpeg Failed\n");

return false;

}

//缩放图片,缩放后的大小为(tb_w,tb_h)

int tb_w = 160, tb_h = 160;

unsigned char * img_buf = do_Stretch_Linear(tb_w,tb_h,24,buff,w,h);

free(buff);

//将缩放后的像素数组保存到jpeg文件

bool bRetWrite = write_JPEG_file(outputFile,img_buf,10,tb_h,tb_w);

delete[] img_buf;

if(bRetWrite){

return true;

}else{

printf("GenerateImageThumbnail: write failed\n");

return true;

}

}

linux交叉编译jpeg,libjpeg的交叉编译以及jpeg图片的缩放(缩略图)相关推荐

  1. LIBJPEG交叉编译并用其将ARGB32转JPEG

    #编译脚本 build.sh #!/bin/bash gcc_prefix=$1 install_dir=$2 cache_dir=$3 host_name=" " if test ...

  2. 在Linux PC上查看arm交叉编译的可执行文件依赖的动态库

    在Ubuntu宿主机上查看ARM交叉编译好的可执行程序和库文件的相关依赖关系,类似于PC linux上的ldd命令.     在电脑上安装的Linux系统中,有一个ldd命令,可以查看对应的可执行文件 ...

  3. imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 1 extraneous bytes be

    imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: 1 extraneous bytes be ...

  4. linux cmake增量编译,Cmake 设置交叉编译环境

    我用的是合众达的dm6446. 一  首先安装CMAKE 下载cmake 然后解压缩,进入解压缩后的目录,依次执行 # ./bootstrap # make && make insta ...

  5. 嵌入式Linux开发,Ubuntu22下交叉编译内核报错: multiple definition of `yylloc‘; scripts/dtc/dtc-lexer.lex.o:(.bss+0x0

    一.问题描述 嵌入式Linux开发,Ubuntu22下交叉编译内核报错: /usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x10): multiple ...

  6. linux命令操作改变图片大小,如何在Linux命令行中优化和压缩JPEG或PNG图像

    原标题:如何在Linux命令行中优化和压缩JPEG或PNG图像 来自:Linux迷https://www.linuxmi.com/linux-yasuo-jpeg-png.html 您有很多图像,并且 ...

  7. linux LTP移植Android,LTP交叉编译(LTP201XXXX以上版本)

    LTP交叉编译(LTP201XXXX以上版本) 交叉编译工具:4.4.1 liaoye163@sina.com密码123456交叉编译工具在software目录下 LTP版本:Ltp-full-201 ...

  8. libjpeg用法linux压缩,使用libjpeg对图像进行压缩

    (2) 指定压缩数据保存的位置 Libjpeg库把压缩后的数据传输到"数据目的"模块.在本库中包含把数据写入到标准流的"数据目的"模块.如果想把数据写入到其他地 ...

  9. 【交叉编译】什么是交叉编译,为何要有交叉编译?

    一.交叉编译简介 1.什么是交叉编译 1.1 本地编译 解释什么是交叉编译之前,先要明白一个概念:本地编译 我们之前常见的软件开发,都是属于本地编译:在当前的PC下,x86的CPU下,直接编译出来程序 ...

最新文章

  1. java中sax的使用_java使用sax对xml文档的解析
  2. 机器学习项目实战----信用卡欺诈检测
  3. 利用Eclipse连接JDBC-(SQL Server2008)
  4. webplugin 没有画面_[问题记录] webpack devServer HtmlWebpackPlugin 没有加载 js、css
  5. LeetCode 566. Reshape the Matrix
  6. request.META里包含了哪些数据?
  7. 51php服务器稳不稳定,百度经验:两步搞定PHP-FPM优化,让服务器更平稳
  8. 移动端的meta标签
  9. 如何在html中加入音效,JavaScript / HTML5中的音效
  10. python发送多人邮件没有展示收件人问题的解决方法
  11. 关于微博api返回内容不全内容
  12. UAC白名单解决每次启动都弹出UAC对话框(不用关闭UAC)
  13. 电容与电感串联直流电路系统分析
  14. 运维技术相关基础面试
  15. 北风的年终总结2021
  16. 【麒麟操作系统软件商店老是闪退?--麒麟系统软件商店卸载与重装(小白教程)】
  17. git恢复commit过的代码
  18. FilterConfig.RegisterGlobalFilters 全局过滤器的用法
  19. android qq三方登录授权失败,QQ第三方登录无法授权错误码110401的解决方法
  20. CAM350光绘文件检查说明

热门文章

  1. 如何走出物联网死亡之井?
  2. 2019年最值得关注的几个公众号,好评率高达99.99%
  3. 作为曾经的 Web 开发“王者”,jQuery 的传奇怎么续写?
  4. 开学季,复旦老师教你玩转“0”“1”浪漫!| 人物志
  5. 频遭黑客攻击的物联网,这里有妙招!
  6. 开源维护者,必有一战!
  7. Python 模拟微博登陆,亲测有效!
  8. 时间复杂度、空间复杂度,如何”不复杂“地学?
  9. 被罚 50 亿美元,Android 究竟招谁惹谁了?
  10. 开源巨献:2017 年 Google 开源了这些超赞的项目