code_app/app/scanner2/device_user.cpp

401 lines
9.0 KiB
C++

#include "device_user.h"
#include <QMessageBox>
#include <QFileDialog>
#include "HGUIGlobal.h"
#include <thread>
#if defined(HG_CMP_MSC)
DeviceUserMgr::DeviceUserMgr(const QString &password, QWidget *wnd)
{
m_password = password;
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);
if (nullptr == ds)
{
if (HGTWAIN_ERR_CANCELUI != ret)
{
QMessageBox::information(m_wnd, tr("Prompt"), tr("Device source not found!"));
}
return nullptr;
}
return new DeviceUser(m_wnd, ds, m_password);
}
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);
}
DeviceUser::DeviceUser(QWidget *wnd, HGTwainDS ds, const QString &password)
{
m_wnd = wnd;
m_twainDS = ds;
m_password = password;
}
DeviceUser::~DeviceUser()
{
HGTwain_CloseDS(m_twainDS);
HGTwain_DestroyDS(m_twainDS);
m_twainDS = nullptr;
}
HGResult DeviceUser::Open()
{
return HGTwain_OpenDS(m_twainDS);
}
HGResult DeviceUser::OpenDefault()
{
return HGTwain_OpenDS(m_twainDS);
}
HGResult DeviceUser::Close()
{
return HGTwain_CloseDS(m_twainDS);
}
QString DeviceUser::GetName()
{
HGChar devName[256] = {0};
HGTwain_GetDSDeviceName(m_twainDS, devName, 256);
return QString(devName);
}
HGResult DeviceUser::ShowSettingDlg()
{
return HGTwain_EnableDSUIOnly(m_twainDS, (HWND)m_wnd->winId(), DSEventFunc, this);
}
HGResult DeviceUser::StartScan()
{
return HGTwain_EnableDS(m_twainDS, HGFALSE, (HWND)m_wnd->winId(), DSEventFunc, this, DSImageFunc, this);
}
HGResult DeviceUser::StartSingleScan()
{
return HGTwain_EnableDSWithSingleScan(m_twainDS, (HWND)m_wnd->winId(), DSEventFunc, this, DSImageFunc, this);
}
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);
}
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());
if (ret == HGBASE_ERR_OK)
return fileName;
return "Fail";
}
HGResult DeviceUser::ClearDriverLog()
{
return HGTwain_ClearDSDriverLog(m_twainDS);
}
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());
if (ret == HGBASE_ERR_OK)
return fileName;
return "Fail";
}
HGResult DeviceUser::ClearDeviceLog()
{
return HGTwain_ClearDSDeviceLog(m_twainDS);
}
void HGAPI DeviceUser::DSEventFunc(HGTwainDS ds, HGUInt event, HGPointer param)
{
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();
}
else if (HGTWAIN_EVENT_TYPE_CLOSEDSREQ == event)
{
HGTwain_DisableDS(p->m_twainDS);
emit p->closeDSReq();
}
}
HGUInt HGAPI DeviceUser::DSImageFunc(HGTwainDS ds, HGImage image, HGUInt type, HGPointer param)
{
DeviceUser* p = (DeviceUser*)param;
emit p->newImage(image);
return HGBASE_ERR_OK;
}
#else
DeviceUserMgr::DeviceUserMgr(const QString &password, QWidget *wnd)
{
m_wnd = wnd;
m_password = password;
m_saneMgr = nullptr;
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);
if (nullptr == source)
return nullptr;
return new DeviceUser(m_wnd, source, m_password);
}
DeviceUser *DeviceUserMgr::OpenDefaultDeviceUser()
{
HGSaneSource source = nullptr;
HGSane_OpenDefaultSource(m_saneMgr, &source);
if (nullptr == source)
return nullptr;
return new DeviceUser(m_wnd, source, m_password);
}
DeviceUser::DeviceUser(QWidget *wnd, HGSaneSource source, const QString &password)
{
m_wnd = wnd;
m_source = source;
m_saneDev = nullptr;
m_password = password;
}
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);
}
}
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);
}
}
HGResult DeviceUser::Close()
{
if (nullptr == m_saneDev)
{
return HGSANE_ERR_FAIL;
}
HGSane_CloseDevice(m_saneDev);
m_saneDev = nullptr;
return HGBASE_ERR_OK;
}
QString DeviceUser::GetName()
{
HGChar devName[256];
HGSane_GetDeviceName(m_saneDev, devName, 256);
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);
}
HGResult DeviceUser::StartSingleScan()
{
return HGSane_StartDeviceWithSingleScan(m_saneDev, m_wnd, DeviceEventFunc, this, DeviceImageFunc, this);
}
HGResult DeviceUser::GetDeviceCustomInfo(HGSaneDeviceCustomInfo *info)
{
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);
}
int DeviceUser::GetDeviceRollerLife()
{
int rollerLife = 0;
HGSane_GetDeviceRollerLife(m_saneDev, &rollerLife);
return rollerLife;
}
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 = HGSane_GetDriverLog(m_saneDev, f.c_str());
if (ret == HGBASE_ERR_OK)
return fileName;
return "Fail";
}
HGResult DeviceUser::ClearDriverLog()
{
return HGSane_ClearDriverLog(m_saneDev);
}
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 = HGSane_GetDeviceLog(m_saneDev, f.c_str());
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)
{
DeviceUser* p = (DeviceUser*)param;
if (HGSANE_EVENT_TYPE_SCANFINISHED == event)
{
emit p->scanFinishEvent();
}
else if (HGSANE_EVENT_TYPE_WORKING == event)
{
emit p->scanWorkingEvent();
}
}
HGUInt HGAPI DeviceUser::DeviceImageFunc(HGSaneDevice dev, HGImage image, HGUInt type, HGPointer param)
{
DeviceUser* p = (DeviceUser*)param;
if (type != HGSANE_IMAGE_TYPE_DOUBLE)
{
emit p->newImage(image);
return HGBASE_ERR_OK;
}
HGUInt imgRet = HGBASE_ERR_OK;
emit p->abnormalImage(image, &imgRet);
if (HGSANE_ERR_IMAGE_RESERVE == imgRet)
{
emit p->newImage(image);
}
return imgRet;
}
#endif