看看网上没有 完成的编解码的代码。 这里写出来 给懒人提供一些code。
首先 添加头文件 和 所需要的库
#include "IJL/include/ijl.h"
#pragma comment(lib,"IJL//lib//ijl15.lib")
1 解码。
原型: bool decodeFromJPEGBuffer(unsigned char* lpJpgBuffer,unsigned int dwJpgBufferSize,unsigned char** lppRgbBuffer,unsigned int* lpdwWidth,unsigned int* lpdwHeight,unsigned int* lpdwNumberOfChannels)
bool bres;
IJLERR jerr;
DWORD dwWholeImageSize;
BYTE* lpTemp = NULL;
JPEG_CORE_PROPERTIES jcprops;
bres = true;
__try
{
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = false;
__leave;
}
jcprops.JPGFile = NULL;
jcprops.JPGBytes = lpJpgBuffer;
jcprops.JPGSizeBytes = dwJpgBufferSize;
jerr = ijlRead(&jcprops, IJL_JBUFF_READPARAMS);
if(IJL_OK != jerr)
{
bres = false;
__leave;
}
switch(jcprops.JPGChannels)
{
case 1:
{
jcprops.JPGColor = IJL_G;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBChannels = 3;
break;
}
case 3:
{
jcprops.JPGColor = IJL_YCBCR;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBChannels = 3;
break;
}
default:
{
jcprops.JPGColor = IJL_OTHER;
jcprops.DIBColor = IJL_OTHER;
jcprops.DIBChannels = jcprops.JPGChannels;
break;
}
}
dwWholeImageSize = jcprops.JPGWidth * jcprops.JPGHeight *
jcprops.DIBChannels;
lpTemp = new BYTE [dwWholeImageSize];
注意 在解码中是 函数内部申请内存 在调用之后要释放掉
if(NULL == lpTemp)
{
bres = false;
__leave;
}
jcprops.DIBWidth = jcprops.JPGWidth;
jcprops.DIBHeight = jcprops.JPGHeight;
jcprops.DIBPadBytes = 0;
jcprops.DIBBytes = lpTemp;
jerr = ijlRead(&jcprops, IJL_JBUFF_READWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = false;
__leave;
}
}
__finally
{
if(FALSE == bres)
{
if(NULL != lpTemp)
{
delete [] lpTemp;
lpTemp = NULL;
}
}
ijlFree(&jcprops);
*lpdwWidth = jcprops.DIBWidth;
*lpdwHeight = jcprops.DIBHeight;
*lpdwNumberOfChannels = jcprops.DIBChannels;
*lppRgbBuffer = lpTemp;
}
return bres;
2 编码。
bool EncodeToJPEGBuffer(BYTE* pRgbBuffer,DWORD dwWidth,DWORD dwHeight,BYTE* pJpgBuffer,DWORD &dwJpgBufferSize)
// 源RGB数据 未对齐的(in) 图像宽度(in) 图像高度 (in) 编码后的JpegBuf(out) JpegBuf的长度(out)
{
DWORD dwRgbBufferSize = dwWidth * dwHeight;//计算出存放图像数据的最大空间
预先算出最大的Jpg大小
IJLERR jerr;
JPEG_CORE_PROPERTIES jcprops;
// Set up the info on the desired DIB properties.
ijlInit(&jcprops);
!!!初始化之后必须释放 否则内存泄漏
填充jcprops 结构体
jcprops.DIBBytes = pRgbBuffer;
jcprops.DIBWidth = dwWidth;
jcprops.DIBHeight = dwHeight; // Implies a bottom-up DIB.
jcprops.DIBPadBytes = 0;
jcprops.DIBChannels = 3;//单通道图像表示一个字节就是一个像素,-3标识-3个字节表示一个像素
jcprops.DIBColor = IJL_BGR;
jcprops.DIBSubsampling = IJL_NONE;//通道数决定的
//目标JPG图像的设置
jcprops.JPGFile = NULL;
// 若直接产生JPG文档则填充该变量
jcprops.JPGSizeBytes = dwRgbBufferSize;//Max buffer size.
jcprops.JPGWidth = dwWidth;
jcprops.JPGHeight = dwHeight;
jcprops.JPGChannels = 3;
jcprops.JPGColor = IJL_RGB;//灰度彩色空间Grayscale color space.
jcprops.JPGSubsampling = IJL_NONE; // 4:1:1 subsampling
jcprops.JPGBytes = pJpgBuffer;//Pointer to buffer based JPEG.
jcprops.jquality = 60; // Select "good" image quality【-100】
jerr =
ijlWrite(&jcprops,IJL_JBUFF_WRITEWHOLEIMAGE);
ijlFree(&jcprops );
if(IJL_OK != jerr)//发生错误提示信息
{
AfxMessageBox((LPCTSTR)ijlErrorStr(jerr),MB_OK,NULL);//ijlErrorStr---Return a pointer to a string with error description.
return FALSE;
}
dwJpgBufferSize = jcprops.JPGSizeBytes;//供外部引用,计算数据量的大小
return TRUE;
}
PS Endode部分的内存是自己提供的 不在内部分配 外部释放 和解码不太一样。 而送入的内存区大小不能小于 w*h 。 否则程序访问非法内存 就挂了
其中 IJL_DIB_PAD_BYTES 可以设置
首先 添加头文件 和 所需要的库
#include "IJL/include/ijl.h"
#pragma comment(lib,"IJL//lib//ijl15.lib")
1 解码。
原型: bool decodeFromJPEGBuffer(unsigned char* lpJpgBuffer,unsigned int dwJpgBufferSize,unsigned char** lppRgbBuffer,unsigned int* lpdwWidth,unsigned int* lpdwHeight,unsigned int* lpdwNumberOfChannels)
bool bres;
IJLERR jerr;
DWORD dwWholeImageSize;
BYTE* lpTemp = NULL;
JPEG_CORE_PROPERTIES jcprops;
bres = true;
__try
{
jerr = ijlInit(&jcprops);
if(IJL_OK != jerr)
{
bres = false;
__leave;
}
jcprops.JPGFile = NULL;
jcprops.JPGBytes = lpJpgBuffer;
jcprops.JPGSizeBytes = dwJpgBufferSize;
jerr = ijlRead(&jcprops, IJL_JBUFF_READPARAMS);
if(IJL_OK != jerr)
{
bres = false;
__leave;
}
switch(jcprops.JPGChannels)
{
case 1:
{
jcprops.JPGColor = IJL_G;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBChannels = 3;
break;
}
case 3:
{
jcprops.JPGColor = IJL_YCBCR;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBChannels = 3;
break;
}
default:
{
jcprops.JPGColor = IJL_OTHER;
jcprops.DIBColor = IJL_OTHER;
jcprops.DIBChannels = jcprops.JPGChannels;
break;
}
}
dwWholeImageSize = jcprops.JPGWidth * jcprops.JPGHeight *
jcprops.DIBChannels;
lpTemp = new BYTE [dwWholeImageSize];
注意 在解码中是 函数内部申请内存 在调用之后要释放掉
if(NULL == lpTemp)
{
bres = false;
__leave;
}
jcprops.DIBWidth = jcprops.JPGWidth;
jcprops.DIBHeight = jcprops.JPGHeight;
jcprops.DIBPadBytes = 0;
jcprops.DIBBytes = lpTemp;
jerr = ijlRead(&jcprops, IJL_JBUFF_READWHOLEIMAGE);
if(IJL_OK != jerr)
{
bres = false;
__leave;
}
}
__finally
{
if(FALSE == bres)
{
if(NULL != lpTemp)
{
delete [] lpTemp;
lpTemp = NULL;
}
}
ijlFree(&jcprops);
*lpdwWidth = jcprops.DIBWidth;
*lpdwHeight = jcprops.DIBHeight;
*lpdwNumberOfChannels = jcprops.DIBChannels;
*lppRgbBuffer = lpTemp;
}
return bres;
2 编码。
bool EncodeToJPEGBuffer(BYTE* pRgbBuffer,DWORD dwWidth,DWORD dwHeight,BYTE* pJpgBuffer,DWORD &dwJpgBufferSize)
// 源RGB数据 未对齐的(in) 图像宽度(in) 图像高度 (in) 编码后的JpegBuf(out) JpegBuf的长度(out)
{
DWORD dwRgbBufferSize = dwWidth * dwHeight;//计算出存放图像数据的最大空间
预先算出最大的Jpg大小
IJLERR jerr;
JPEG_CORE_PROPERTIES jcprops;
// Set up the info on the desired DIB properties.
ijlInit(&jcprops);
!!!初始化之后必须释放 否则内存泄漏
填充jcprops 结构体
jcprops.DIBBytes = pRgbBuffer;
jcprops.DIBWidth = dwWidth;
jcprops.DIBHeight = dwHeight; // Implies a bottom-up DIB.
jcprops.DIBPadBytes = 0;
jcprops.DIBChannels = 3;//单通道图像表示一个字节就是一个像素,-3标识-3个字节表示一个像素
jcprops.DIBColor = IJL_BGR;
jcprops.DIBSubsampling = IJL_NONE;//通道数决定的
//目标JPG图像的设置
jcprops.JPGFile = NULL;
// 若直接产生JPG文档则填充该变量
jcprops.JPGSizeBytes = dwRgbBufferSize;//Max buffer size.
jcprops.JPGWidth = dwWidth;
jcprops.JPGHeight = dwHeight;
jcprops.JPGChannels = 3;
jcprops.JPGColor = IJL_RGB;//灰度彩色空间Grayscale color space.
jcprops.JPGSubsampling = IJL_NONE; // 4:1:1 subsampling
jcprops.JPGBytes = pJpgBuffer;//Pointer to buffer based JPEG.
jcprops.jquality = 60; // Select "good" image quality【-100】
jerr =
ijlWrite(&jcprops,IJL_JBUFF_WRITEWHOLEIMAGE);
ijlFree(&jcprops );
if(IJL_OK != jerr)//发生错误提示信息
{
AfxMessageBox((LPCTSTR)ijlErrorStr(jerr),MB_OK,NULL);//ijlErrorStr---Return a pointer to a string with error description.
return FALSE;
}
dwJpgBufferSize = jcprops.JPGSizeBytes;//供外部引用,计算数据量的大小
return TRUE;
}
PS Endode部分的内存是自己提供的 不在内部分配 外部释放 和解码不太一样。 而送入的内存区大小不能小于 w*h 。 否则程序访问非法内存 就挂了
其中 IJL_DIB_PAD_BYTES 可以设置