C++ 读入JPEG,调整大小并保存到磁盘 我想要的是:
将现有jpeg调整为新的更小的jpeg 示例:在_3000x2000.jpeg之前→→→ 在_1024x768.jpeg之后 我想我需要这样的伪函数: foo(文件路径前、文件路径后、新宽度、新高度) 利用 解压缩\u jpeg(在\u文件\u路径之前) 调整大小(解压缩的_数据、新的_宽度、新的_高度) 压缩\u jpeg(调整大小的\u数据) 保存jpeg(压缩数据,在文件路径之后) 如果我误解了什么,请纠正我的做法 我的假设是:C++ 读入JPEG,调整大小并保存到磁盘 我想要的是:,c++,windows,jpeg,libjpeg,C++,Windows,Jpeg,Libjpeg,将现有jpeg调整为新的更小的jpeg 示例:在_3000x2000.jpeg之前→→→ 在_1024x768.jpeg之后 我想我需要这样的伪函数: foo(文件路径前、文件路径后、新宽度、新高度) 利用 解压缩\u jpeg(在\u文件\u路径之前) 调整大小(解压缩的_数据、新的_宽度、新的_高度) 压缩\u jpeg(调整大小的\u数据) 保存jpeg(压缩数据,在文件路径之后) 如果我误解了什么,请纠正我的做法 我的假设是: 由于分辨率会有所不同,首先必须解压缩现有的jpeg,以便可
- 由于分辨率会有所不同,首先必须解压缩现有的jpeg,以便可以正确处理像素数据李>
- 其次,使用一些函数foo()将pixeldata调整为较小的缓冲区
- 第三,压缩缓冲区并将其保存到磁盘
fopen(…)
替换为fopen\u s(…)
我将质量硬编码为100英寸
jpeg\u set\u质量(…)
代码:
#包括
#包括“jpeg-9b\jpeglib.h”
#包括“jpeg-9b\jerror.h”
int TEST_WRITE_JPEG(字符*文件名)
{
//在这里定义一个图片
const int image_width=100;/*图像中的列数*/
const int image_height=100;/*图像中的行数*/
JSAMPLE*image\u buffer=newjsample[image\u width*image\u height]();/*指向R、G、B阶数据的大数组*/
对于(int i=0;i<图像宽度*图像高度;i++)
{
图像缓冲区[i]=255;
}
/*步骤1:分配并初始化JPEG压缩对象*/
结构jpeg\u压缩\u结构cinfo;
结构jpeg\u错误\u管理器jerr;
文件*输出文件;
JSAMPROW行_指针[1];
int row_跨步;
cinfo.err=jpeg\u std\u error(&jerr);
jpeg\u创建\u压缩(&cinfo);
/*步骤2:指定数据目的地(例如,文件)*/
if(fopen_s(&outfile,文件名,“wb”)!=NULL)
{
fprintf(stderr,“无法打开%s\n”,文件名);
出口(1);
}
jpeg_stdio_dest(&cinfo,outfile);
/*步骤3:设置压缩参数*/
cinfo.image_width=图像_width;/*图像宽度和高度,以像素为单位*/
cinfo.image\u height=image\u height;
cinfo.input_components=3;/*#每像素的颜色分量*/
cinfo.in_color_space=JCS_RGB;/*输入图像的颜色空间*/
jpeg\u set\u默认值(&cinfo);
jpeg设置质量(&cinfo,100,真);
/*步骤4:启动压缩机*/
jpeg\u start\u compress(&cinfo,TRUE);
/*步骤5:while(扫描行仍需写入)*/
行步幅=图像宽度*3;
while(cinfo.next\u扫描线
明确问题:
为什么我写的是一幅画而不是纯白色的?如果
image\u buffer
包含超过128x128个无符号字符,为什么程序会崩溃?3个字节用于每个像素,因此您必须分配3*image\u width*image\u height
元素,而只分配image\u width*image\u height
元素。分配并初始化足够的元素。是的,在我点击提交3秒钟后,我立即看到了这一点。哇,这太尴尬了。非常感谢你!一切都解决了。无碰撞,适用于较大尺寸。现在还不能接受你的答案,因为时间还早。你不想这么做。您想用较低的Q值保存它。这种方法将使数据像素化。@EJP我不想具体做什么?我想你说的是调整尺寸的部分吧?请你详细说明一下好吗?
#include <iostream>
#include "jpeg-9b\jpeglib.h"
#include "jpeg-9b\jerror.h"
int TEST_WRITE_JPEG(char* file_name)
{
// DEFINE A PIC HERE
const int image_width = 100; /* Number of columns in image */
const int image_height = 100; /* Number of rows in image */
JSAMPLE* image_buffer = new JSAMPLE[image_width * image_height](); /* Points to large array of R,G,B-order data */
for (int i = 0; i < image_width*image_height; i++)
{
image_buffer[i] = 255;
}
/* Step 1: allocate and initialize JPEG compression object */
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * outfile;
JSAMPROW row_pointer[1];
int row_stride;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
/* Step 2: specify data destination (eg, a file) */
if (fopen_s(&outfile, file_name, "wb") != NULL)
{
fprintf(stderr, "can't open %s\n", file_name);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
/* Step 3: set parameters for compression */
cinfo.image_width = image_width; /* image width and height, in pixels */
cinfo.image_height = image_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);
/* Step 4: Start compressor */
jpeg_start_compress(&cinfo, TRUE);
/* Step 5: while (scan lines remain to be written) */
row_stride = image_width * 3;
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = (JSAMPROW)&image_buffer[cinfo.next_scanline * row_stride];
(void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
/* Step 6: Finish compression */
jpeg_finish_compress(&cinfo);
/* After finish_compress, we can close the output file. */
fclose(outfile);
/* Step 7: release JPEG compression object */
/* This is an important step since it will release a good deal of memory. */
jpeg_destroy_compress(&cinfo);
return 0;
}