win10 vs2017 编译jpeglib

jpeglib是一个跨平台的jpeg图像处理 开源c++ 组件

处理jpeg  很方便

下载地址 :http://www.ijg.org/files/

然后vs2017 编译

打开vs2017 cmd命令行

然后输入nmake的指令:

nmake /f makefile.vc

报错:提示系统找不到指定文件jconfig.h,

这个时候就到解压出来的jpeblib路径下找到jconfig.vc,

然后复制一份,将后缀改名为jconfig.h,重新执行上述指令;

又会报错,提示找不到win32.mak,

win10 没有 win32.mak,可以到github 查找  下载

找到makefile.vc,修改第12行包含win32.mak的那条代码路径

这个路径可能要看电脑具体的位置的。然后重复上述指令,编译,然后完成,静态库出来了

使用方法:

libjepg.lb  加入导入库

//网上的代码,地址忘了

int rgb2jpgAction(struct jpeg_compress_struct* pCinfo, const unsigned char *pRgbData, const int width, const int height)
{
	int depth = 3;
	JSAMPROW row_pointer[1];

	pCinfo->image_width = width;
	pCinfo->image_height = height;
	pCinfo->input_components = depth;
	pCinfo->in_color_space = JCS_RGB;

	jpeg_set_defaults(pCinfo);
	jpeg_set_quality(pCinfo, 80, TRUE);

	jpeg_start_compress(pCinfo, TRUE);

	int row_stride = width * depth;
	while (pCinfo->next_scanline < pCinfo->image_height)
	{
		row_pointer[0] = (JSAMPROW)(pRgbData + pCinfo->next_scanline * row_stride);
		jpeg_write_scanlines(pCinfo, row_pointer, 1);
	}

	jpeg_finish_compress(pCinfo);
	jpeg_destroy_compress(pCinfo);

	return 0;
}

/**
 这里特别说明jpeg_mem_dest的第二个参数,buffer。
 如果在rgb2jpg声明指针或者缓冲区,然后试图复制到pDest,直接崩溃;或者取不到数据。
 研究了半天不行。必须是如下的写法。
 如果缓冲区不够怎么办?那就开大一点。
 char pDest[512*1024];
 int  size=512*1024;
 然后再传递过来。
 */
int rgb2jpg(const unsigned  char *pRgbData, const int width, const int height, int type,  char* pDest, int* pSize)
{
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;

	FILE* pOutFile = NULL;

	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	if (type)
	{
		if ((pOutFile = fopen(pDest, "wb")) == NULL)
		{
			return -1;
		}
		jpeg_stdio_dest(&cinfo, pOutFile);
	}
	else
	{
		jpeg_mem_dest(&cinfo, (unsigned char **)&pDest, (size_t *)pSize);
	}

	rgb2jpgAction(&cinfo, pRgbData, width, height);
	if (type)
	{
		fclose(pOutFile);
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/y281252548/article/details/113660940