123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- #include "EncodeDecodeHelper.h"
- /// <summary>
- /// 构造函数
- /// </summary>
- EncodeDecodeHelper::EncodeDecodeHelper()
- {
- _imgDecode = cv::Mat();
- _imgEncode = cv::Mat();
- }
- /// <summary>
- /// 析构函数
- /// </summary>
- EncodeDecodeHelper::~EncodeDecodeHelper()
- {
- _imgDecode.release();
- _imgEncode.release();
- _dataEncode.clear();
- _dataDecode.clear();
- }
- /// <summary>
- /// 编码
- /// </summary>
- 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<int> 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::imencode(ext, _imgEncode, _dataEncode, params);
- int encodedSize = _dataEncode.size();
- if(encodedSize > 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<encodedSize; ni++)
- {
- dstImgData[ni] = _dataEncode[ni];
- }
- dstDataSize = encodedSize;
- return true;
- }
- catch (const std::exception& ex)
- {
- ErrorMsg::SetErrorMsg(EncodeError,{ ex.what() });
- return false;
- }
- }
- /// <summary>
- /// 解码
- /// </summary>
- 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<srcDataSize; ni++)
- {
- _dataDecode[ni] = srcImgData[ni];
- }
- _imgDecode = cv::imdecode(_dataDecode, readMode);
- int size = _imgDecode.total() * _imgDecode.elemSize();
- if(dstDataSize < size)
- {
- char strMsgBuff[64];
- std::snprintf(strMsgBuff, 64, "the dstDataSize ( %i ) is less than need ( %i )", dstDataSize, size);
- ErrorMsg::SetErrorMsg(DecodeError,{ strMsgBuff });
- return false;
- }
- std::memcpy(dstImgData, _imgDecode.data, size);
- return true;
- }
- catch (const std::exception& ex)
- {
- ErrorMsg::SetErrorMsg(DecodeError, { ex.what() });
- return false;
- }
- }
- /// <summary>
- /// 图像编码并保存至本地
- /// </summary>
- 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<int> 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;
- }
- }
|