修复缓存扫描内存过大问题

This commit is contained in:
masayume_ht 2021-08-09 18:42:20 +08:00
parent aa72fa4e41
commit 0cb98a539d
3 changed files with 34 additions and 5 deletions

View File

@ -782,8 +782,9 @@ void GScanO1003399::imgproce(std::shared_ptr<std::vector<char>>& buff)
if (m_param.pixtype == 1 && m_param.hsvcorrect) if (m_param.pixtype == 1 && m_param.hsvcorrect)
if (mats[i].channels() == 3) if (mats[i].channels() == 3)
cvtColor(mats[i], mats[i], cv::COLOR_BGR2GRAY); cvtColor(mats[i], mats[i], cv::COLOR_BGR2GRAY);
idata = (isbwimg|| m_param.pixtype == 0 || (((m_param.automaticcolortype == 0) && (m_param.automaticcolor == true)) && (mats[i].channels() == 1))) ? (IMat2Bmp)Mat2BmpBw(mats[i], m_param.resolution_dst) : Mat2Bmp(mats[i], m_param.resolution_dst); idata = (isbwimg|| m_param.pixtype == 0 || (((m_param.automaticcolortype == 0) && (m_param.automaticcolor == true)) && (mats[i].channels() == 1))) ? (IMat2Bmp)Mat2BmpBw(mats[i], m_param.resolution_dst) : (IMat2Bmp)Mat2Bmp(mats[i], m_param.resolution_dst);
m_imagedata.Put(idata.getBmpDataBuffer()); m_imagedata.Put(idata.getBmpDataBuffer());
idata.~IMat2Bmp();
} }
} }
@ -882,6 +883,11 @@ void GScanO1003399::proc(bool bcachemode)
b_imgprothread = true; b_imgprothread = true;
while (b_imgprothread) while (b_imgprothread)
{ {
if (m_imagedata.Size() > 0)
{
this_thread::sleep_for(chrono::milliseconds(10));
continue;
}
std::shared_ptr<std::vector<char>> buffer; std::shared_ptr<std::vector<char>> buffer;
if (bcachemode) { if (bcachemode) {
if (m_paths.Size() < 1) if (m_paths.Size() < 1)

View File

@ -16,7 +16,10 @@
using namespace std; using namespace std;
class IMat2Bmp { class IMat2Bmp {
public: public:
virtual ~IMat2Bmp() {} virtual ~IMat2Bmp() {
if (m_data.get())
m_data.reset();
}
virtual std::shared_ptr<std::vector<unsigned char>> getBmpDataBuffer() { virtual std::shared_ptr<std::vector<unsigned char>> getBmpDataBuffer() {
return m_data; return m_data;
}; };
@ -29,13 +32,33 @@ protected:
class Mat2Bmp:public IMat2Bmp { class Mat2Bmp:public IMat2Bmp {
public: public:
Mat2Bmp(const cv::Mat& mat,float res) { Mat2Bmp(const cv::Mat& mat,float res) {
m_data =std::shared_ptr<std::vector<unsigned char>>(new std::vector<unsigned char>()); //m_data =std::shared_ptr<std::vector<unsigned char>>(new std::vector<unsigned char>());
m_data.reset(new std::vector<unsigned char>());
int headersize = mat.channels() == 3 ? 54 : 1078; int headersize = mat.channels() == 3 ? 54 : 1078;
int bmpdatasize = mat.step1() * mat.rows; int step = mat.step;
m_data->resize(headersize + bmpdatasize); int bmpdatasize = (step + 3) / 4 * 4 * mat.rows;
int m_datalinesize = (step + 3) / 4 * 4;
int height = mat.rows;
//m_data->resize(headersize + bmpdatasize);
m_data.reset(new std::vector<unsigned char>(headersize + bmpdatasize));
if (mat.channels() == 1)
{
uchar colortable[256 * 4]{ 0 };
for (int i = 0; i < 256; i++) {
colortable[i*4] = colortable[i*4 + 1] = colortable[i*4 + 2] = i;
}
memcpy(m_data->data() + 54, colortable, 256 * 4);
}
cv::imencode(".bmp", mat, *(m_data.get())); cv::imencode(".bmp", mat, *(m_data.get()));
setBmpFileHeader(mat); setBmpFileHeader(mat);
setBmpInfoHeader(mat, res); setBmpInfoHeader(mat, res);
//uchar* data = m_data->data() + headersize + bmpdatasize;
//uchar* matdata = mat.data;
//for (int i = 0; i < mat.rows; i++){
// data -= m_datalinesize;
// memcpy(data, matdata, step);
// matdata += step;
//}
} }
private: private:
void setBmpFileHeader(const cv::Mat& mat) void setBmpFileHeader(const cv::Mat& mat)

Binary file not shown.