#include "EncodeDecodeHelper.h"
///
/// 构造函数
///
EncodeDecodeHelper::EncodeDecodeHelper()
{
_imgDecode = cv::Mat();
_imgEncode = cv::Mat();
}
///
/// 析构函数
///
EncodeDecodeHelper::~EncodeDecodeHelper()
{
_imgDecode.release();
_imgEncode.release();
_dataEncode.clear();
_dataDecode.clear();
}
///
/// 编码
///
bool EncodeDecodeHelper::Encode(ImageInfo srcImgInfo,ImwriteExtension extension,
ImwriteParam* imwriteParams,int paramCount, uint8_t* dstImgData, int& dstDataSize)
{
try
{
auto depthFlag = ImageHelper::GetDepthFlag(srcImgInfo.colorType);
_imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), depthFlag, (void*)srcImgInfo.dataBuffer);
std::vector params;
if(paramCount>0)
{
for(int ni=0; ni dstDataSize)
{
char strMsgBuff[32];
std::snprintf(strMsgBuff, 32, "not enough space to copy the encoded image data. ( need: %i actual: %i ) ",
_dataEncode.size(), dstDataSize);
ErrorMsg::SetErrorMsg(EncodeError,{ strMsgBuff });
return false;
}
for(int ni=0; ni
/// 解码
///
bool EncodeDecodeHelper::Decode(const uint8_t* srcImgData, const int srcDataSize,
uint8_t* dstImgData, const int dstDataSize, ImreadModes readMode)
{
try
{
_dataDecode.resize(srcDataSize);
for(int ni=0; ni
/// 图像编码并保存至本地
///
bool EncodeDecodeHelper::SaveImage(ImageInfo srcImgInfo, ImwriteExtension extension,
ImwriteParam* imwriteParams, int paramCount, const char* savePath)
{
try
{
auto depthFlag = ImageHelper::GetDepthFlag(srcImgInfo.colorType);
_imgEncode = cv::Mat(cv::Size(srcImgInfo.width, srcImgInfo.height), depthFlag, (void*)srcImgInfo.dataBuffer);
std::vector params;
if (paramCount > 0)
{
for (int ni = 0; ni < paramCount; ni++)
{
ImwriteParam imwriteParam = imwriteParams[ni];
switch (imwriteParam.flagName)
{
case JpegQuality:
params.push_back(cv::IMWRITE_JPEG_QUALITY);
break;
case JpegProgressive:
params.push_back(cv::IMWRITE_JPEG_PROGRESSIVE);
break;
case JpegOptimize:
params.push_back(cv::IMWRITE_JPEG_OPTIMIZE);
break;
case JpegRstInterval:
params.push_back(cv::IMWRITE_JPEG_RST_INTERVAL);
break;
case JpegLumaQuality:
params.push_back(cv::IMWRITE_JPEG_LUMA_QUALITY);
break;
case JpegChromaQuality:
params.push_back(cv::IMWRITE_JPEG_CHROMA_QUALITY);
break;
case PngCompression:
params.push_back(cv::IMWRITE_PNG_COMPRESSION);
break;
case PngStrategy:
params.push_back(cv::IMWRITE_PNG_STRATEGY);
break;
case PngBilevel:
params.push_back(cv::IMWRITE_PNG_BILEVEL);
break;
}
// Todo 没有检查值是否在范围内
params.push_back(imwriteParam.flagValue);
}
}
cv::String ext;
switch (extension)
{
case Png:
ext = ".png";
break;
case Jpg:
ext = ".jpg";
break;
case Bmp:
ext = ".bmp";
break;
default:
char strMsgBuff[32];
std::snprintf(strMsgBuff, 32, "unexpected extension value ( %i ) ", extension);
ErrorMsg::SetErrorMsg(EncodeError, { strMsgBuff });
return false;
}
cv::imwrite(savePath, _imgEncode);
return true;
}
catch (const std::exception& ex)
{
ErrorMsg::SetErrorMsg(EncodeError, { ex.what() });
return false;
}
}