code_app/app/scanner2/device_user.cpp

396 lines
8.8 KiB
C++
Raw Normal View History

#include "device_user.h"
2023-05-04 06:55:26 +00:00
#include <QMessageBox>
#include <QFileDialog>
#include "HGUIGlobal.h"
#include <thread>
2023-05-04 06:55:26 +00:00
#if defined(HG_CMP_MSC)
DeviceUserMgr::DeviceUserMgr(const QString &password, QWidget *wnd)
2023-05-04 06:55:26 +00:00
{
m_password = password;
2023-05-04 06:55:26 +00:00
m_wnd = wnd;
m_twainDSM = nullptr;
HGTwain_CreateDSM((HWND)m_wnd->winId(), &m_twainDSM);
}
DeviceUserMgr::~DeviceUserMgr()
{
HGTwain_DestroyDSM(m_twainDSM);
m_twainDSM = nullptr;
}
class DeviceUser* DeviceUserMgr::OpenDeviceUser()
{
HGTwainDS ds = nullptr;
HGResult ret = HGTwain_CreateSelectedDSEx(m_twainDSM, &ds);
2023-05-04 06:55:26 +00:00
if (nullptr == ds)
{
if (HGTWAIN_ERR_CANCELUI != ret)
{
QMessageBox::information(m_wnd, tr("Prompt"), tr("Device source not found!"));
}
2023-05-04 06:55:26 +00:00
return nullptr;
}
return new DeviceUser(m_wnd, ds, m_password);
2023-05-04 06:55:26 +00:00
}
2023-05-30 11:11:24 +00:00
DeviceUser *DeviceUserMgr::OpenDefaultDeviceUser()
{
HGTwainDS ds = nullptr;
HGTwain_CreateDefaultDS(m_twainDSM, &ds);
if (nullptr == ds)
{
QMessageBox::information(m_wnd, tr("Prompt"), tr("Device source not found!"));
return nullptr;
}
return new DeviceUser(m_wnd, ds, m_password);
2023-05-30 11:11:24 +00:00
}
DeviceUser::DeviceUser(QWidget *wnd, HGTwainDS ds, const QString &password)
2023-05-04 06:55:26 +00:00
{
m_wnd = wnd;
m_twainDS = ds;
m_password = password;
2023-05-04 06:55:26 +00:00
}
DeviceUser::~DeviceUser()
{
HGTwain_CloseDS(m_twainDS);
HGTwain_DestroyDS(m_twainDS);
2023-05-04 06:55:26 +00:00
m_twainDS = nullptr;
}
HGResult DeviceUser::Open()
{
return HGTwain_OpenDS(m_twainDS);
}
2023-08-15 07:38:21 +00:00
HGResult DeviceUser::OpenDefault()
{
return HGTwain_OpenDS(m_twainDS);
}
HGResult DeviceUser::Close()
{
return HGTwain_CloseDS(m_twainDS);
}
2023-05-04 06:55:26 +00:00
QString DeviceUser::GetName()
{
2023-06-01 07:38:58 +00:00
HGChar devName[256] = {0};
2023-05-31 02:12:38 +00:00
HGTwain_GetDSDeviceName(m_twainDS, devName, 256);
2023-05-04 06:55:26 +00:00
return QString(devName);
}
HGResult DeviceUser::ShowSettingDlg()
{
return HGTwain_EnableDSUIOnly(m_twainDS, (HWND)m_wnd->winId(), DSEventFunc, this);
2023-05-04 06:55:26 +00:00
}
HGResult DeviceUser::StartScan()
{
return HGTwain_EnableDS(m_twainDS, HGFALSE, (HWND)m_wnd->winId(), DSEventFunc, this, DSImageFunc, this);
2023-05-04 06:55:26 +00:00
}
HGResult DeviceUser::StartSingleScan()
{
2023-05-20 08:27:48 +00:00
return HGTwain_EnableDSWithSingleScan(m_twainDS, (HWND)m_wnd->winId(), DSEventFunc, this, DSImageFunc, this);
}
2023-05-04 06:55:26 +00:00
HGResult DeviceUser::GetDeviceCustomInfo(HGTwainDeviceCustomInfo *info)
{
return HGTwain_GetDSDeviceCustomInfo(m_twainDS, info);
}
HGResult DeviceUser::Login()
{
return HGTwain_LoginDS(m_twainDS, "user", getStdString(m_password).c_str());
}
HGResult DeviceUser::Logout()
{
return HGTwain_LogoutDS(m_twainDS);
}
HGResult DeviceUser::ClearRollerCount()
{
return HGTwain_ClearDSRollerCount(m_twainDS);
}
2023-05-17 10:29:08 +00:00
QString DeviceUser::GetDriverLog()
{
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
std::string f = getStdFileName(fileName).toLocal8Bit().data();
HGResult ret = HGTwain_GetDSDriverLog(m_twainDS, f.c_str());
2023-05-17 10:29:08 +00:00
if (ret == HGBASE_ERR_OK)
return fileName;
2023-05-17 10:29:08 +00:00
return "Fail";
}
HGResult DeviceUser::ClearDriverLog()
{
return HGTwain_ClearDSDriverLog(m_twainDS);
}
2023-05-17 10:29:08 +00:00
QString DeviceUser::GetDeviceLog()
{
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
std::string f = getStdFileName(fileName).toLocal8Bit().data();
HGResult ret = HGTwain_GetDSDeviceLog(m_twainDS, f.c_str());
2023-05-17 10:29:08 +00:00
if (ret == HGBASE_ERR_OK)
return fileName;
2023-05-17 10:29:08 +00:00
return "Fail";
}
HGResult DeviceUser::ClearDeviceLog()
{
return HGTwain_ClearDSDeviceLog(m_twainDS);
}
void HGAPI DeviceUser::DSEventFunc(HGTwainDS ds, HGUInt event, HGPointer param)
2023-05-04 06:55:26 +00:00
{
DeviceUser* p = (DeviceUser*)param;
if (HGTWAIN_EVENT_TYPE_SCANFINISHED == event)
{
HGTwain_DisableDS(p->m_twainDS);
emit p->scanFinishEvent();
}
else if (HGTWAIN_EVENT_TYPE_WORKING == event)
{
emit p->scanWorkingEvent();
}
2023-05-04 06:55:26 +00:00
}
2023-10-16 02:38:35 +00:00
HGUInt HGAPI DeviceUser::DSImageFunc(HGTwainDS ds, HGImage image, HGUInt type, HGPointer param)
2023-05-04 06:55:26 +00:00
{
DeviceUser* p = (DeviceUser*)param;
emit p->newImage(image);
2023-10-16 02:38:35 +00:00
return HGBASE_ERR_OK;
}
2023-05-04 06:55:26 +00:00
#else
DeviceUserMgr::DeviceUserMgr(const QString &password, QWidget *wnd)
2023-05-04 06:55:26 +00:00
{
m_wnd = wnd;
m_password = password;
m_saneMgr = nullptr;
2023-05-04 06:55:26 +00:00
HGSane_CreateManager(&m_saneMgr);
}
DeviceUserMgr::~DeviceUserMgr()
{
HGSane_DestroyManager(m_saneMgr);
m_saneMgr = nullptr;
}
class DeviceUser* DeviceUserMgr::OpenDeviceUser()
{
HGSaneSource source = nullptr;
HGSane_OpenSource(m_saneMgr, 0, &source);
2023-05-04 06:55:26 +00:00
if (nullptr == source)
return nullptr;
return new DeviceUser(m_wnd, source, m_password);
2023-05-04 06:55:26 +00:00
}
2023-06-03 09:25:56 +00:00
DeviceUser *DeviceUserMgr::OpenDefaultDeviceUser()
{
HGSaneSource source = nullptr;
HGSane_OpenDefaultSource(m_saneMgr, &source);
if (nullptr == source)
return nullptr;
return new DeviceUser(m_wnd, source, m_password);
2023-06-03 09:25:56 +00:00
}
DeviceUser::DeviceUser(QWidget *wnd, HGSaneSource source, const QString &password)
2023-05-04 06:55:26 +00:00
{
m_wnd = wnd;
m_source = source;
m_saneDev = nullptr;
m_password = password;
2023-05-04 06:55:26 +00:00
}
DeviceUser::~DeviceUser()
{
HGSane_CloseDevice(m_saneDev);
m_saneDev = nullptr;
HGSane_CloseSource(m_source);
m_source = nullptr;
}
HGResult DeviceUser::Open()
{
if (nullptr != m_saneDev)
{
return HGSANE_ERR_FAIL;
}
HGUInt count = 0;
HGSane_GetDeviceCount(m_source, &count);
if (0 == count)
{
QMessageBox::information(m_wnd, tr("Prompt"), tr("Scanner not found!"));
return HGSANE_ERR_FAIL;
}
else
{
return HGSane_OpenSelectedDevice(m_source, m_wnd, &m_saneDev);
}
}
2023-08-15 07:38:21 +00:00
HGResult DeviceUser::OpenDefault()
{
if (nullptr != m_saneDev)
{
return HGSANE_ERR_FAIL;
}
HGUInt count = 0;
HGSane_GetDeviceCount(m_source, &count);
if (0 == count)
{
QMessageBox::information(m_wnd, tr("Prompt"), tr("Scanner not found!"));
return HGSANE_ERR_FAIL;
}
else
{
HGChar errInfo[256];
return HGSane_OpenDevice(m_source, 0, &m_saneDev, errInfo, 256);
}
2023-08-15 07:38:21 +00:00
}
HGResult DeviceUser::Close()
{
if (nullptr == m_saneDev)
{
return HGSANE_ERR_FAIL;
}
HGSane_CloseDevice(m_saneDev);
m_saneDev = nullptr;
return HGBASE_ERR_OK;
}
2023-05-04 06:55:26 +00:00
QString DeviceUser::GetName()
{
HGChar devName[256];
2023-05-31 02:12:38 +00:00
HGSane_GetDeviceName(m_saneDev, devName, 256);
2023-05-04 06:55:26 +00:00
return QString(devName);
}
HGResult DeviceUser::ShowSettingDlg()
{
return HGSane_ShowDeviceSettingDlg(m_saneDev, m_wnd);
}
HGResult DeviceUser::StartScan()
{
return HGSane_StartDevice(m_saneDev, m_wnd, DeviceEventFunc, this, DeviceImageFunc, this);
2023-05-04 06:55:26 +00:00
}
2023-05-20 10:06:05 +00:00
HGResult DeviceUser::StartSingleScan()
{
return HGSane_StartDeviceWithSingleScan(m_saneDev, m_wnd, DeviceEventFunc, this, DeviceImageFunc, this);
}
2023-05-20 10:06:05 +00:00
HGResult DeviceUser::GetDeviceCustomInfo(HGSaneDeviceCustomInfo *info)
2023-05-04 06:55:26 +00:00
{
return HGSane_GetDeviceCustomInfo(m_saneDev, info);
}
HGResult DeviceUser::Login()
{
return HGSane_Login(m_saneDev, "user", getStdString(m_password).c_str());
}
HGResult DeviceUser::Logout()
{
return HGSane_Logout(m_saneDev);
}
HGResult DeviceUser::ClearRollerCount()
{
return HGSane_ClearRollerCount(m_saneDev);
}
2023-06-13 07:24:58 +00:00
int DeviceUser::GetDeviceRollerLife()
{
int rollerLife = 0;
HGSane_GetDeviceRollerLife(m_saneDev, &rollerLife);
return rollerLife;
}
2023-05-20 10:06:05 +00:00
QString DeviceUser::GetDriverLog()
{
2023-05-20 10:06:05 +00:00
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
std::string f = getStdFileName(fileName).toLocal8Bit().data();
HGResult ret = HGSane_GetDriverLog(m_saneDev, f.c_str());
2023-05-20 10:06:05 +00:00
if (ret == HGBASE_ERR_OK)
return fileName;
return "Fail";
}
HGResult DeviceUser::ClearDriverLog()
{
return HGSane_ClearDriverLog(m_saneDev);
}
2023-05-20 10:06:05 +00:00
QString DeviceUser::GetDeviceLog()
{
2023-05-20 10:06:05 +00:00
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
std::string f = getStdFileName(fileName).toLocal8Bit().data();
HGResult ret = HGSane_GetDeviceLog(m_saneDev, f.c_str());
2023-05-20 10:06:05 +00:00
if (ret == HGBASE_ERR_OK)
return fileName;
return "Fail";
}
HGResult DeviceUser::ClearDeviceLog()
{
return HGSane_ClearDeviceLog(m_saneDev);
}
void HGAPI DeviceUser::DeviceEventFunc(HGSaneDevice dev, HGUInt event, HGPointer param)
{
2023-06-03 09:25:56 +00:00
DeviceUser* p = (DeviceUser*)param;
if (HGSANE_EVENT_TYPE_SCANFINISHED == event)
{
emit p->scanFinishEvent();
}
else if (HGSANE_EVENT_TYPE_WORKING == event)
{
emit p->scanWorkingEvent();
}
}
2023-10-16 02:31:37 +00:00
HGUInt HGAPI DeviceUser::DeviceImageFunc(HGSaneDevice dev, HGImage image, HGUInt type, HGPointer param)
2023-05-04 06:55:26 +00:00
{
DeviceUser* p = (DeviceUser*)param;
if (type != HGSANE_IMAGE_TYPE_DOUBLE)
2023-10-17 02:47:24 +00:00
{
emit p->newImage(image);
return HGBASE_ERR_OK;
2023-10-17 02:47:24 +00:00
}
HGUInt imgRet = HGBASE_ERR_OK;
emit p->abnormalImage(image, &imgRet);
if (HGSANE_ERR_IMAGE_RESERVE == imgRet)
2023-10-17 02:47:24 +00:00
{
emit p->newImage(image);
}
return imgRet;
2023-05-04 06:55:26 +00:00
}
#endif