'struct jpeg_compress_struct' has no member named 'image_quality'
时间: 2024-01-14 17:04:48 浏览: 100
对不起,我之前提供的代码中有一处错误。在libjpeg库中,`struct jpeg_compress_struct`确实没有名为`image_quality`的成员。正确的代码应该是使用`struct jpeg_compress_struct`的`jpeg_set_quality`函数来设置图像的压缩质量。请使用以下修正后的代码:
```c
void compress_image(const char* input_image, const char* output_image, int quality) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE* infile, *outfile;
JSAMPROW row_pointer[1];
int row_stride;
if ((infile = fopen(input_image, "rb")) == NULL) {
fprintf(stderr, "Can't open %s\n", input_image);
return;
}
if ((outfile = fopen(output_image, "wb")) == NULL) {
fprintf(stderr, "Can't create %s\n", output_image);
fclose(infile);
return;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
jpeg_read_header(&cinfo, TRUE);
jpeg_set_quality(&cinfo, quality, TRUE); // 设置压缩质量
jpeg_start_compress(&cinfo, TRUE);
row_stride = cinfo.image_width * cinfo.input_components;
unsigned char* buffer = (unsigned char*)malloc(row_stride);
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &buffer[cinfo.next_scanline * row_stride];
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
free(buffer);
jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
fclose(infile);
}
```
在修正后的代码中,我使用了`jpeg_set_quality`函数来设置压缩质量。你可以传递一个介于0和100之间的整数值作为`quality`参数,其中0表示最低质量,100表示最高质量。
非常抱歉给你带来困扰,希望这次修正后的代码能够帮助到你。如果还有其他问题,请随时提问。
阅读全文