现在的位置: 首页 > 综合 > 正文

linux下利用libjpeg库对jpg格式图片的操作

2016年11月28日 ⁄ 综合 ⁄ 共 4161字 ⁄ 字号 评论关闭

1、在网上找到了libjpeg,可以用这个库实现JPG图片的各种操作:

下载:http://freeware.sgi.com/source/libjpeg/libjpeg-6b.tar.gz

2、参考README安装库,大体步骤:

./configure

Make

Make install

3、安装后,djpeg命令在/usr/bin目录下,对jpg格式图像的处理的命令格式:

以将jpg格式图片转为gif格式图片为例:

格式:djpeg -gif -outfile new_day.gif new_day.jpg

4、通过程序来对图片进行处理

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
//通过命令djpeg,将jpg格式的图像转换为gif,也可以转为bmp格式
//格式:djpeg -gif -outfile new_day.gif new_day.jpg 
int main()
{
  pid_t pid;
  pid=fork();
  
if(pid==0)
    printf(
"this is main/n");
  
else if(pid>0)
    execl(
"/usr/bin/djpeg","djpeg","-gif","-outfile","new_day.gif","new_day.jpg",(char*)0);
  
else if(pid<0)
    printf(
"error/n");
  printf(
"end/n");
  
return 0;
}

 

step1:下载并解压jpeg源代码包

 

step2:
#cd ./jpeg-6b
#./configure --prefix=/usr/arm/arm-linux --exec-prefix=/usr/arm/arm-linux \
--enable-shared --enable-static
下面分别介绍这几个参数的作用:

--prefix=/usr/arm/arm-linux : 执行make install 后,会将与体系无关的文件拷贝到此目录下,具体如下:
/usr/arm/arm-linux .....................................
|
+---include........................................
|
---jconfig.h
|
---jerror.h
|
---jmorecfg.h
|
---jpeglib.h
+---man............................................
|
+---man1.......................................
|
---cjeg.1
|
---djpeg.1
|
---jpegtran.1
|
---rdjpgcom.1
|
---wrjpgcom.1

--exec-prefix=/usr/arm/arm-linux : 执行make install 后,会将与体系无关的文件拷贝到此目录下,即将一些可执行程序、动态链接库和静态链接库拷贝到此目录的相应目录下,具体如下:
/usr/arm/arm-linux ........................................
|
+---bin............................................
|
---cjeg
|
---djpeg
|
---jpegtran
|
---rdjpgcom
|
---wrjpgcom
+---lib...........................................
|
---libjpeg.la
|
---libjpeg.so
|
---libjpeg.so.62
|
---libjpeg.so.62.0.0

--enable-shared : 用GNU libtool编译成动态链接库
。下面分别对应有无此参数所生成的Makefile的比较:
--------------------------------------------------------------------------------------
无--enable-shared参数             |               有--enable-shared参数
--------------------------------------------------------------------------------------

LIBTOOL =                   |            LIBTOOL = ./libtool
--------------------------------------------------------------------------------------
O = o  A = a                 |            O = lo    A = la
-------------------------------------------------------------------------------------
LN= $(CC)                    |      LN= $(LIBTOOL) --mode=link $(CC)
--------------------------------------------------------------------------------------
INSTALL_PROGRAM= ${INSTALL}     |  INSTALL_PROGRAM= $(LIBTOOL) --mode=install ${INSTALL}
INSTALL_LIB= ${INSTALL} -m 644  |  INSTALL_LIB= $(LIBTOOL) --mode=install ${INSTALL}
---------------------------------------------------------------------------------------
无参数:
# .c.lo:
#  $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c $(srcdir)/$*.c
有参数:
.c.lo:
$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c $(srcdir)/$*.c
------------------------------------------------------------------------------------------
无参数:
install: cjpeg djpeg jpegtran rdjpgcom wrjpgcom
有参数:
install: cjpeg djpeg jpegtran rdjpgcom wrjpgcom install-lib
-----------------------------------------------------------------------------------------

 

step3:修改生成的Makefile文件:
# The name of your C compiler:
CC= gcc  该成  CC=/usr/arm/bin/arm-linux-gcc (根据你自己交叉编译器的位置修改)
# library (.a) file creation command
AR= ar rc 该成  AR= /usr/arm/bin/arm-linux-ar rc  (同上)
# second step in .a creation (use "touch" if not needed)
AR2= ranlib 该成  AR2= /usr/arm/bin/arm-linux-ranlib (同上)

 

step4:#make
#make install
这样JPEG库就交叉编译成功了,相应的库和头文件都在/usr/arm/arm-linux/include 和/usr/arm/arm-linux/lib目录下了

安装好以后就在这2个目录下增加了如下文件:
/usr/arm/arm-linux/include
jerror.h
jmorecfg.h
jconfig.h
jpeglib.h
/usr/arm/arm-linux/lib
libjpeg.a
libjpeg.la
libjpeg.so
libjpeg.so.62
libjpeg.so.62.0.0
编译程序的时候记得加上 -ljpeg
不然大概就有下面这些
/tmp/cc4aJtAS.o: In function `write_JPEG_file':
/tmp/cc4aJtAS.o(.text+0x20): undefined reference to `jpeg_std_error'
/tmp/cc4aJtAS.o(.text+0x3c): undefined reference to `jpeg_CreateCompress'
/tmp/cc4aJtAS.o(.text+0x88): undefined reference to `jpeg_stdio_dest'
/tmp/cc4aJtAS.o(.text+0xbc): undefined reference to `jpeg_set_defaults'
/tmp/cc4aJtAS.o(.text+0xd0): undefined reference to `jpeg_set_quality'
/tmp/cc4aJtAS.o(.text+0xe0): undefined reference to `jpeg_start_compress'
/tmp/cc4aJtAS.o(.text+0x140): undefined reference to `jpeg_write_scanlines'
/tmp/cc4aJtAS.o(.text+0x168): undefined reference to `jpeg_finish_compress'
/tmp/cc4aJtAS.o(.text+0x17c): undefined reference to `jpeg_destroy_compress'

抱歉!评论已关闭.