(原)调用jpeglib对图像进行压缩
网址: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对图像进行压缩的更多相关文章
- 使用方向变换(directional transform)图像分块压缩感知
论文的思路是先介绍分块压缩感知BCS,然后介绍使用投影和硬阈值方法的迭代投影方法PL,接着将PL与维纳滤波器结合形成SPL(平滑PL),并且介绍了稀疏表示的几种基,提出了两种效果较好的稀疏基:CT与D ...
- C#调用7z实现文件的压缩与解压
1.关于7z 首先在这里先介绍一下7z压缩软件,7z是一种主流的 压缩格式,它拥有极高的压缩比.在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式.主要有以下特点: 来源且模块化的组 ...
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
XML序列化 #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...
- Android 调用jepg库进行图片压缩,保持图片不失真
1. 浅谈为什么Android和iOS图片质量差距那么大? 首先来说,作为一个安卓狗,机器当然用的是安卓的手机.现在的安卓手机大多数都会以高清拍照,动不动就几千万柔光相机来吸引各种买家.买来后,拍照发 ...
- 图像jpeg压缩
图像分割 8X8 颜色空间转换RGB->YCbCr 3个8X8的矩阵 离散余弦变换:(Discrete cosine transform),简称DCT. DCT转换后的数组中第一个是一个直线数据 ...
- C++调用Matlab引擎 图像读写与处理 (知识+代码篇)
准备知识 之 Matlab Engine 执行命令 /* Execute matlab statement */ int engEvalString(Engine* ep, const char* s ...
- C# - WinFrm应用程序调用SharpZipLib实现文件的压缩和解压缩
前言 本篇主要记录:VS2019 WinFrm桌面应用程序调用SharpZipLib,实现文件的简单压缩和解压缩功能. SharpZipLib 开源地址戳这里. 准备工作 搭建WinFrm前台界面 添 ...
- swift 图像的压缩上传
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [Str ...
- C# 如何进行图像的压缩
从网上找的非常有效.图片3M到500k private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCode ...
随机推荐
- java-web-dom4j解析XML-递归方式
<?xml version="1.0" encoding="UTF-8"?><书架> <书 出版日期="2013-10 ...
- React/React Native的 ES5 ES6 写法对照
ES5 ES6 模块 var React = require("react-native); var { Image, Text, View } = React; import Re ...
- php中的时区设置
date_default_timezone_set('PRC'); 设置为中国时区
- 开心菜鸟系列学习笔记------javascript(5)
一.this的关系 1)全局代码中的this 2)函数代码中的this在函数代码中使用this时很有趣,这种情况很难且会导致很多问题. 这种类型的代码中,this值的首要特点(或许是最主要 ...
- Effective Java2读书笔记-对于所有对象都通用的方法(一)
第8条:覆盖equals时请遵守通用约定 ①约定的内容 自反性.对于任何非null的引用值x.x.equals(x)必须返回true. 对称性.对于任何非null的引用值x和y.当且仅当y.equal ...
- PagedList.MVC 应用
1. NuGet 下载 PagedList.MVC 2. View Page @model PagedList.IPagedList<Libaray.Models.Entities.BookMo ...
- InnoSetup XML操作函数
用于InnoSetup 5 以上.对XML文件的操作,简化InnoSetup XML访问过程. 1. [代码]InnoSetup 5 脚本 { ======================== ...
- Java学习系列(一)Java的运行机制、JDK的安装配置及常用命令详解
俗话说:“十五的月亮十六圆”.那学习是不是也是如此呢?如果把月亮看成是我们的愿望,那十五便是我们所处的“高原期”,坚持迈过这个坎,我相信你的愿望终究会现实的.记得马云曾说:今天很残酷,明天更残酷,后天 ...
- Raspberrypi安装使用开发简要说明
Raspberrypi安装使用开发简要说明 (更新于2013年8月25日 newsuppy) 一,安装 使用win32diskimager将操作系统的image刷在SD卡上,image文件可以在htt ...
- DELL配置信息
CPU详情CPU厂商 英特尔CPU (英特尔)Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHzCPU核心数 2CPU默认频率 2400 MhzCPU外频 533 MHz ...