下载libjpeg 9b并解压
libjpeg 9b
tar -xzf jpegsrc.v9b.tar.gz
进如解压好的文件夹 编译
./configure
mkdir build
make -j32
make install DESTDIR=/XXX/build
拷贝include和lib文件夹到自己的工程
cmake引入
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
代码如下
#include "jpeglib.h"
// 设置一个足够大的buffer
char jpeg[512*1024];
int jpeg_size=512*1024;
rgb2jpeg(rgb_buffer, width, height, channel, jpeg, &jpeg_size);
int rgb2jpeg(const char *rgb_buffer, int width, int height, int depth, char *jpeg_buffer, int *size)
{
jpeg.err = jpeg_std_error(&jerr);
jpeg_create_compress(&jpeg);
jpeg_mem_dest(&jpeg, (unsigned char **)&jpeg_buffer, (long unsigned int *)size);
JSAMPROW row_pointer[1];
jpeg.image_width = width;
jpeg.image_height = height;
jpeg.input_components = depth;
jpeg.in_color_space = JCS_RGB;
jpeg_set_defaults(&jpeg);
jpeg_start_compress(&jpeg, TRUE);
int row_stride = width * depth;
while (jpeg.next_scanline < jpeg.image_height)
{
row_pointer[0] = (JSAMPROW)(rgb_buffer + jpeg.next_scanline * row_stride);
jpeg_write_scanlines(&jpeg, row_pointer, 1);
}
jpeg_finish_compress(&jpeg);
jpeg_destroy_compress(&jpeg);
return 0;
}
最后
如有不好的地方或做的不对的地方,欢迎联系我
WeChat: Debu_cat