使用LibJpg保存JPG图像或数据
使用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
使用LibJpg保存JPG图像或数据相关推荐
- powerbuilder 保存图表图像_数据可视化/统计图表循序渐进指南
可视化图表,图形是使数字能够实际对话的最强大的方式之一. 一个关于数据可视化的图表一定是严谨并可以直观表达数据.方便决策者去做决策的. 但是,很多刚开始使用的人往往会不知所措,无法下手. 本文作者详细 ...
- OpenCV中图像以Mat类型保存时各通道数据在内存中的组织形式及python代码访问各通道数据的简要方式...
OpenCV中图像以Mat类型保存时各通道数据在内存中的组织形式及python代码访问各通道数据的简要方式 以最简单的4 x 5三通道图像为例,其在内存中Mat类型的数据组织形式如下: 每一行的每一列 ...
- 使用python对单幅图像进行数据增并保存增强后的结果
cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: ...
- C语言(二)BMP图像 文本数据保存为图像
BMP图像数据处理 申请空间读取像素数据 读取TXT文本数据,并保存为图像 读取图片,并将数据存入数组 参考 申请空间读取像素数据 使用VS2015连续读取TXT文本数据时,如果申请数组过大时,会导致 ...
- 【Win 10 应用开发】将墨迹保存到图像的两种方法
IT界最近这几年,各种乱七八糟的东西不断出现,其中能用在实际工作与生活中的,大概也就那么几个.Web 前端也冒出各种框架,这就为那些喜欢乱用框架的公司提供了很好的机会,于是造成很多项目体积越来越庞大, ...
- PyTorch模型的保存加载以及数据的可视化
文章目录 PyTorch模型的保存和加载 模块和张量的序列化和反序列化 模块状态字典的保存和载入 PyTorch数据的可视化 TensorBoard的使用 总结 PyTorch模型的保存和加载 在深度 ...
- html5数据提交到mysql,保存html5画布作为数据到mysql数据库
这可能是不可能的,它只是非常适合我的情况.我使用html5画布作为幻灯片演示者,用户可以使用它并将其保存到本地机器上.保存html5画布作为数据到mysql数据库 我的目标是改为以某种不同的方式将其保 ...
- javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转换为YUV、BGR24或RGB24等图像像素数据...
javacpp-ffmpeg系列: javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片 javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转 ...
- OCR图文识别软件是怎么保存页面图像的
2019独角兽企业重金招聘Python工程师标准>>> ABBYY FineReader作为一款OCR图文识别软件,可以保存识别的页面图像,包括扫描的文档,当文档非常大时,可以仅选取 ...
最新文章
- 谷歌宣布推出Dart编程新语言
- 2016 大数据版图
- OpenCASCADE绘制测试线束:拓扑命令之原语Primitives
- Eclipse新建Android项目后,出现“The import android.support.v7.app cannot be resolved”
- python 一次输入10个数_python 如何一次输入3个整数
- 关于软件项目中的风险
- 学习一下戴戒指的含义[转]
- [转载] python中numpy库的使用
- offline 与 online 事件监听浏览器是否在线
- 学习笔记2 光伏MPPT算法
- 360怎么修改域名服务器地址,怎样修改DNS地址
- 智能系统的信息处理原理
- ubuntu phpmyadmin php5.3,ubuntu中怎么下载安装phpmyadmin
- Pycharm Debug调试(纯干货)
- 什么是Java(什么?Java?)
- 宝藏水晶VRay材质球素材,速来收藏
- 什么是iu组装服务器,IU李知恩和“买家秀”在“教科书”级别的私有服务器共享是邻居...
- 科创人·派拉软件CEO谭翔:零信任本质是数字安全,To B也要深研用户心智
- 心流状态---人们做事时内心的一种状态
- c 和易语言如何传字节集,易语言文本型和字节集型数据相互转换的工具