使用LibJpg保存JPG图像或数据
2015-08-01 14:45
441 查看
使用LibJpg保存JPG图像或数据
libJpg 是处理JPG 较多的三文库。保存JPG
int savejpeg(const char *filename, unsigned char *bits, int width, int height, int depth) { 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); jpeg_create_compress(&cinfo); if ((outfile = fopen(filename, "wb")) == NULL) { fprintf(stderr, "can't open %s/n", filename); return -1; } jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = width; /* image width and height, in pixels */ cinfo.image_height = 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, 100, TRUE /* limit to baseline-JPEG values */); jpeg_start_compress(&cinfo, TRUE); row_stride = width * depth; /* JSAMPLEs per row in image_buffer */ while (cinfo.next_scanline < cinfo.image_height) { //这里我做过修改,由于jpg文件的图像是倒的,所以改了一下读的顺序 //这是原代码:row_pointer[0] = & bits[cinfo.next_scanline * row_stride]; row_pointer[0] = & bits[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride]; (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); } jpeg_finish_compress(&cinfo); fclose(outfile); jpeg_destroy_compress(&cinfo); return 0; }
转换数据成 JPG格式
bool CompressJpeg(unsigned char *bits, int width, int height, int depth, int iCompressRate, unsigned char *pDest, unsigned long& lLen) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; JSAMPROW row_pointer[1]; // pointer to JSAMPLE row[s] int row_stride; //physical row width in image buffer // cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); unsigned char* pCompressBuffer = NULL; // unsigned long lLen(0); jpeg_mem_dest(&cinfo, &pCompressBuffer, &lLen); JpegErrorMgr jerrMgr; if(setjmp(jerrMgr.setjmp_buffer) != 0) return false; cinfo.image_width = width; /* image width and height, in pixels */ cinfo.image_height = 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, iCompressRate, TRUE /* limit to baseline-JPEG values */); jpeg_start_compress(&cinfo, TRUE); row_stride = width * depth; /* JSAMPLEs per row in image_buffer */ while (cinfo.next_scanline < cinfo.image_height) { //这里我做过修改,由于jpg文件的图像是倒的,所以改了一下读的顺序 //这是原代码:row_pointer[0] = & bits[cinfo.next_scanline * row_stride]; row_pointer[0] = & bits[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride]; (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); } jpeg_finish_compress(&cinfo); memcpy(pDest, pCompressBuffer, lLen); free(pCompressBuffer); jpeg_destroy_compress(&cinfo); return 0; }
Example
脚注
[1] http://bbs.csdn.net/topics/390542727[2] http://bbs.csdn.net/topics/390542727
[3]http://www.cppblog.com/cc/archive/2012/02/22/166217.html
相关文章推荐
- 深入理解Hadoop集群和网络
- bzoj3796: Mushroom追妹纸
- mysql字符串查找(统计客源)
- C语言-自定义函数,及其调用;函数返回值类型的自动转换,即其数量;
- svn的配置和使用
- 《游戏程序设计模式》 1.3 - 观察者模式
- 【2015多校】【hdoj 5336】XYZ and Drops 模拟 论时间驱动的模拟实现与事件驱动的模拟实现
- hdu 5333 Undirected Graph (LCT)
- Linux socket编程(三) 简单的多线程聊天室
- iOS 与 Android 生命周期对比
- [robocup] 3D仿真环境搭建
- 机试算法讲解: 第45题 深度优先搜索之寻找沙特石油存储区
- Linux 网络编程 调试工具
- 数据同步相关专利
- java环境变量配置
- 【Swift】学习笔记(八)——类和结构体
- PF_INET 和 AF_INET的区别
- setsockopt的作用
- 敏捷软件开发模型--SCRUM
- 机试算法讲解: 第44题 深度优先搜索之素数环问题