您的位置:首页 > 其它

使用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: