#include "SaveMoveImageEngine.h"


using namespace ai_matrix;

SaveMoveImageEngine::SaveMoveImageEngine() {}

SaveMoveImageEngine::~SaveMoveImageEngine() {}

APP_ERROR SaveMoveImageEngine::Init()
{

    strPort0_ = engineName_ + "_" + std::to_string(engineId_) + "_0";

    this->baseConfig_ = Config::getins()->getBaseConfig();
    vecCompressionParams_.push_back(cv::IMWRITE_JPEG_QUALITY);  //选择jpeg
    vecCompressionParams_.push_back(this->iPicQuality_);        //图片质量

    LogInfo << "SaveMoveImageEngine Init ok";
    return APP_ERR_OK;
}

APP_ERROR SaveMoveImageEngine::DeInit()
{
    LogInfo << "SaveMoveImageEngine DeInit ok";
    return APP_ERR_OK;
}

APP_ERROR SaveMoveImageEngine::Process()
{


    int iRet = APP_ERR_OK;
    while (!isStop_)
    {
        std::shared_ptr<void> pvoidd = nullptr;
        inputQueMap_[strPort0_]->pop(pvoidd);
        if (nullptr == pvoidd)
        {
            usleep(1000);
            continue;
        }
        
        std::shared_ptr<SaveImgData> pSaveImgData = std::static_pointer_cast<SaveImgData>(pvoidd);

        if (pSaveImgData->cvImage.empty())
        {
            LogWarn << "图片异常，跳过";
            continue;
        }

        cv::Mat image = pSaveImgData->cvImage.clone();

        //图片绝对路径
        if (pSaveImgData->strFilePath.back() != '/')
        {
            pSaveImgData->strFilePath += "/";
        }
        std::string strImgFilePath = pSaveImgData->strFilePath + pSaveImgData->strFileName;

        if (!FileUtil::getins()->createDirPath(pSaveImgData->strFilePath))
        {
            LogError << " 创建图片存储目录失败:" << strImgFilePath;
            continue;
        }

        if (cv::imwrite(strImgFilePath, image, this->vecCompressionParams_))
        {
        }
        else
        {
            LogError << "图片存储失败：" << strImgFilePath;
        }
    }
    return APP_ERR_OK;
}
