目前借助于microhttp的网络传输一般都是以二进制形式直接读取图像文件,比如jpeg等。但是我们往往同时有其它操作,比如通过opencv对读取的图像进行处理,内存中以Mat的形式进行存储运算,若取data区域数据直接作为buffer通过网络传输,往往不会得到正确的结果,原因就在于没有经过jpeg的编解码过程。若通过存储图像文件后再进行读取上传,势必会增加io操作,程序运行效率大打折扣,故在此探讨一种内存直接编解码上传的方法。直接上代码:
void myjpegcompress(unsigned char* imageData,unsigned char* compressedBuffer,int imageWidth,int imageHeight,int channels,unsigned long& outSize)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
unsigned char* inImageBuffer=imageData;
unsigned char* outbuffer;
outbuffer=NULL;
JSAMPROW row_pointer[1];
int row_stride;
cinfo.err=jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo,&outbuffer,&outSize);
cinfo.image_width=imageWidth;
cinfo.image_height=imageHeight;
cinfo.input_components=channels;
cinfo.in_color_space=JCS_G