调整扫描取图流程,删除部分不必要协议交互

This commit is contained in:
masayume_ht 2021-08-14 17:35:01 +08:00
parent 0cb98a539d
commit 7e4bcca9df
6 changed files with 140 additions and 59 deletions

View File

@ -125,7 +125,7 @@ int GScanO1003399::aquire_bmpdata(std::vector<unsigned char>& bmpdata)
StopWatch sw; StopWatch sw;
while (true) while (true)
{ {
if ((imagecount == 0) && (!is_runing()) && (!scanflag) || (sw.elapsed_s() > 30)) if ((imagecount == 0) && (!scanflag) || (sw.elapsed_s() > 30))
{ {
DoEvents(); DoEvents();
this_thread::sleep_for(chrono::milliseconds(1)); this_thread::sleep_for(chrono::milliseconds(1));
@ -138,7 +138,7 @@ int GScanO1003399::aquire_bmpdata(std::vector<unsigned char>& bmpdata)
set_scannum(abs(roller_num_new - roller_num)); set_scannum(abs(roller_num_new - roller_num));
return AQUIRE_IMAGE_TIMEOUT; return AQUIRE_IMAGE_TIMEOUT;
} }
if (!is_runing()) if (!scanflag)
{ {
int roller_num_new = count(); int roller_num_new = count();
set_scannum(abs(roller_num_new - roller_num)); set_scannum(abs(roller_num_new - roller_num));
@ -213,8 +213,8 @@ bool GScanO1003399::is_scan()
BOOL GScanO1003399::Get_Scanner_PaperOn() BOOL GScanO1003399::Get_Scanner_PaperOn()
{ {
//return scanner_read_reg(m_usb, SR_PAPER_READY); return scanner_read_reg(m_usb, SR_GET_PAPERON);
return true; //return true;
} }
int GScanO1003399::Get_Roller_num() int GScanO1003399::Get_Roller_num()
@ -332,13 +332,13 @@ void GScanO1003399::Scanner_StartScan(UINT16 count)
keeplastimg = true; keeplastimg = true;
reset(); reset();
devState = DEV_ISRUNNING; devState = DEV_ISRUNNING;
//if (scanner_read_reg(m_usb, SR_GET_SLEEP_STAUTUS) != 1) if (scanner_read_reg(m_usb, SR_GET_SLEEP_STAUTUS) != 1)
//{ {
// devState = DEV_WRONG; devState = DEV_WRONG;
// Set_ErrorCode(SLEEPING); Set_ErrorCode(SLEEPING);
// scanflag = false; scanflag = false;
// return; return;
//} }
if (scan_mode()) if (scan_mode())
{ {
devState = DEV_WRONG; devState = DEV_WRONG;
@ -346,12 +346,13 @@ void GScanO1003399::Scanner_StartScan(UINT16 count)
scanflag = false; scanflag = false;
return; return;
} }
//if (!Get_Scanner_PaperOn()) if (!Get_Scanner_PaperOn())
//{ {
// devState = DEV_WRONG; devState = DEV_WRONG;
// Set_ErrorCode(NO_FEED); Set_ErrorCode(NO_FEED);
// return; scanflag = false;
//} return;
}
start(); start();
bool procmode = (m_param.resolution_dst > 200.0f || m_param.papertype == 52 || m_param.papertype == 54 || m_param.papertype == 131); bool procmode = (m_param.resolution_dst > 200.0f || m_param.papertype == 52 || m_param.papertype == 54 || m_param.papertype == 131);
@ -505,14 +506,14 @@ void GScanO1003399::usb_run()
if ((codeconvter(info) == -1)) if ((codeconvter(info) == -1))
{ {
if ((get_ErrorCode() != PAPER_JAM) && (get_ErrorCode() != DETECT_DOUBLE_FEED) && (get_ErrorCode() != DETECT_STAPLE)) //if ((get_ErrorCode() != PAPER_JAM) && (get_ErrorCode() != DETECT_DOUBLE_FEED) && (get_ErrorCode() != DETECT_STAPLE))
{ //{
keeplastimg = false; // keeplastimg = false;
while (im_dev_count()) // while (im_dev_count())
im_rx(); // im_rx();
scanflag = false; // scanflag = false;
} //}
else { //else {
while (!scanner_read_reg(m_usb, SR_GET_IMAGEPROCESSDONE)) while (!scanner_read_reg(m_usb, SR_GET_IMAGEPROCESSDONE))
this_thread::sleep_for(chrono::microseconds(30)); this_thread::sleep_for(chrono::microseconds(30));
if (scanner_read_reg(m_usb, SR_GET_KEEP_LAST_PAPER)) if (scanner_read_reg(m_usb, SR_GET_KEEP_LAST_PAPER))
@ -531,7 +532,7 @@ void GScanO1003399::usb_run()
im_rx(); im_rx();
} }
scanflag = false; scanflag = false;
} //}
if ((devState != DEV_WRONG) && (get_ErrorCode()<=0)) if ((devState != DEV_WRONG) && (get_ErrorCode()<=0))
devState = DEV_STOP; devState = DEV_STOP;
@ -668,7 +669,7 @@ int GScanO1003399::read_data(void* data, int length, int timeout)
FileTools::writelog(log_INFO, "if (!m_usb.get() && !m_usb->is_connected())"); FileTools::writelog(log_INFO, "if (!m_usb.get() && !m_usb->is_connected())");
return 0; return 0;
} }
timeout = std::max(500, timeout); timeout = std::max(1000, timeout);
int readed = 0; int readed = 0;
int reading = 0; int reading = 0;
@ -739,7 +740,6 @@ void GScanO1003399::imgproce(std::shared_ptr<std::vector<char>>& buff)
FileTools::writelog(log_ERROR, e.what()); FileTools::writelog(log_ERROR, e.what());
} }
} }
FileTools::writelog(log_DEBUG, "图像解码耗时 " + to_string(sw.elapsed_ms()));
sw.reset(); sw.reset();
buffs.clear(); buffs.clear();
if (m_param.is_autotext) if (m_param.is_autotext)
@ -771,7 +771,6 @@ void GScanO1003399::imgproce(std::shared_ptr<std::vector<char>>& buff)
CImageApplyRotation(type, m_param.is_backrotate180, m_param.resolution_dst, chRtn).apply(mats,m_param.is_duplex); CImageApplyRotation(type, m_param.is_backrotate180, m_param.resolution_dst, chRtn).apply(mats,m_param.is_duplex);
delete[] chRtn; delete[] chRtn;
} }
FileTools::writelog(log_DEBUG, "图像旋转耗时 " + to_string(sw.elapsed_ms()));
if (m_param.automaticcolor) if (m_param.automaticcolor)
{ {
CImageApplyColorRecognition(m_param.automaticcolortype == 1 ? CImageApplyColorRecognition::ColorRecognitionMode::Color_Gray : CImageApplyColorRecognition::ColorRecognitionMode::Color_Mono).apply(mats,m_param.is_duplex); CImageApplyColorRecognition(m_param.automaticcolortype == 1 ? CImageApplyColorRecognition::ColorRecognitionMode::Color_Gray : CImageApplyColorRecognition::ColorRecognitionMode::Color_Mono).apply(mats,m_param.is_duplex);

View File

@ -181,6 +181,9 @@ enum Scanner_Reg_Defs
SR_GET_SLEEP_STAUTUS, SR_GET_SLEEP_STAUTUS,
SR_GET_IMAGEPROCESSDONE, SR_GET_IMAGEPROCESSDONE,
SR_GET_KEEP_LAST_PAPER, SR_GET_KEEP_LAST_PAPER,
SR_GET_PAPERON,
SR_SET_SPEEDMODE,
SR_GET_SPEEDMODE,
SR_GET_CUO_ERROR = 0x50, SR_GET_CUO_ERROR = 0x50,
SR_GET_DOU_ERROR, SR_GET_DOU_ERROR,
SR_GET_JAM_ERROR, SR_GET_JAM_ERROR,

View File

@ -166,9 +166,13 @@ int UsbScanEx::read_bulk(void* data, int len)
int error_code = GetLastError(); int error_code = GetLastError();
switch (error_code) switch (error_code)
{ {
case ERROR_IO_PENDING: case ERROR_IO_PENDING: {
GetOverlappedResult(h_pipe, lp_overlap, &pdw_ret, TRUE); auto ret = WaitForSingleObject(lp_overlap->hEvent, 300);
//FileTools::writelog(log_FATAL, "WaitForSingleObject return " + std::to_string(ret));;
GetOverlappedResult(h_pipe, lp_overlap, &pdw_ret, FALSE);
//GetOverlappedResult(h_pipe, lp_overlap, &pdw_ret, TRUE);
return pdw_ret; return pdw_ret;
}
case ERROR_FILE_NOT_FOUND: case ERROR_FILE_NOT_FOUND:
case ERROR_ACCESS_DENIED: case ERROR_ACCESS_DENIED:
m_b_is_connected = false; m_b_is_connected = false;
@ -206,7 +210,9 @@ int UsbScanEx::write_bulk(void* data, int len)
switch (GetLastError()) switch (GetLastError())
{ {
case ERROR_IO_PENDING: case ERROR_IO_PENDING:
GetOverlappedResult(h_pipe, lp_overlap, &dw_size, TRUE); WaitForSingleObject(lp_overlap->hEvent, 500);
GetOverlappedResult(h_pipe, lp_overlap, &dw_size, FALSE);
//GetOverlappedResult(h_pipe, lp_overlap, &dw_size, TRUE);
return dw_size; return dw_size;
case ERROR_FILE_NOT_FOUND: case ERROR_FILE_NOT_FOUND:
case ERROR_ACCESS_DENIED: case ERROR_ACCESS_DENIED:

View File

@ -1,17 +1,26 @@
#include "ImageApplyDiscardBlank.h" #include "ImageApplyDiscardBlank.h"
#include "ImageProcess_Public.h" #include "ImageProcess_Public.h"
#include "filetools.h"
CImageApplyDiscardBlank::CImageApplyDiscardBlank()
: m_res(false)
, m_dSize(20)
, m_devTh(15, 15, 15, 15)
{
}
CImageApplyDiscardBlank::CImageApplyDiscardBlank(int blockSize, int devTh) CImageApplyDiscardBlank::CImageApplyDiscardBlank(int blockSize, int devTh)
: m_res(false) : m_res(false)
, m_dSize(blockSize) , m_dSize(blockSize)
, m_devTh(devTh) , m_devTh(devTh)
{ {
} }
CImageApplyDiscardBlank::CImageApplyDiscardBlank()
CImageApplyDiscardBlank::CImageApplyDiscardBlank(bool isnormal)
: m_res(false) : m_res(false)
, m_dSize(200) , m_isNormalDiscard(isnormal)
, m_devTh(15, 15, 15, 15)
{ {
m_dSize = m_isNormalDiscard ? 200 : 300;
m_devTh = m_isNormalDiscard ? cv::Scalar::all(8) : cv::Scalar::all(20);
} }
CImageApplyDiscardBlank::~CImageApplyDiscardBlank(void) CImageApplyDiscardBlank::~CImageApplyDiscardBlank(void)
@ -76,6 +85,13 @@ int CImageApplyDiscardBlank::processRectR(const cv::Mat& image, cv::RotatedRect&
rotatedRect.size.width = rotatedRect.size.height; rotatedRect.size.width = rotatedRect.size.height;
rotatedRect.size.height = l_temp; rotatedRect.size.height = l_temp;
} }
if (rotatedRect.angle > 45.0f)
{
rotatedRect.angle -= 90.0f;
float l_temp = rotatedRect.size.width;
rotatedRect.size.width = rotatedRect.size.height;
rotatedRect.size.height = l_temp;
}
std::vector<int> hull(list_com.size()); std::vector<int> hull(list_com.size());
cv::convexHull(list_com, hull); cv::convexHull(list_com, hull);
@ -91,7 +107,7 @@ int CImageApplyDiscardBlank::processRectR(const cv::Mat& image, cv::RotatedRect&
return blockCount; return blockCount;
} }
bool CImageApplyDiscardBlank::scalar_LE(const cv::Scalar& val1, const cv::Scalar& val2) bool CImageApplyDiscardBlank::scalar_LE(const cv::Scalar& val1, const cv::Scalar& val2)
{ {
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
if (val1[i] > val2[i]) if (val1[i] > val2[i])
@ -116,7 +132,7 @@ cv::Mat CImageApplyDiscardBlank::getRoiMat(const cv::Mat& image)
processRectR(image, rect, contour, scale, thresh, blobSize); processRectR(image, rect, contour, scale, thresh, blobSize);
cv::Rect rect2 = rect.boundingRect(); cv::Rect rect2 = rect.boundingRect();
cv::Rect inRect = rect2 & cv::Rect(0, 0, image.cols, image.rows); cv::Rect inRect = rect2 & cv::Rect(0, 0, image.cols, image.rows);
gap = cv::max(inRect.width - rect.size.width, inRect.height - rect.size.height) + 20; gap = cv::max(inRect.width - rect.size.width, inRect.height - rect.size.height) + 100;
inRect = cv::Rect(inRect.x + gap, inRect.y + gap, inRect.width - gap * 2, inRect.height - gap * 2); inRect = cv::Rect(inRect.x + gap, inRect.y + gap, inRect.width - gap * 2, inRect.height - gap * 2);
if (inRect.width <= 0 || inRect.height <= 0) if (inRect.width <= 0 || inRect.height <= 0)
@ -126,8 +142,15 @@ cv::Mat CImageApplyDiscardBlank::getRoiMat(const cv::Mat& image)
void CImageApplyDiscardBlank::apply(cv::Mat& pDib, int side) void CImageApplyDiscardBlank::apply(cv::Mat& pDib, int side)
{ {
#ifdef LOG
FileTools::write_log("imgprc.txt", "enter CImageApplyDiscardBlank apply");
#endif // LOG
if (pDib.empty()) if (pDib.empty())
{ {
#ifdef LOG
FileTools::write_log("imgprc.txt", "exit CImageApplyDiscardBlank apply");
#endif // LOG
return; return;
} }
@ -136,16 +159,19 @@ void CImageApplyDiscardBlank::apply(cv::Mat& pDib, int side)
cv::Mat image = getRoiMat(pDib); cv::Mat image = getRoiMat(pDib);
cv::Rect rect; cv::Rect rect;
cv::Rect imRect(0, 0, image.cols, image.rows); cv::Rect imRect(0, 0, image.cols, image.rows);
for(int i = 0; i < image.cols; i+= m_dSize) for (int i = 0; i < image.cols; i += m_dSize)
for(int j = 0; j < image.rows; j+= m_dSize) for (int j = 0; j < image.rows; j += m_dSize)
{ {
rect = cv::Rect(i, j, m_dSize, m_dSize) & imRect; rect = cv::Rect(i, j, m_dSize, m_dSize) & imRect;
if(rect != cv::Rect()) if (rect != cv::Rect())
{ {
cv::meanStdDev (image(rect) , mean, dev); cv::meanStdDev(image(rect), mean, dev);
if(!scalar_LE(dev, m_devTh)) if (!scalar_LE(dev, m_devTh))
{ {
m_res = false; m_res = false;
#ifdef LOG
FileTools::write_log("imgprc.txt", "exit CImageApplyDiscardBlank apply");
#endif // LOG
return; return;
} }
} }
@ -153,11 +179,14 @@ void CImageApplyDiscardBlank::apply(cv::Mat& pDib, int side)
m_res = true; m_res = true;
if (m_res) if (m_res)
pDib.release(); pDib.release();
#ifdef LOG
FileTools::write_log("imgprc.txt", "exit CImageApplyDiscardBlank apply");
#endif // LOG
} }
void CImageApplyDiscardBlank::apply(std::vector<cv::Mat>& mats, bool isTwoSide) void CImageApplyDiscardBlank::apply(std::vector<cv::Mat>& mats, bool isTwoSide)
{ {
(void)isTwoSide; (void)isTwoSide;
int i = 0; int i = 0;
for (cv::Mat& var : mats) { for (cv::Mat& var : mats) {
if (i != 0 && isTwoSide == false) if (i != 0 && isTwoSide == false)
@ -194,3 +223,4 @@ bool CImageApplyDiscardBlank::apply(const cv::Mat& pDib, int blockSize, int devT
} }
return true; return true;
} }

View File

@ -5,8 +5,13 @@
* *
* 2020/4/21 * 2020/4/21
* 2020/4/21 v1.0 * 2020/4/21 v1.0
2020/8/12 v1.1 setIntensity和setMinAreaisNormal标识位setIntensity的设置范围[2, 20][1, 100] 2020/8/12 v1.1 setIntensity和setMinAreaisNormal标识位setIntensity的设置范围[2, 20][1, 100]
* v1.1 2020/8/25 v1.1.1 10020
2020/10/16 v1.2 便
2020/10/19 v1.2.1 BUG
2021/04/13 v1.3.0 /
2021/08/12 v1.3.1 opencv版本导致计算结果存在差异的代码
* v1.3.1
* ==================================================== * ====================================================
*/ */
@ -19,16 +24,18 @@
class CImageApplyDiscardBlank : public CImageApply class CImageApplyDiscardBlank : public CImageApply
{ {
public: public:
CImageApplyDiscardBlank();
CImageApplyDiscardBlank(int blockSize, int devTh); CImageApplyDiscardBlank(int blockSize, int devTh);
/* /*
* isnormal [in]:true标准模式false为票据复写纸模式 * isnormal [in]:true标准模式false为票据复写纸模式
* */ * */
CImageApplyDiscardBlank(); CImageApplyDiscardBlank(bool isnormal);
virtual ~CImageApplyDiscardBlank(void); virtual ~CImageApplyDiscardBlank(void);
virtual void apply(cv::Mat& pDib,int side); virtual void apply(cv::Mat& pDib, int side);
virtual void apply(std::vector<cv::Mat>& mats, bool isTwoSide); virtual void apply(std::vector<cv::Mat>& mats, bool isTwoSide);

View File

@ -1,10 +1,10 @@
#include "ImageApplyRotation.h" #include "ImageApplyRotation.h"
#define USE_TESSERCAT #define USE_TESSERCAT
#ifdef USE_TESSERCAT //#define USE_HANWANG
//#define HG_GPDF_API_BUILD //#define HG_GPDF_API_BUILD
#include "hg_gpdf.h" #include "hg_gpdf.h"
#endif
CImageApplyRotation::CImageApplyRotation(RotationType rotation, bool isBackTransposed, int dpi, const char* tessdataPath) CImageApplyRotation::CImageApplyRotation(RotationType rotation, bool isBackTransposed, int dpi, const char* tessdataPath)
: m_rotation(rotation) : m_rotation(rotation)
@ -38,6 +38,42 @@ void CImageApplyRotation::apply(cv::Mat& pDib, int side)
if (m_rotation == RotationType::AutoTextOrientation) //<2F>Զ<EFBFBD><D4B6>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><CAB6> if (m_rotation == RotationType::AutoTextOrientation) //<2F>Զ<EFBFBD><D4B6>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><CAB6>
{ {
#ifdef USE_HANWANG
cv::Mat temp;
if (m_dpi != 200)
{
double scale = 200 / static_cast<double>(m_dpi);
int new_w = static_cast<int>(pDib.cols * scale) / 4 * 4;
int new_h = pDib.rows * scale;
cv::resize(pDib, temp, cv::Size(new_w, new_h));
}
else
temp = pDib(cv::Rect(0, 0, pDib.cols / 4 * 4, pDib.rows)).clone();
if (temp.channels() == 3)
cv::cvtColor(temp, temp, cv::COLOR_BGR2GRAY);
cv::threshold(temp, temp, 180, 255, cv::THRESH_OTSU);
int orientation = HG_OCR::orientation(temp.data, temp.cols, temp.rows, temp.channels());
switch (orientation)
{
case 90:
cv::transpose(pDib, pDib);
cv::flip(pDib, pDib, 0);
break;
case 180:
cv::flip(pDib, pDib, 0);
cv::flip(pDib, pDib, 1);
break;
case 270:
cv::transpose(pDib, pDib);
cv::flip(pDib, pDib, 1);
break;
default:
break;
}
#endif
#ifdef USE_TESSERCAT #ifdef USE_TESSERCAT
if (osd) if (osd)
{ {
@ -45,7 +81,7 @@ void CImageApplyRotation::apply(cv::Mat& pDib, int side)
if (m_dpi != 200) if (m_dpi != 200)
{ {
double scale = 200 / static_cast<double>(m_dpi); double scale = 200 / static_cast<double>(m_dpi);
int new_w = (pDib.cols * scale + 3) / 4 * 4; int new_w = (static_cast<int>(pDib.cols * scale) + 3) / 4 * 4;
int new_h = pDib.rows * scale; int new_h = pDib.rows * scale;
cv::resize(pDib, temp, cv::Size(new_w, new_h)); cv::resize(pDib, temp, cv::Size(new_w, new_h));
} }