newtx/imgproc/algs/image_encoder.cpp

120 lines
3.6 KiB
C++
Raw Normal View History

2024-02-27 04:03:51 +00:00
#include "image_encoder.h"
#include <huagao/hgscanner_error.h>
#include <sane/sane_ex.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// img_encoder
static std::string device_opt_json[] = {
"{\"img-fmt\":{\"cat\":\"imgp\",\"group\":\"output\",\"title\":\"\\u56fe\\u7247\\u683c\\u5f0f\",\"desc\":\"\\u8bbe\\u5907\\u8f93\\u51fa\\u7684\\u56fe\\u7247\\u6587\\u4ef6\\u683c\\u5f0f\",\"type\":\"string\",\"ver\":1,\"pos\":9900,\"ui-pos\":10,\"auth\":10,\"size\":16,\"cur\":\"JPEG\",\"default\":\"JPEG\",\"range\":[\"JPEG\",\"PNG\",\"BMP\"]},\"jpeg-quality\":{\"cat\":\"imgp\",\"group\":\"output\",\"title\":\"JPEG\\u8d28\\u91cf\",\"desc\":\"\\u8bbe\\u7f6eJPEG\\u538b\\u7f29\\u8d28\\u91cf\\uff0c\\u8d28\\u91cf\\u8d8a\\u9ad8\\uff0c\\u538b\\u7f29\\u7387\\u8d8a\\u4f4e\",\"type\":\"int\",\"ver\":1,\"pos\":9901,\"ui-pos\":11,\"auth\":10,\"affect\":4,\"unit\":\"%\",\"size\":4,\"cur\":100,\"default\":100,\"range\":{\"min\":10,\"max\":100,\"step\":1},\"depend\":\"img-fmt==JPEG\"}}"
2024-02-27 04:03:51 +00:00
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// img_encoder
img_encoder::img_encoder(bool weaker) : image_processor("img_encoder")
2024-02-27 04:03:51 +00:00
{
if(weaker)
return;
2024-02-27 04:03:51 +00:00
ADD_THIS_JSON();
2024-02-27 04:03:51 +00:00
param_.push_back(cv::IMWRITE_JPEG_QUALITY);
param_.push_back(jpeg_quality_);
}
img_encoder::~img_encoder()
{}
int img_encoder::set_value(const char* name, void* val)
{
int ret = SCANNER_ERR_OK;
if(strcmp(name, SANE_OPT_NAME(OUT_FORMAT)) == 0)
{
fmt_ = (char*)val;
if(fmt_ == "JPEG")
{
fmt_ = ".jpg";
param_.clear();
param_.push_back(cv::IMWRITE_JPEG_QUALITY);
param_.push_back(jpeg_quality_);
}
else if(fmt_ == "PNG")
{
fmt_ = ".png";
param_.clear();
param_.push_back(CV_IMWRITE_PNG_STRATEGY);
param_.push_back(cv::IMWRITE_PNG_STRATEGY_FIXED);
}
else
{
fmt_ = ".bmp";
param_.clear();
}
}
else if(strcmp(name, SANE_OPT_NAME(JPEG_QUALITY)) == 0)
{
jpeg_quality_ = *(int*)val;
}
else
{
ret = SCANNER_ERR_DEVICE_NOT_SUPPORT;
}
return ret;
}
image_processor* img_encoder::copy_weaker(void)
{
img_encoder *weaker = new img_encoder(true);
weaker->pos_ = pos_;
weaker->enabled_ = enabled_;
weaker->fmt_ = fmt_;
weaker->jpeg_quality_ = jpeg_quality_;
weaker->param_ = param_;
return weaker;
}
2024-02-27 04:03:51 +00:00
int img_encoder::process(std::vector<PROCIMGINFO>& in, std::vector<PROCIMGINFO>& out)
{
for(auto& v: in)
{
v.info.prc_stage = get_position();
v.info.prc_time = 0;
out.push_back(v);
}
return SCANNER_ERR_OK;
}
std::shared_ptr<std::vector<uchar>> img_encoder::encode(LPPACKIMAGE head, cv::Mat& mat)
{
std::shared_ptr<std::vector<uchar>> ptr;
chronograph watch;
head->prc_stage = get_position();
if(fmt_ == ".bmp")
{
size_t size = mat.total() * mat.channels();
ptr.reset(new std::vector<uchar>(size));
memcpy(ptr->data(), mat.data, size);
head->prc_time = watch.elapse_ms();
head->format = IMG_FMT_BMP;
}
else
{
2024-02-28 09:52:40 +00:00
cv::Mat out;
cv::flip(mat, out, 0); // 0 - flip around x-axis; >0 - flip around y-axis; <0 - flip both x and y
2024-02-27 04:03:51 +00:00
ptr.reset(new std::vector<uchar>());
head->format = fmt_ == ".jpg" ? IMG_FMT_JPEG : IMG_FMT_PNG;
2024-02-28 09:52:40 +00:00
cv::imencode(fmt_.c_str(), out, *ptr, param_);
2024-02-27 04:03:51 +00:00
head->prc_time = watch.elapse_ms();
}
return ptr;
}