网址:http://www.cnblogs.com/darkknightzh/p/4973828.html。未经允许,严禁转载。

参考网站:

http://dev.w3.org/Amaya/libjpeg/example.c

http://www.360doc.com/content/13/0226/15/2036337_268016821.shtml

1. 首先去官网http://www.ijg.org/files/下载源码,下载的是jpegsr9a.zip。

2. 解压后放到E盘根目录,“E:\jpeg-9a\”下会有很多文件。

3. 将“jconfig.vc”改成“jconfig.h”

4. 将“makefile.vc”中第12行

!include <win32.mak>

改成

!include <C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include\win32.mak>

5. 打开vs2013的“VS2013 开发人员命令提示”

6. 定位到E:\jpeg-9a。在命令行中输入:

E:

之后输入:

cd jpeg-9a

7. 输入” nmake -f makefile.vc” 生成所需要的libjpeg.lib函数库。

8. 使用时,将“jconfig.h”、“jmorecfg.h”、“jpeglib.h”、“libjpeg.lib”四个文件拷贝到对应的文件夹内。

9.  libjpeg.lib是用c语言开发的,
    如果在C++程序里使用,需要用extern "C" { }包含一下。如下:

extern "C"
{
#include "jpeglib.h"
}

10. 在“解决方案资源管理器”中“属性页“的”连接器-输入-附加依赖项”内,增加“libjpeg.lib”

11. 从缓冲区生成jpg的程序:

 void GeneJpegFile(const char* jpegFileName, unsigned char* inputData,
int nWidth, int nHeight, int nChannel, int nQuality)
{
/* This struct contains the JPEG compression parameters and pointers to
* working space (which is allocated as needed by the JPEG library).
* It is possible to have several such structures, representing multiple
* compression/decompression processes, in existence at once. We refer
* to any one struct (and its associated working data) as a "JPEG object".
*/
struct jpeg_compress_struct cinfo; /* This struct represents a JPEG error handler. It is declared separately
* because applications often want to supply a specialized error handler
* (see the second half of this file for an example). But here we just
* take the easy way out and use the standard error handler, which will
* print a message on stderr and call exit() if compression fails.
* Note that this struct must live as long as the main JPEG parameter
* struct, to avoid dangling-pointer problems.
*/
struct jpeg_error_mgr jerr; /* More stuff */
FILE *outfile; /* target file */
JSAMPROW row_pointer[]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */ /* Step 1: allocate and initialize JPEG compression object */ /* We have to set up the error handler first, in case the initialization
* step fails. (Unlikely, but it could happen if you are out of memory.)
* This routine fills in the contents of struct jerr, and returns jerr's
* address which we place into the link field in cinfo.
*/
cinfo.err = jpeg_std_error(&jerr); /* Now we can initialize the JPEG compression object. */
jpeg_create_compress(&cinfo); /* Now we can initialize the JPEG compression object. */ /* Step 2: specify data destination (eg, a file) */
/* Note: steps 2 and 3 can be done in either order. */ /* Here we use the library-supplied code to send compressed data to a
* stdio stream. You can also write your own code to do something else.
* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
* requires it in order to write binary files.
*/
if ((outfile = fopen(jpegFileName, "wb")) == NULL)
{
fprintf(stderr, "can't open %s\n", jpegFileName);
return;
}
jpeg_stdio_dest(&cinfo, outfile); /* Step 3: set parameters for compression */ /* First we supply a description of the input image.
* Four fields of the cinfo struct must be filled in:
*/
cinfo.image_width = nWidth; /* image width and height, in pixels */
cinfo.image_height = nHeight;
cinfo.input_components = nChannel; /* # of color components per pixel */ if (nChannel == )
{
cinfo.in_color_space = JCS_GRAYSCALE; /* colorspace of input image */
}
else if (nChannel == )
{
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
} /* Now use the library's routine to set default compression parameters.
* (You must set at least cinfo.in_color_space before calling this,
* since the defaults depend on the source color space.)
*/
jpeg_set_defaults(&cinfo); // Now you can set any non-default parameters you wish to.
// Here we just illustrate the use of quality (quantization table) scaling:
jpeg_set_quality(&cinfo, nQuality, TRUE); /* limit to baseline-JPEG values */ /* Step 4: Start compressor */ /* TRUE ensures that we will write a complete interchange-JPEG file.
* Pass TRUE unless you are very sure of what you're doing.
*/
jpeg_start_compress(&cinfo, TRUE); /* Step 5: while (scan lines remain to be written) */
/* jpeg_write_scanlines(...); */ /* Here we use the library's state variable cinfo.next_scanline as the
* loop counter, so that we don't have to keep track ourselves.
* To keep things simple, we pass one scanline per call; you can pass
* more if you wish, though.
*/
row_stride = nWidth * nChannel; /* JSAMPLEs per row in image_buffer */ while (cinfo.next_scanline < cinfo.image_height)
{
/* jpeg_write_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could pass
* more than one scanline at a time if that's more convenient.
*/
row_pointer[] = &inputData[cinfo.next_scanline * row_stride];
(void)jpeg_write_scanlines(&cinfo, row_pointer, );
} /* Step 6: Finish compression */
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo); /* After finish_compress, we can close the output file. */
fclose(outfile);
}

说明:

1. 灰度图像的话,缓冲区大小就是width*height.

2. RGB图像的话,缓冲区大小是width*height*3,同时,像素排列顺序是RGB RGB RGB RGB

3. 其他格式的话,cinfo.in_color_space需要改成相应的格式,且row_stride的值估计也需要修改,暂时没有考虑。

(原)调用jpeglib对图像进行压缩的更多相关文章

  1. 使用方向变换(directional transform)图像分块压缩感知

    论文的思路是先介绍分块压缩感知BCS,然后介绍使用投影和硬阈值方法的迭代投影方法PL,接着将PL与维纳滤波器结合形成SPL(平滑PL),并且介绍了稀疏表示的几种基,提出了两种效果较好的稀疏基:CT与D ...

  2. C#调用7z实现文件的压缩与解压

    1.关于7z 首先在这里先介绍一下7z压缩软件,7z是一种主流的 压缩格式,它拥有极高的压缩比.在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式.主要有以下特点: 来源且模块化的组 ...

  3. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  4. Android 调用jepg库进行图片压缩,保持图片不失真

    1. 浅谈为什么Android和iOS图片质量差距那么大? 首先来说,作为一个安卓狗,机器当然用的是安卓的手机.现在的安卓手机大多数都会以高清拍照,动不动就几千万柔光相机来吸引各种买家.买来后,拍照发 ...

  5. 图像jpeg压缩

    图像分割 8X8 颜色空间转换RGB->YCbCr 3个8X8的矩阵 离散余弦变换:(Discrete cosine transform),简称DCT. DCT转换后的数组中第一个是一个直线数据 ...

  6. C++调用Matlab引擎 图像读写与处理 (知识+代码篇)

    准备知识 之 Matlab Engine 执行命令 /* Execute matlab statement */ int engEvalString(Engine* ep, const char* s ...

  7. C# - WinFrm应用程序调用SharpZipLib实现文件的压缩和解压缩

    前言 本篇主要记录:VS2019 WinFrm桌面应用程序调用SharpZipLib,实现文件的简单压缩和解压缩功能. SharpZipLib 开源地址戳这里. 准备工作 搭建WinFrm前台界面 添 ...

  8. swift 图像的压缩上传

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [Str ...

  9. C# 如何进行图像的压缩

    从网上找的非常有效.图片3M到500k private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCode ...

随机推荐

  1. 费马小定理&欧拉定理

    在p是素数的情况下,对任意整数x都有xp≡x(mod p).这个定理被称作费马小定理其中如果x无法被p整除,我们有xp-1≡1(mod p).利用这条性质,在p是素数的情况下,就很容易求出一个数的逆元 ...

  2. UVa10651(记忆化搜索)

    题意: 给一串-和o组成的字符串,你可以把“-oo"变成”o--",可以把“oo-”变成“--o",问最后最少有多少个o. 状态压缩 ,记忆化搜索 code: #incl ...

  3. MyISAM 存储引擎

    在MYSQL 5.1 以及之前的版本,MyISAM 是默认的存储引擎.MyISAM 提供了大量的特性,包括全文索引,压缩,空间函数(gis)等,但是MyISAM不支持事务和行级锁,而且有一个毫无疑问的 ...

  4. js中return;、return true、return false;区别

    一.返回控制与函数结果, 语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果  二.返回控制, 无函数结果,语法为:return;  在大多数情况下,为事件 ...

  5. Django RESTful API 设计指南

    网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...

  6. C# .net Jquery ajax 简单示例

    jquery中ajax相信大家都不陌生,这里只写个简单例子示意用法,详细后续再写. 在html中按钮事件中添加如下js var param = "data=" + escape($ ...

  7. CloudStack核心类ApiServlet、ApiServer、ApiDispatcher、GenericDaoBase源码分析

    ApiServlet 首先从整体上看下ApiServlet,Outline视图如下, 一.注意@Inject依赖的是javax.inject.jar,它和spring的@Autowired的区别在于使 ...

  8. Tomcat 6.0.32 +Spring dbcp datasource关闭Tomcat出现严重异常

    异常如下: 信息: Pausing Coyote HTTP/ -- :: org.apache.catalina.core.StandardService stop 信息: Stopping serv ...

  9. Android中focusable属性的妙用——底层按钮的实现

    http://www.cnblogs.com/kofi1122/archive/2011/03/22/1991828.html http://www.juziku.com/weizhishi/3077 ...

  10. LeetCode_Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...