#include <jpeglib.h>
#define JPEG_QUALITY 100 //图片质量
int savejpg(uchar *pdata, char *jpg_file, int width, int height)
{ //分别为RGB数据,要保存的jpg文件名,图片长宽
int depth = 3;
JSAMPROW row_pointer[1];//指向一行图像数据的指针
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *outfile;
cinfo.err = jpeg_std_error(&jerr);//要首先初始化错误信息
//* Now we can initialize the JPEG compression object.
jpeg_create_compress(&cinfo);
if ((outfile = fopen(jpg_file, "wb")) == NULL)
{
fprintf(stderr, "can't open %s\n", jpg_file);
return -1;
}
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = width; //* image width and height, in pixels
cinfo.image_height = height;
cinfo.input_components = depth; //* # of color components per pixel
cinfo.in_color_space = JCS_RGB; //* colorspace of input image
将RGB图像数据压缩成jpg格式图片的代码实现
这是一个使用C语言将RGB图像数据压缩成JPEG格式的代码示例。通过`jpeglib.h`库,定义JPEG质量参数,设置图像尺寸,然后进行压缩、写入文件和释放资源的操作。
摘要由CSDN通过智能技术生成