我使用libjpeg将V4L2缓冲区保存到文件中。生成的图像看起来很棒。现在我正在尝试设置JPEG的质量(范围1-100)。我查阅了文档:https://www4.cs.fau.de/Services/Doc/graphics/doc/jpeg/libjpeg.html
我看到有一个jpeg_set_quality函数。我把它添加到我的代码中它总是看起来像最高质量,即使质量参数是1。
如何让质量参数生效?感谢。
我的保存jpeg功能如下所示:
static void save_jpeg(CamInfo *cam, struct v4l2_buffer *dqBuf)
{
static int inited = 0;
static int fileno = 0;
int row_stride;
JSAMPROW row_pointer[1];
char *image_buf;
FILE * outfile;
char filename[32];
if(!inited)
{
memset(&cam->cinfo, 0, sizeof(cam->cinfo));
memset(&cam->jerr, 0, sizeof(cam->jerr));
cam->cinfo.err = jpeg_std_error(&cam->jerr);
jpeg_create_compress(&cam->cinfo);
cam->cinfo.image_width = WIDTH; //image width and height, in pixels
cam->cinfo.image_height = HEIGHT;
cam->cinfo.input_components = 3; // must be 3
cam->cinfo.in_color_space = JCS_YCbCr;
printf("Setting JPEG quality: %d\n",quality);
jpeg_set_quality(&cam->cinfo, quality, (boolean)0);
jpeg_set_defaults(&cam->cinfo);
inited = 1;
}
if( continuous_stream )
fileno = 0; /* same filename each time */
printf("About to write JPEG image\n");
sprintf(filename, "/tmp/nginx/vl.jpeg");
if ((outfile = fopen(filename, "wb")) == NULL)
{
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_dest(&cam->cinfo, outfile);
jpeg_start_compress(&cam->cinfo, TRUE);
row_stride = WIDTH * 3;
image_buf = planar_to_interleave(mCam.frames[dqBuf->index]);
while (cam->cinfo.next_scanline < cam->cinfo.image_height)
{
row_pointer[0] = (JSAMPROW)(&image_buf[cam->cinfo.next_scanline * row_stride]);
(void) jpeg_write_scanlines(&cam->cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cam->cinfo);
fclose(outfile);
}
答案 0 :(得分:2)
在jpeg_set_defaults()
之后调用jpeg_set_quality()
将质量重置为75.交换函数调用的顺序以解决此问题。
GLOBAL(void)
jpeg_set_defaults (j_compress_ptr cinfo)
{
…
/* Set up two quantization tables using default quality of 75 */
jpeg_set_quality(cinfo, 75, TRUE);