#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include "base/HGUtility.h" #include "imgfmt/HGImgFmt.h" #include "HGUIGlobal.h" #include "HGString.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_twainDSM(nullptr) , m_twainDS(nullptr) , m_cacheUuid("") { ui->setupUi(this); HGChar cacheUuid[256] = { 0 }; HGBase_GetUuid(cacheUuid, 256); m_cacheUuid = cacheUuid; QStringList capTypes = { "TWTY_INT8", "TWTY_UINT8", "TWTY_INT16", "TWTY_UINT16", "TWTY_INT32", "TWTY_UINT32", "TWTY_BOOL","TWTY_FIX32", "TWTY_STR32", "TWTY_STR64", "TWTY_STR128", "TWTY_STR255" }; ui->comboBox_capType->addItems(capTypes); ui->comboBox_capType->setCurrentIndex(0); ui->comboBox_capType->setEditable(true); ui->label_deviceStatus->setText(tr("No device connected")); HGTwain_CreateDSM((HWND)this->winId(), &m_twainDSM); connect(this, SIGNAL(newImage(void*)), this, SLOT(on_newImage(void*)), Qt::DirectConnection); connect(this, SIGNAL(clearRes()), this, SLOT(on_clearRes()), Qt::QueuedConnection); } MainWindow::~MainWindow() { QString cachePath = getCachePath() + m_cacheUuid + "/"; QDir dir = QDir(cachePath); if (dir.exists()) dir.removeRecursively(); if (m_twainDSM != nullptr) { HGTwain_DestroyDSM(m_twainDSM); m_twainDSM = nullptr; } if (m_twainDS != nullptr) { HGTwain_CloseDS(m_twainDS); HGTwain_DestroyDS(m_twainDS); m_twainDS = nullptr; } delete ui; } void MainWindow::on_act_selectDevice_triggered() { ui->label_deviceStatus->setText(tr("No device connected")); if (m_twainDS != nullptr) { HGTwain_CloseDS(m_twainDS); HGTwain_DestroyDS(m_twainDS); m_twainDS = nullptr; } HGResult ret = HGTwain_CreateSelectedDSEx(m_twainDSM, &m_twainDS); if (nullptr != m_twainDS) { HGResult ret = HGTwain_OpenDS(m_twainDS); if (ret == HGBASE_ERR_OK) { HGTwain_LoginDS(m_twainDS, "user", "huagoscan"); ui->label_deviceStatus->setText(tr("Device %1 is open").arg(getDeviceName())); } } else { if (HGTWAIN_ERR_CANCELUI != ret) { QMessageBox::information(this, tr("Prompt"), tr("Device source not found!")); } } } void MainWindow::on_pushButton_SetCap_clicked() { QString capCode = ui->lineEdit_setCap->text(); bool ok = false; HGUShort cap = capCode.toInt(&ok, 16); HGCapValue value; QString capType = ui->comboBox_capType->currentText(); if (capType == "TWTY_BOOL") { value.type = HGCAPVALUE_TYPE_BOOL; value.valueBool = ui->lineEdit_setCapContent->text().toInt(); } HGResult ret = HGTwain_SetCapbility(m_twainDS, cap, &value, ui->checkBox_resetCap->isChecked()); if (ret == HGBASE_ERR_OK) { QMessageBox::information(this, tr("Prompt"), tr("Set successfully")); } else { QMessageBox::information(this, tr("Prompt"), tr("Set failed")); } } void MainWindow::on_pushButton_scan_clicked() { HGResult ret = HGTwain_EnableDS(m_twainDS, HGFALSE, (HWND)this->winId(), DSEventFunc, this, DSImageFunc, this); } void MainWindow::on_newImage(void *image) { HGImage img = nullptr; HGBase_CloneImage((HGImage)image, 0, 0, &img); QString scanFileName = getCacheFileName((HGImage)image); HGImgFmtSaveInfo info; info.jpegQuality = 100; info.tiffCompression = HGIMGFMT_TIFFCOMP_LZW; info.tiffJpegQuality = 0; HGResult ret = HGImgFmt_SaveImage((HGImage)image, 0, &info, getStdString(scanFileName).c_str()); } void MainWindow::on_clearRes() { HGTwain_DisableDS(m_twainDS); } void MainWindow::DSEventFunc(HGTwainDS ds, HGUInt event, HGPointer param) { MainWindow* p = (MainWindow*)param; if (HGTWAIN_EVENT_TYPE_SCANFINISHED == event || HGTWAIN_EVENT_TYPE_CLOSEDSREQ == event) { emit p->clearRes(); } } HGUInt MainWindow::DSImageFunc(HGTwainDS ds, HGImage image, HGUInt type, HGPointer param) { MainWindow* p = (MainWindow*)param; emit p->newImage(image); return HGBASE_ERR_OK; } QString MainWindow::getDeviceName() { HGChar devName[256] = {0}; HGTwain_GetDSDeviceName(m_twainDS, devName, 256); return QString(devName); } QString MainWindow::getCachePath() { HGChar cachePath[512]; HGBase_GetDocumentsPath(cachePath, 512); HGChar procName[512]; HGBase_GetProcessName(procName, 512); strcat(cachePath, procName); strcat(cachePath, "/Cache/"); return getStdFileName(StdStringToUtf8(cachePath).c_str()); } QString MainWindow::getCacheFileName(HGImage img) { assert(nullptr != img); HGImageInfo imgInfo; HGBase_GetImageInfo(img, &imgInfo); QString cachePath = getCachePath() + m_cacheUuid + "/"; HGBase_CreateDir(getStdString(cachePath).c_str()); char uuid[256] = {0}; HGBase_GetUuid(uuid, 256); QString suffix = (HGBASE_IMGTYPE_BINARY == imgInfo.type) ? ".bmp" : ".jpg"; QString fileName = getStdFileName(cachePath + uuid + suffix); return fileName; }