scantool增加配置互斥功能

This commit is contained in:
luoliangyi 2024-04-28 18:15:51 +08:00
parent f42d57228a
commit 1b2940efd8
12 changed files with 1016 additions and 1078 deletions

View File

@ -14,7 +14,10 @@ Dialog_Add::Dialog_Add(SANE_Handle devHandle, class MainWindow *mainWnd, const s
this->setWindowTitle(tr("Add") + " (" + QString(deviceType.c_str()) + ")"); this->setWindowTitle(tr("Add") + " (" + QString(deviceType.c_str()) + ")");
ui->comboBoxButtonId->addItem("Manual"); ui->comboBoxButtonId->addItem("Manual");
// TODO 通过devHandle获取支持的按钮并添加到下拉列表 SANE_Int buttonCount = 0;
sane_control_option(devHandle, (SANE_Int)0x886E, SANE_ACTION_GET_VALUE, &buttonCount, NULL);
for (SANE_Int i = 0; i < buttonCount; ++i)
ui->comboBoxButtonId->addItem(tr("Button ") + QString::number(i + 1));
m_scanParam.deviceType = deviceType; m_scanParam.deviceType = deviceType;
m_scanParam.buttonId = ui->comboBoxButtonId->currentIndex(); m_scanParam.buttonId = ui->comboBoxButtonId->currentIndex();
@ -39,7 +42,10 @@ Dialog_Add::Dialog_Add(SANE_Handle devHandle, class MainWindow *mainWnd, const S
this->setWindowTitle(tr("Modify") + " (" + QString(scanParam.deviceType.c_str()) + ")"); this->setWindowTitle(tr("Modify") + " (" + QString(scanParam.deviceType.c_str()) + ")");
ui->comboBoxButtonId->addItem("Manual"); ui->comboBoxButtonId->addItem("Manual");
// TODO 通过devHandle获取支持的按钮并添加到下拉列表 SANE_Int buttonCount = 0;
sane_control_option(devHandle, (SANE_Int)0x886E, SANE_ACTION_GET_VALUE, &buttonCount, NULL);
for (SANE_Int i = 0; i < buttonCount; ++i)
ui->comboBoxButtonId->addItem(tr("Button ") + QString::number(i + 1));
m_scanParam = scanParam; m_scanParam = scanParam;
ui->comboBoxButtonId->setCurrentIndex(m_scanParam.buttonId); ui->comboBoxButtonId->setCurrentIndex(m_scanParam.buttonId);

View File

@ -1,701 +0,0 @@
#include "dialog_scan.h"
#include "ui_dialog_scan.h"
#include <QCloseEvent>
#include <QMessageBox>
#include <QDateTime>
#include <QFileInfo>
#include "base/HGTime.h"
#include "base/HGUtility.h"
#include "imgproc/HGImgProc.h"
#include "imgproc/HGOCR.h"
#include "HGUIGlobal.h"
#include "dialog_scaninfo.h"
Dialog_Scan::Dialog_Scan(class MainWindow *mainWnd)
: QDialog(nullptr)
, ui(new Ui::Dialog_Scan)
, m_mainWnd(mainWnd)
, m_dlgScanInfo(nullptr)
, m_devHandle(nullptr)
, m_scanning(false)
, m_dpi(200)
, m_scanFileName("")
, m_scanImgFmtWriter(nullptr)
, m_ocrMsgPump(nullptr)
, m_ocrThread(nullptr)
{
ui->setupUi(this);
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
m_dlgScanInfo = new Dialog_ScanInfo(this);
connect(this, SIGNAL(deviceArrive(QString)), this, SLOT(on_deviceArrive(QString)));
connect(this, SIGNAL(deviceRemove(QString)), this, SLOT(on_deviceRemove(QString)));
connect(this, SIGNAL(keyPress(unsigned int)), this, SLOT(on_keyPress(unsigned int)));
connect(this, SIGNAL(scanWorking()), this, SLOT(on_scanWorking()));
connect(this, SIGNAL(scanInfo(QString, bool)), this, SLOT(on_scanInfo(QString, bool)));
connect(this, SIGNAL(scanImage(unsigned int)), this, SLOT(on_scanImage(unsigned int)));
connect(this, SIGNAL(scanFinish()), this, SLOT(on_scanFinish()));
SANE_Int version_code = 0;
sane_init_ex(&version_code, sane_ex_callback, this);
}
Dialog_Scan::~Dialog_Scan()
{
if (NULL != m_devHandle)
{
StopScan();
sane_close(m_devHandle);
m_devHandle = NULL;
m_devName.clear();
}
sane_exit();
delete m_dlgScanInfo;
delete ui;
}
void Dialog_Scan::StartScan(unsigned int buttonId)
{
if (nullptr == m_devHandle)
{
QMessageBox::information(this, tr("Tips"), tr("Device is offline"));
return;
}
if (m_scanning)
{
return;
}
std::string deviceType = m_devName.toStdString();
char v[256] = {0};
SANE_Status status = sane_control_option(m_devHandle, (SANE_Int)0x886D, SANE_ACTION_GET_VALUE, v, NULL);
if (SANE_STATUS_GOOD == status)
{
deviceType = v;
}
ScanParam scanParam;
if (!m_mainWnd->GetScanParam(deviceType, buttonId, scanParam))
{
QMessageBox::information(this, tr("Tips"), tr("No key scanning configuration found"));
return;
}
// 1.恢复默认
SANE_Int num_dev_options = 0;
sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(SANE_STD_OPT_NAME_RESTORE, name) && SANE_TYPE_BUTTON == desp->type)
{
sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, NULL, NULL);
break;
}
}
// 2.设置新的属性
for (int i = 0; i < (int)scanParam.deviceConfigs.size(); ++i)
{
for (int j = 1; j < num_dev_options; ++j)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, j);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(scanParam.deviceConfigs[i].name.c_str(), name))
{
if (SANE_TYPE_STRING == desp->type)
{
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, (void*)scanParam.deviceConfigs[i].stringValue.c_str(), NULL);
}
else if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = scanParam.deviceConfigs[i].intValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_FIXED == desp->type)
{
SANE_Fixed value = SANE_FIX(scanParam.deviceConfigs[i].doubleValue);
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_BOOL == desp->type)
{
SANE_Bool value = (SANE_Bool)scanParam.deviceConfigs[i].boolValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
break;
}
}
}
// 3. 获取DPI
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (nullptr == desp)
continue;
if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = 0;
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, nullptr);
if (0 == strcmp(desp->name, SANE_STD_OPT_NAME_RESOLUTION))
{
m_dpi = (HGUInt)value;
}
}
}
// 4.保存配置
assert(m_scanFileName.isEmpty());
assert(nullptr == m_scanImgFmtWriter);
m_aquireIntoSaveParam = scanParam.saveParam;
m_aquireIntoInBlank = true;
m_aquireIntoBatchStartIndex = 0;
m_aquireIntoPageIndex = 0;
m_aquireIntoMultiPageCount = 0;
QDateTime dateTime = QDateTime::currentDateTime();
if (m_aquireIntoSaveParam.m_isUseSubfolderByTime)
{
m_aquireIntoSaveParam.m_savePath = getStdFileName(m_aquireIntoSaveParam.m_savePath + dateTime.toString("yyyy-MM-dd") + "/");
}
if (m_aquireIntoSaveParam.m_isOcr)
{
HGBase_CreateMsgPump(&m_ocrMsgPump);
HGBase_OpenThread(ocrThreadFunc, this, &m_ocrThread);
}
m_scanning = true;
m_mainWnd->SetBusy(true);
ui->comboBox->setEnabled(false);
ui->pushButtonScan->setEnabled(false);
status = sane_start(m_devHandle);
if (SANE_STATUS_GOOD != status)
{
if (NULL != m_ocrMsgPump)
{
HGBase_ExitMsgPump(m_ocrMsgPump);
HGBase_CloseThread(m_ocrThread);
m_ocrThread = NULL;
HGBase_DestroyMsgPump(m_ocrMsgPump);
m_ocrMsgPump = NULL;
}
m_scanning = false;
m_mainWnd->SetBusy(false);
m_dpi = 200;
ui->comboBox->setEnabled(true);
ui->pushButtonScan->setEnabled(true);
emit scanWorking();
emit scanInfo((const char*)sane_strstatus(status), true);
emit scanFinish();
return;
}
}
void Dialog_Scan::StopScan()
{
if (m_scanning)
{
assert(NULL != m_devHandle);
sane_cancel(m_devHandle);
m_scanning = false;
m_mainWnd->SetBusy(false);
m_dpi = 200;
ui->comboBox->setEnabled(true);
ui->pushButtonScan->setEnabled(true);
}
}
void Dialog_Scan::SaveImage(HGImage image)
{
if (m_aquireIntoSaveParam.m_isSaveAsMultiPage)
{
if (nullptr == m_scanImgFmtWriter)
{
assert(m_scanFileName.isEmpty());
HGBase_CreateDir(getStdString(m_aquireIntoSaveParam.m_savePath).c_str());
QString scanFileName;
while (1)
{
scanFileName = m_aquireIntoSaveParam.m_savePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2")
.arg(m_aquireIntoSaveParam.m_fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0'))
.arg(m_aquireIntoSaveParam.m_fileNameExt);
QFileInfo fileInfo(scanFileName);
if (fileInfo.isFile())
{
++m_aquireIntoSaveParam.m_fileNameStartIndex;
}
else
{
break;
}
}
HGUInt fmtType = 0;
if (nullptr != m_ocrMsgPump)
{
fmtType = HGIMGFMT_TYPE_TIFF;
}
HGImgFmt_OpenImageWriter(getStdString(getStdFileName(scanFileName)).c_str(), fmtType, &m_scanImgFmtWriter);
if (nullptr != m_scanImgFmtWriter)
{
m_scanFileName = scanFileName;
}
}
if (nullptr != m_scanImgFmtWriter)
{
HGImgFmtSaveInfo saveInfo;
saveInfo.jpegQuality = (HGUInt)m_aquireIntoSaveParam.m_jpegQuality;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = (HGUInt)m_aquireIntoSaveParam.m_tiffQuality;
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
if (HGBASE_IMGTYPE_BINARY == imgInfo.type)
{
if (1 == m_aquireIntoSaveParam.m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == m_aquireIntoSaveParam.m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_CCITTFAX4;
}
else
{
if (1 == m_aquireIntoSaveParam.m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == m_aquireIntoSaveParam.m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_JPEG;
}
if (nullptr != m_ocrMsgPump)
{
saveInfo.jpegQuality = 100;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = 100;
}
if (HGBASE_ERR_OK == HGImgFmt_SaveImageToWriter(m_scanImgFmtWriter, image, &saveInfo))
{
++m_aquireIntoMultiPageCount;
if (1 == m_aquireIntoSaveParam.m_multiPagesType && m_aquireIntoMultiPageCount == m_aquireIntoSaveParam.m_customMultiPages)
{
HGImgFmt_CloseImageWriter(m_scanImgFmtWriter);
m_scanImgFmtWriter = nullptr;
if (nullptr != m_ocrMsgPump)
{
QString *filePath = new QString(m_scanFileName);
HGMsg msg;
msg.id = 1;
msg.data = filePath;
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_ocrMsgPump, &msg))
{
delete filePath;
}
}
m_scanFileName.clear();
++m_aquireIntoSaveParam.m_fileNameStartIndex;
m_aquireIntoMultiPageCount = 0;
}
}
}
}
else
{
assert(nullptr == m_scanImgFmtWriter);
QString savePath = m_aquireIntoSaveParam.m_savePath;
if (m_aquireIntoSaveParam.m_isUseSubfolderByBlankPages)
{
HGBool isBlank = HGFALSE;
HGImgProc_ImageBlankCheck(image, nullptr, &isBlank);
if (isBlank)
{
m_aquireIntoInBlank = true;
}
else
{
if (m_aquireIntoInBlank)
{
++m_aquireIntoBatchStartIndex;
}
m_aquireIntoInBlank = false;
}
char batchDir[20];
sprintf(batchDir, "batch%d", m_aquireIntoBatchStartIndex);
savePath = getStdFileName(savePath + batchDir + "/");
}
if (m_aquireIntoSaveParam.m_isUseSubfolderByColor)
{
QString colorModeName;
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
if (HGBASE_IMGTYPE_BINARY == imgInfo.type)
colorModeName = tr("binary");
else if (HGBASE_IMGTYPE_GRAY == imgInfo.type)
colorModeName = tr("gray");
else
colorModeName = tr("rgb");
savePath = getStdFileName(savePath + colorModeName + "/");
}
HGBase_CreateDir(getStdString(savePath).c_str());
while (1)
{
m_scanFileName = savePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2")
.arg(m_aquireIntoSaveParam.m_fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0'))
.arg(m_aquireIntoSaveParam.m_fileNameExt);
QFileInfo fileInfo(m_scanFileName);
if (fileInfo.isFile())
{
++m_aquireIntoSaveParam.m_fileNameStartIndex;
}
else
{
break;
}
}
HGImgFmtSaveInfo saveInfo;
saveInfo.jpegQuality = (HGUInt)m_aquireIntoSaveParam.m_jpegQuality;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = (HGUInt)m_aquireIntoSaveParam.m_tiffQuality;
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
if (HGBASE_IMGTYPE_BINARY == imgInfo.type)
{
if (1 == m_aquireIntoSaveParam.m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == m_aquireIntoSaveParam.m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_CCITTFAX4;
}
else
{
if (1 == m_aquireIntoSaveParam.m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == m_aquireIntoSaveParam.m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_JPEG;
}
HGUInt fmtType = 0;
if (nullptr != m_ocrMsgPump)
{
fmtType = HGIMGFMT_TYPE_TIFF;
saveInfo.jpegQuality = 100;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = 100;
}
if (HGBASE_ERR_OK == HGImgFmt_SaveImage(image, fmtType, &saveInfo, getStdString(m_scanFileName).c_str()))
{
if (nullptr != m_ocrMsgPump)
{
QString *filePath = new QString(m_scanFileName);
HGMsg msg;
msg.id = 1;
msg.data = filePath;
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_ocrMsgPump, &msg))
{
delete filePath;
}
}
++m_aquireIntoSaveParam.m_fileNameStartIndex;
}
m_scanFileName.clear();
}
}
int Dialog_Scan::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param)
{
(void)hdev;
(void)len;
Dialog_Scan* p = (Dialog_Scan*)param;
switch (code)
{
case SANE_EVENT_DEVICE_ARRIVED:
{
SANE_Device* sane_dev = (SANE_Device*)data;
emit p->deviceArrive(sane_dev->name);
}
break;
case SANE_EVENT_DEVICE_LEFT:
{
SANE_Device* sane_dev = (SANE_Device*)data;
emit p->deviceRemove(sane_dev->name);
}
break;
case SANE_EVENT_WORKING:
{
emit p->scanWorking();
emit p->scanInfo((const char*)data, false);
}
break;
case SANE_EVENT_SCAN_FINISHED:
{
emit p->scanInfo((const char*)data, (0 != *len));
emit p->scanFinish();
}
break;
case SANE_EVENT_STATUS:
{
//emit p->scanInfo((const char*)data, false);
}
break;
case SANE_EVENT_ERROR:
{
//emit p->scanInfo((const char*)data, (0 != *len));
}
break;
case SANE_EVENT_DEV_KEY_PRESSED:
{
emit p->keyPress(*len);
}
break;
case SANE_EVENT_IMAGE_OK:
{
++p->m_aquireIntoPageIndex;
emit p->scanImage(p->m_aquireIntoPageIndex);
if ((1 == p->m_aquireIntoSaveParam.m_fileNameOddEventType && 1 != p->m_aquireIntoPageIndex % 2)
|| (2 == p->m_aquireIntoSaveParam.m_fileNameOddEventType && 0 != p->m_aquireIntoPageIndex % 2))
{
// 跳过
}
else
{
SANE_Image* sane_img = (SANE_Image*)data;
HGUInt imgType = 0;
if (sane_img->header.format == SANE_FRAME_GRAY)
{
if (1 == sane_img->header.depth)
imgType = HGBASE_IMGTYPE_BINARY;
else if (8 == sane_img->header.depth)
imgType = HGBASE_IMGTYPE_GRAY;
}
else if (sane_img->header.format == SANE_FRAME_RGB)
imgType = HGBASE_IMGTYPE_RGB;
HGByte* data = sane_img->data;
HGImageInfo imgInfo = { (HGUInt)sane_img->header.pixels_per_line, (HGUInt)sane_img->header.lines,
imgType, (HGUInt)sane_img->header.bytes_per_line, HGBASE_IMGORIGIN_TOP };
HGImage img = NULL;
HGBase_CreateImageFromData(data, &imgInfo, NULL, 0, HGBASE_IMGORIGIN_TOP, &img);
if (NULL != img)
{
HGBase_SetImageDpi(img, p->m_dpi, p->m_dpi);
p->SaveImage(img);
HGBase_DestroyImage(img);
}
}
}
break;
}
return 0;
}
void Dialog_Scan::ocrThreadFunc(HGThread thread, HGPointer param)
{
Dialog_Scan *p = (Dialog_Scan*)param;
HGBase_RunMsgPump(p->m_ocrMsgPump, ocrMsgPumpFunc, param);
}
void Dialog_Scan::ocrMsgPumpFunc(HGMsgPump msgPump, const HGMsg *msg, HGPointer param)
{
Dialog_Scan *p = (Dialog_Scan*)param;
if (msg->id == 1)
{
QString *filePath = (QString *)msg->data;
HGOCRMgr ocrMgr = NULL;
HGImgProc_CreateOCRMgr(HGIMGPROC_OCRALGO_DEFAULT, &ocrMgr);
if (NULL != ocrMgr)
{
HGImgFmtReader reader = NULL;
HGImgFmt_OpenImageReader(filePath->toLocal8Bit().toStdString().c_str(), 0, &reader);
if (NULL != reader)
{
HGUInt count = 0;
HGImgFmt_GetImagePageCount(reader, &count);
for (HGUInt i = 0; i < count; ++i)
{
HGImage image = NULL;
HGImgFmt_LoadImageFromReader(reader, i, NULL, 0, 0, &image);
if (NULL != image)
{
HGImgProc_AddToImageOCRList(ocrMgr, image);
HGBase_DestroyImage(image);
}
}
HGImgFmt_CloseImageReader(reader);
}
//HGBase_DeleteFile(filePath->toLocal8Bit().toStdString().c_str());
HGImgProc_ImageListOCRToFile(ocrMgr, 0, filePath->toLocal8Bit().toStdString().c_str(), NULL, NULL);
HGImgProc_DestroyOCRMgr(ocrMgr);
}
delete filePath;
}
}
void Dialog_Scan::closeEvent(QCloseEvent *e)
{
hide(); // 隐藏主窗口
e->ignore(); //忽略关闭事件,这样才不会关闭程序
}
void Dialog_Scan::on_deviceArrive(QString devName)
{
ui->comboBox->addItem(devName);
if (nullptr == m_devHandle)
{
SANE_Status status = sane_open(devName.toStdString().c_str(), &m_devHandle);
if (SANE_STATUS_GOOD == status)
{
m_devName = devName;
m_mainWnd->AddManualScanParam();
}
}
}
void Dialog_Scan::on_deviceRemove(QString devName)
{
if (devName == m_devName)
{
StopScan();
sane_close(m_devHandle);
m_devHandle = NULL;
m_devName.clear();
}
for (int i = 0; i < ui->comboBox->count(); ++i)
{
if (ui->comboBox->itemText(i) == devName)
{
ui->comboBox->removeItem(i);
break;
}
}
}
void Dialog_Scan::on_keyPress(unsigned int buttonId)
{
assert(NULL != m_devHandle);
StartScan(buttonId);
}
void Dialog_Scan::on_scanWorking()
{
m_dlgScanInfo->Start();
}
void Dialog_Scan::on_scanInfo(QString info, bool error)
{
m_dlgScanInfo->SetScanInfo(info, error);
}
void Dialog_Scan::on_scanImage(unsigned int count)
{
m_dlgScanInfo->SetScanCount(count);
}
void Dialog_Scan::on_scanFinish()
{
if (nullptr != m_scanImgFmtWriter)
{
HGImgFmt_CloseImageWriter(m_scanImgFmtWriter);
m_scanImgFmtWriter = nullptr;
if (nullptr != m_ocrMsgPump)
{
QString *filePath = new QString(m_scanFileName);
HGMsg msg;
msg.id = 1;
msg.data = filePath;
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_ocrMsgPump, &msg))
{
delete filePath;
}
}
m_scanFileName.clear();
++m_aquireIntoSaveParam.m_fileNameStartIndex;
m_aquireIntoMultiPageCount = 0;
}
if (NULL != m_ocrMsgPump)
{
HGBase_ExitMsgPump(m_ocrMsgPump);
HGBase_CloseThread(m_ocrThread);
m_ocrThread = NULL;
HGBase_DestroyMsgPump(m_ocrMsgPump);
m_ocrMsgPump = NULL;
}
StopScan();
m_dlgScanInfo->Finish();
}
void Dialog_Scan::on_comboBox_currentIndexChanged(const QString &arg1)
{
assert(!m_scanning);
if (NULL != m_devHandle)
{
sane_close(m_devHandle);
m_devHandle = NULL;
m_devName.clear();
}
SANE_Status status = sane_open(arg1.toStdString().c_str(), &m_devHandle);
if (SANE_STATUS_GOOD == status)
{
m_devName = arg1;
m_mainWnd->AddManualScanParam();
}
}
void Dialog_Scan::on_pushButtonScan_clicked()
{
assert(!m_scanning);
StartScan(0);
}

View File

@ -1,82 +0,0 @@
#ifndef DIALOG_SCAN_H
#define DIALOG_SCAN_H
#include <QDialog>
#include <vector>
#include "sane/sane_ex.h"
#include "sane/sane_option_definitions.h"
#include "base/HGImage.h"
#include "base/HGThread.h"
#include "base/HGMsgPump.h"
#include "imgfmt/HGImgFmt.h"
#include "mainwindow.h"
namespace Ui {
class Dialog_Scan;
}
class Dialog_Scan : public QDialog
{
Q_OBJECT
friend class MainWindow;
public:
explicit Dialog_Scan(class MainWindow *mainWnd);
~Dialog_Scan();
public:
void StartScan(unsigned int buttonId);
void StopScan();
private:
void SaveImage(HGImage image);
static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param);
static void HGAPI ocrThreadFunc(HGThread thread, HGPointer param);
static void HGAPI ocrMsgPumpFunc(HGMsgPump msgPump, const HGMsg* msg, HGPointer param);
protected:
virtual void closeEvent(QCloseEvent *e) override;
signals:
void deviceArrive(QString devName);
void deviceRemove(QString devName);
void keyPress(unsigned int buttonId);
void scanWorking();
void scanInfo(QString info, bool error);
void scanImage(unsigned int count);
void scanFinish();
private slots:
void on_deviceArrive(QString devName);
void on_deviceRemove(QString devName);
void on_keyPress(unsigned int buttonId);
void on_scanWorking();
void on_scanInfo(QString info, bool error);
void on_scanImage(unsigned int count);
void on_scanFinish();
void on_comboBox_currentIndexChanged(const QString &arg1);
void on_pushButtonScan_clicked();
private:
Ui::Dialog_Scan *ui;
class MainWindow *m_mainWnd;
class Dialog_ScanInfo *m_dlgScanInfo;
QString m_devName;
SANE_Handle m_devHandle;
bool m_scanning;
HGUInt m_dpi;
QString m_scanFileName;
HGImgFmtWriter m_scanImgFmtWriter;
SaveParam m_aquireIntoSaveParam;
bool m_aquireIntoInBlank;
int m_aquireIntoBatchStartIndex;
int m_aquireIntoPageIndex;
int m_aquireIntoMultiPageCount;
HGMsgPump m_ocrMsgPump;
HGThread m_ocrThread;
};
#endif // DIALOG_SCAN_H

View File

@ -1,150 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog_Scan</class>
<widget class="QDialog" name="Dialog_Scan">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>316</width>
<height>181</height>
</rect>
</property>
<property name="windowTitle">
<string>Scan Setting</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>62</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>设备列表:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox">
<property name="minimumSize">
<size>
<width>171</width>
<height>20</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButtonScan">
<property name="minimumSize">
<size>
<width>81</width>
<height>31</height>
</size>
</property>
<property name="text">
<string>Scan</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>61</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -1,12 +1,12 @@
#include "dialog_scaninfo.h" #include "dialog_scaninfo.h"
#include "ui_dialog_scaninfo.h" #include "ui_dialog_scaninfo.h"
#include "dialog_scan.h" #include "mainwindow.h"
#include <QDesktopWidget> #include <QDesktopWidget>
Dialog_ScanInfo::Dialog_ScanInfo(class Dialog_Scan *dlgScan) Dialog_ScanInfo::Dialog_ScanInfo(class MainWindow *mainWindow)
: QDialog(nullptr) : QDialog(nullptr)
, ui(new Ui::Dialog_ScanInfo) , ui(new Ui::Dialog_ScanInfo)
, m_dlgScan(dlgScan) , m_mainWindow(mainWindow)
, m_timerId(-1) , m_timerId(-1)
, m_error(false) , m_error(false)
{ {
@ -91,5 +91,5 @@ void Dialog_ScanInfo::on_pushButtonOK_clicked()
void Dialog_ScanInfo::on_pushButtonCancel_clicked() void Dialog_ScanInfo::on_pushButtonCancel_clicked()
{ {
m_dlgScan->StopScan(); m_mainWindow->StopScan();
} }

View File

@ -12,7 +12,7 @@ class Dialog_ScanInfo : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit Dialog_ScanInfo(class Dialog_Scan *dlgScan); explicit Dialog_ScanInfo(class MainWindow *mainWindow);
~Dialog_ScanInfo(); ~Dialog_ScanInfo();
void Start(); void Start();
@ -30,7 +30,7 @@ private slots:
private: private:
Ui::Dialog_ScanInfo *ui; Ui::Dialog_ScanInfo *ui;
class Dialog_Scan *m_dlgScan; class MainWindow *m_mainWindow;
int m_timerId; int m_timerId;
bool m_error; bool m_error;
}; };

View File

@ -12,40 +12,14 @@
#include <QSpacerItem> #include <QSpacerItem>
#include <QPushButton> #include <QPushButton>
#include <QEvent> #include <QEvent>
#include <QMessageBox>
Form_DeviceConfig::Form_DeviceConfig(SANE_Handle devHandle, const std::vector<DeviceConfig>& deviceConfigs, QWidget *parent) Form_DeviceConfig::Form_DeviceConfig(SANE_Handle devHandle, const std::vector<DeviceConfig>& deviceConfigs, QWidget *parent)
: QWidget(parent) : QWidget(parent)
, m_devHandle(devHandle)
{ {
Init(devHandle); Init();
Update(deviceConfigs);
std::vector<DeviceConfigsGroup> deviceConfigsGroups = m_baseDeviceConfigsGroups;
for (int i = 0; i < (int)deviceConfigs.size(); ++i)
{
bool set = false;
for (int m = 0; m < (int)deviceConfigsGroups.size(); ++m)
{
for (int n = 0; n < (int)deviceConfigsGroups[m].deviceConfigs.size(); ++n)
{
DeviceConfigEx &deviceConfig = deviceConfigsGroups[m].deviceConfigs[n];
if (deviceConfig.name == deviceConfigs[i].name && deviceConfig.valueType == deviceConfigs[i].valueType)
{
deviceConfig.stringValue = deviceConfigs[i].stringValue;
deviceConfig.intValue = deviceConfigs[i].intValue;
deviceConfig.doubleValue = deviceConfigs[i].doubleValue;
deviceConfig.boolValue = deviceConfigs[i].boolValue;
set = true;
break;
}
}
if (set)
{
break;
}
}
}
Update(deviceConfigsGroups);
} }
Form_DeviceConfig::~Form_DeviceConfig() Form_DeviceConfig::~Form_DeviceConfig()
@ -180,15 +154,15 @@ bool Form_DeviceConfig::eventFilter(QObject *target, QEvent *event)
return QWidget::eventFilter(target, event); return QWidget::eventFilter(target, event);
} }
void Form_DeviceConfig::Init(SANE_Handle devHandle) void Form_DeviceConfig::Init()
{ {
// 1.重置设备 // 1.重置设备
SANE_Int num_dev_options = 0; SANE_Int num_dev_options = 0;
sane_control_option(devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL); sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int i = 1; i < num_dev_options; ++i) for (int i = 1; i < num_dev_options; ++i)
{ {
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(devHandle, i); const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp) if (NULL == desp)
continue; continue;
@ -198,7 +172,7 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
if (0 == strcmp(SANE_STD_OPT_NAME_RESTORE, name) && SANE_TYPE_BUTTON == desp->type) if (0 == strcmp(SANE_STD_OPT_NAME_RESTORE, name) && SANE_TYPE_BUTTON == desp->type)
{ {
sane_control_option(devHandle, i, SANE_ACTION_SET_VALUE, NULL, NULL); sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, NULL, NULL);
break; break;
} }
} }
@ -208,7 +182,7 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
m_baseDeviceConfigsGroups.clear(); m_baseDeviceConfigsGroups.clear();
for (int i = 1; i < num_dev_options; ++i) for (int i = 1; i < num_dev_options; ++i)
{ {
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(devHandle, i); const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp) if (NULL == desp)
continue; continue;
@ -229,9 +203,10 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
else if (SANE_TYPE_STRING == desp->type) else if (SANE_TYPE_STRING == desp->type)
{ {
char value[256] = { 0 }; char value[256] = { 0 };
sane_control_option(devHandle, i, SANE_ACTION_GET_VALUE, value, NULL); sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, value, NULL);
DeviceConfigEx devConfig; DeviceConfigEx devConfig;
devConfig.id = i;
devConfig.name = name; devConfig.name = name;
devConfig.title = title; devConfig.title = title;
devConfig.valueType = 1; devConfig.valueType = 1;
@ -254,9 +229,10 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
else if (SANE_TYPE_INT == desp->type) else if (SANE_TYPE_INT == desp->type)
{ {
SANE_Int value = 0; SANE_Int value = 0;
sane_control_option(devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL); sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL);
DeviceConfigEx devConfig; DeviceConfigEx devConfig;
devConfig.id = i;
devConfig.name = name; devConfig.name = name;
devConfig.title = title; devConfig.title = title;
devConfig.valueType = 2; devConfig.valueType = 2;
@ -284,9 +260,10 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
else if (SANE_TYPE_FIXED == desp->type) else if (SANE_TYPE_FIXED == desp->type)
{ {
SANE_Word value = 0; SANE_Word value = 0;
sane_control_option(devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL); sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL);
DeviceConfigEx devConfig; DeviceConfigEx devConfig;
devConfig.id = i;
devConfig.name = name; devConfig.name = name;
devConfig.title = title; devConfig.title = title;
devConfig.valueType = 3; devConfig.valueType = 3;
@ -314,9 +291,10 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
else if (SANE_TYPE_BOOL == desp->type) else if (SANE_TYPE_BOOL == desp->type)
{ {
SANE_Bool value = 0; SANE_Bool value = 0;
sane_control_option(devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL); sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, NULL);
DeviceConfigEx devConfig; DeviceConfigEx devConfig;
devConfig.id = i;
devConfig.name = name; devConfig.name = name;
devConfig.title = title; devConfig.title = title;
devConfig.valueType = 4; devConfig.valueType = 4;
@ -414,12 +392,13 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
slider->setProperty("relate_ctrl", QVariant::fromValue(spinBox)); slider->setProperty("relate_ctrl", QVariant::fromValue(spinBox));
spinBox->setProperty("relate_ctrl", QVariant::fromValue(slider)); spinBox->setProperty("relate_ctrl", QVariant::fromValue(slider));
ctrl = slider;
QHBoxLayout* hLayout = new QHBoxLayout; QHBoxLayout* hLayout = new QHBoxLayout;
hLayout->addWidget(slider); hLayout->addWidget(slider);
hLayout->addWidget(spinBox); hLayout->addWidget(spinBox);
QWidget* sliderSpinWidget = new QWidget; QWidget* sliderSpinWidget = new QWidget;
sliderSpinWidget->setLayout(hLayout); sliderSpinWidget->setLayout(hLayout);
ctrl = slider;
ctrlWidget = sliderSpinWidget; ctrlWidget = sliderSpinWidget;
} }
else if (5 == deviceConfig.rangeType) else if (5 == deviceConfig.rangeType)
@ -443,12 +422,13 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
slider->setProperty("relate_ctrl", QVariant::fromValue(doubleSpinBox)); slider->setProperty("relate_ctrl", QVariant::fromValue(doubleSpinBox));
doubleSpinBox->setProperty("relate_ctrl", QVariant::fromValue(slider)); doubleSpinBox->setProperty("relate_ctrl", QVariant::fromValue(slider));
ctrl = slider;
QHBoxLayout* hLayout = new QHBoxLayout; QHBoxLayout* hLayout = new QHBoxLayout;
hLayout->addWidget(slider); hLayout->addWidget(slider);
hLayout->addWidget(doubleSpinBox); hLayout->addWidget(doubleSpinBox);
QWidget* sliderSpinWidget = new QWidget; QWidget* sliderSpinWidget = new QWidget;
sliderSpinWidget->setLayout(hLayout); sliderSpinWidget->setLayout(hLayout);
ctrl = slider;
ctrlWidget = sliderSpinWidget; ctrlWidget = sliderSpinWidget;
} }
else if (0 == deviceConfig.rangeType) else if (0 == deviceConfig.rangeType)
@ -474,6 +454,7 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
else if (4 == deviceConfig.valueType) else if (4 == deviceConfig.valueType)
{ {
QCheckBox *checkBox = new QCheckBox; QCheckBox *checkBox = new QCheckBox;
connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(on_checkedClicked()));
ctrl = checkBox; ctrl = checkBox;
ctrlWidget = checkBox; ctrlWidget = checkBox;
} }
@ -482,6 +463,9 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
if (nullptr != ctrl) if (nullptr != ctrl)
{ {
ctrl->setProperty("config_name", QString::fromStdString(deviceConfig.name)); ctrl->setProperty("config_name", QString::fromStdString(deviceConfig.name));
ctrl->setProperty("config_id", deviceConfig.id);
ctrl->setProperty("ctrl_label", QVariant::fromValue(label));
ctrl->setProperty("ctrl_widget", QVariant::fromValue(ctrlWidget));
} }
if (nullptr != ctrlWidget) if (nullptr != ctrlWidget)
@ -494,8 +478,37 @@ void Form_DeviceConfig::Init(SANE_Handle devHandle)
} }
} }
void Form_DeviceConfig::Update(std::vector<DeviceConfigsGroup> &deviceConfigsGroups) void Form_DeviceConfig::Update(const std::vector<DeviceConfig>& deviceConfigs)
{ {
// 1.更新UI
std::vector<DeviceConfigsGroup> deviceConfigsGroups = m_baseDeviceConfigsGroups;
for (int i = 0; i < (int)deviceConfigs.size(); ++i)
{
bool set = false;
for (int m = 0; m < (int)deviceConfigsGroups.size(); ++m)
{
for (int n = 0; n < (int)deviceConfigsGroups[m].deviceConfigs.size(); ++n)
{
DeviceConfigEx &deviceConfig = deviceConfigsGroups[m].deviceConfigs[n];
if (deviceConfig.name == deviceConfigs[i].name && deviceConfig.valueType == deviceConfigs[i].valueType)
{
deviceConfig.stringValue = deviceConfigs[i].stringValue;
deviceConfig.intValue = deviceConfigs[i].intValue;
deviceConfig.doubleValue = deviceConfigs[i].doubleValue;
deviceConfig.boolValue = deviceConfigs[i].boolValue;
set = true;
break;
}
}
if (set)
{
break;
}
}
}
for (int i = 0; i < (int)deviceConfigsGroups.size(); ++i) for (int i = 0; i < (int)deviceConfigsGroups.size(); ++i)
{ {
for (int j = 0; j < (int)deviceConfigsGroups[i].deviceConfigs.size(); ++j) for (int j = 0; j < (int)deviceConfigsGroups[i].deviceConfigs.size(); ++j)
@ -569,11 +582,113 @@ void Form_DeviceConfig::Update(std::vector<DeviceConfigsGroup> &deviceConfigsGro
} }
} }
} }
// 2.恢复默认
SANE_Int num_dev_options = 0;
sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(SANE_STD_OPT_NAME_RESTORE, name) && SANE_TYPE_BUTTON == desp->type)
{
sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, NULL, NULL);
break;
}
}
// 3.设置新的属性
for (int i = 0; i < (int)deviceConfigs.size(); ++i)
{
for (int j = 1; j < num_dev_options; ++j)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, j);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(deviceConfigs[i].name.c_str(), name))
{
if (SANE_TYPE_STRING == desp->type)
{
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, (void*)deviceConfigs[i].stringValue.c_str(), NULL);
}
else if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = deviceConfigs[i].intValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_FIXED == desp->type)
{
SANE_Fixed value = SANE_FIX(deviceConfigs[i].doubleValue);
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_BOOL == desp->type)
{
SANE_Bool value = (SANE_Bool)deviceConfigs[i].boolValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
break;
}
}
}
UpdateControls();
}
void Form_DeviceConfig::UpdateControls()
{
SANE_Int num_dev_options = 0;
sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
QWidget *ctrl = nullptr;
QList<QWidget*> widgets = this->findChildren<QWidget*>();
foreach (QWidget* widget, widgets)
{
if (widget->property("config_name").toString().toStdString() == name)
{
ctrl = widget;
break;
}
}
if (nullptr != ctrl)
{
QWidget *label = qvariant_cast<QWidget*>(ctrl->property("ctrl_label"));
QWidget *ctrlWidget = qvariant_cast<QWidget*>(ctrl->property("ctrl_widget"));
bool hide = ((desp->cap & SANE_CAP_INACTIVE) == SANE_CAP_INACTIVE);
label->setVisible(!hide);
ctrlWidget->setVisible(!hide);;
}
}
} }
void Form_DeviceConfig::on_defaultBtn_clicked() void Form_DeviceConfig::on_defaultBtn_clicked()
{ {
Update(m_baseDeviceConfigsGroups); std::vector<DeviceConfig> deviceConfigs;
Update(deviceConfigs);
} }
void Form_DeviceConfig::on_sliderClicked(int value) void Form_DeviceConfig::on_sliderClicked(int value)
@ -606,3 +721,28 @@ void Form_DeviceConfig::on_doubleSpinboxClicked(double value)
QSlider* slider = qvariant_cast<QSlider*>(doubleSpinBox->property("relate_ctrl")); QSlider* slider = qvariant_cast<QSlider*>(doubleSpinBox->property("relate_ctrl"));
slider->setValue(round(value * 100)); slider->setValue(round(value * 100));
} }
void Form_DeviceConfig::on_checkedClicked()
{
QCheckBox *checkBox = qobject_cast<QCheckBox*>(sender());
SANE_Bool currentState = checkBox->isChecked();
int id = checkBox->property("config_id").toInt();
SANE_Int method = 0;
SANE_Status ret = sane_control_option(m_devHandle, id, SANE_ACTION_SET_VALUE, &currentState, &method);
if (ret == SANE_STATUS_UNSUPPORTED)
{
SANE_Bool value = false;
ret = sane_control_option(m_devHandle, id, SANE_ACTION_GET_VALUE, &value, &method);
disconnect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(on_checkedClicked()));
checkBox->setCheckState(value ? Qt::Checked : Qt::Unchecked);
connect(checkBox, SIGNAL(stateChanged(int)), this, SLOT(on_checkedClicked()));
QMessageBox::information(this, tr("Prompt"), tr("The funtion is unsupported"));
return;
}
if ((method & SANE_INFO_RELOAD_OPTIONS) == SANE_INFO_RELOAD_OPTIONS)
{
UpdateControls();
}
}

View File

@ -43,6 +43,7 @@ struct DeviceConfigEx
} }
// 配置名 // 配置名
int id;
std::string name; std::string name;
std::string title; std::string title;
@ -83,17 +84,19 @@ public:
private: private:
virtual bool eventFilter(QObject *target, QEvent *event) override; virtual bool eventFilter(QObject *target, QEvent *event) override;
void Init(SANE_Handle devHandle); void Init();
void Update(std::vector<DeviceConfigsGroup> &deviceConfigsGroups); void Update(const std::vector<DeviceConfig>& deviceConfigs);
void UpdateControls();
private slots: private slots:
void on_defaultBtn_clicked(); void on_defaultBtn_clicked();
void on_sliderClicked(int value); void on_sliderClicked(int value);
void on_spinBoxClicked(int value); void on_spinBoxClicked(int value);
void on_doubleSpinboxClicked(double value); void on_doubleSpinboxClicked(double value);
void on_checkedClicked();
private: private:
SANE_Handle m_devHandle;
std::vector<DeviceConfigsGroup> m_baseDeviceConfigsGroups; std::vector<DeviceConfigsGroup> m_baseDeviceConfigsGroups;
}; };

View File

@ -1,36 +1,48 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "dialog_scan.h"
#include "form_saveparam.h"
#include <QMessageBox>
#include <QCloseEvent> #include <QCloseEvent>
#include <QMessageBox>
#include <QDateTime>
#include <QFileInfo>
#include "base/HGTime.h"
#include "base/HGUtility.h"
#include "imgproc/HGImgProc.h"
#include "imgproc/HGOCR.h"
#include "HGUIGlobal.h"
#include "dialog_scaninfo.h"
#include "form_saveparam.h"
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
, m_dlgAdd(nullptr)
, m_dlgScanInfo(nullptr)
, m_devHandle(nullptr)
, m_scanning(false)
, m_dpi(200)
, m_scanFileName("")
, m_scanImgFmtWriter(nullptr)
, m_ocrMsgPump(nullptr)
, m_ocrThread(nullptr)
{ {
ui->setupUi(this); ui->setupUi(this);
m_scanDlg = new Dialog_Scan(this);
m_trayIcon = new QSystemTrayIcon(this); m_trayIcon = new QSystemTrayIcon(this);
m_trayIcon->setIcon(QIcon(":images/image_rsc/png/logo.png")); m_trayIcon->setIcon(QIcon(":images/image_rsc/png/logo.png"));
m_trayIcon->show(); m_trayIcon->show();
m_trayIcon->setToolTip(tr("HuaGao Scan Tool")); m_trayIcon->setToolTip(tr("Scan Tool"));
qRegisterMetaType<QSystemTrayIcon::ActivationReason>("QSystemTrayIcon::ActivationReason"); qRegisterMetaType<QSystemTrayIcon::ActivationReason>("QSystemTrayIcon::ActivationReason");
connect(m_trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::on_trayActivated); connect(m_trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::on_trayActivated);
m_scanAction = new QAction(tr("Scan Setting"), this); m_showAction = new QAction(tr("Show"), this);
connect(m_scanAction, &QAction::triggered, this, &MainWindow::on_showScanSettingDlg); connect(m_showAction, &QAction::triggered, this, &MainWindow::on_showMainWindowDlg);
m_btnAction = new QAction(tr("Button Setting"), this);
connect(m_btnAction, &QAction::triggered, this, &MainWindow::on_showMainWindowDlg);
m_quitAction = new QAction(tr("Exit"), this); m_quitAction = new QAction(tr("Exit"), this);
connect(m_quitAction, &QAction::triggered, this, &QCoreApplication::quit); //应用程序的退出 connect(m_quitAction, &QAction::triggered, this, &QCoreApplication::quit); //应用程序的退出
//创建菜单,添加菜单项 //创建菜单,添加菜单项
m_trayIconMenu = new QMenu(this); m_trayIconMenu = new QMenu(this);
m_trayIconMenu->addAction(m_scanAction); m_trayIconMenu->addAction(m_showAction);
m_trayIconMenu->addAction(m_btnAction);
m_trayIconMenu->addSeparator(); //分割线 m_trayIconMenu->addSeparator(); //分割线
m_trayIconMenu->addAction(m_quitAction); m_trayIconMenu->addAction(m_quitAction);
//给系统托盘添加右键菜单 //给系统托盘添加右键菜单
@ -77,32 +89,291 @@ MainWindow::MainWindow(QWidget *parent)
} }
ui->tableWidget->selectRow(0); ui->tableWidget->selectRow(0);
m_dlgScanInfo = new Dialog_ScanInfo(this);
connect(this, SIGNAL(deviceArrive(QString)), this, SLOT(on_deviceArrive(QString)));
connect(this, SIGNAL(deviceRemove(QString)), this, SLOT(on_deviceRemove(QString)));
connect(this, SIGNAL(keyPress(unsigned int)), this, SLOT(on_keyPress(unsigned int)));
connect(this, SIGNAL(scanWorking()), this, SLOT(on_scanWorking()));
connect(this, SIGNAL(scanInfo(QString, bool)), this, SLOT(on_scanInfo(QString, bool)));
connect(this, SIGNAL(scanImage(unsigned int)), this, SLOT(on_scanImage(unsigned int)));
connect(this, SIGNAL(scanFinish()), this, SLOT(on_scanFinish()));
SANE_Int version_code = 0;
sane_init_ex(&version_code, sane_ex_callback, this);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
delete m_scanDlg; assert(nullptr == m_dlgAdd);
if (NULL != m_devHandle)
{
StopScan();
sane_close(m_devHandle);
m_devHandle = NULL;
m_devName.clear();
}
sane_exit();
delete m_dlgScanInfo;
delete ui; delete ui;
} }
void MainWindow::SetBusy(bool busy) bool MainWindow::FindScanParam(const std::string &deviceType, unsigned int buttonId, int ignoreIndex)
{ {
m_quitAction->setEnabled(!busy); for (int i = 0; i < (int)m_vScanParams.size(); ++i)
ui->pushButtonAdd->setEnabled(!busy); {
ui->pushButtonModify->setEnabled(!busy); if (deviceType == m_vScanParams[i].deviceType && buttonId == m_vScanParams[i].buttonId)
ui->pushButtonRemove->setEnabled(!busy); {
if (-1 == ignoreIndex) // 表示均不忽略
{
return true;
}
else if (i != ignoreIndex)
{
return true;
}
}
}
return false;
} }
void MainWindow::AddManualScanParam() void MainWindow::StopScan()
{ {
if (NULL == m_scanDlg->m_devHandle) if (m_scanning)
{
assert(NULL != m_devHandle);
sane_cancel(m_devHandle);
m_scanning = false;
m_quitAction->setEnabled(true);
ui->pushButtonAdd->setEnabled(true);
ui->pushButtonModify->setEnabled(true);
ui->pushButtonRemove->setEnabled(true);
ui->comboBox->setEnabled(true);
ui->pushButtonScan->setEnabled(true);
m_dpi = 200;
}
}
void MainWindow::LoadCfg()
{
// TODO 加载配置文件到m_vScanParams
}
void MainWindow::SaveCfg()
{
// TODO 保存m_vScanParams到配置文件
}
QString MainWindow::GetDesc(const std::vector<DeviceConfig> &deviceConfigs)
{
QString desc;
return desc;
}
QString MainWindow::GetDesc(const SaveParam &saveParam)
{
QString desc;
desc += "[";
desc += tr("Save path:");
desc += saveParam.m_savePath;
desc += "]";
desc += " ";
desc += "[";
desc += tr("Image format:");
desc += saveParam.m_fileNameExt;
desc += "]";
return desc;
}
void MainWindow::StartScan(unsigned int buttonId)
{
if (nullptr == m_devHandle)
{
QMessageBox::information(this, tr("Tips"), tr("Device is offline"));
return;
}
if (m_scanning || nullptr != m_dlgAdd)
{ {
return; return;
} }
std::string deviceType = m_scanDlg->m_devName.toStdString(); std::string deviceType = m_devName.toStdString();
char v[256] = {0}; char v[256] = {0};
SANE_Status status = sane_control_option(m_scanDlg->m_devHandle, (SANE_Int)0x886D, SANE_ACTION_GET_VALUE, v, NULL); SANE_Status status = sane_control_option(m_devHandle, (SANE_Int)0x886D, SANE_ACTION_GET_VALUE, v, NULL);
if (SANE_STATUS_GOOD == status)
{
deviceType = v;
}
ScanParam scanParam;
bool find = false;
for (int i = 0; i < (int)m_vScanParams.size(); ++i)
{
if (deviceType == m_vScanParams[i].deviceType && buttonId == m_vScanParams[i].buttonId)
{
scanParam = m_vScanParams[i];
find = true;
}
}
if (!find)
{
// 手动扫描一定能找到配置,按键扫描不能提示
return;
}
// 1.恢复默认
SANE_Int num_dev_options = 0;
sane_control_option(m_devHandle, 0, SANE_ACTION_GET_VALUE, &num_dev_options, NULL);
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(SANE_STD_OPT_NAME_RESTORE, name) && SANE_TYPE_BUTTON == desp->type)
{
sane_control_option(m_devHandle, i, SANE_ACTION_SET_VALUE, NULL, NULL);
break;
}
}
// 2.设置新的属性
for (int i = 0; i < (int)scanParam.deviceConfigs.size(); ++i)
{
for (int j = 1; j < num_dev_options; ++j)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, j);
if (NULL == desp)
continue;
const char* name = desp->name;
while (' ' == *name)
++name;
if (0 == strcmp(scanParam.deviceConfigs[i].name.c_str(), name))
{
if (SANE_TYPE_STRING == desp->type)
{
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, (void*)scanParam.deviceConfigs[i].stringValue.c_str(), NULL);
}
else if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = scanParam.deviceConfigs[i].intValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_FIXED == desp->type)
{
SANE_Fixed value = SANE_FIX(scanParam.deviceConfigs[i].doubleValue);
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
else if (SANE_TYPE_BOOL == desp->type)
{
SANE_Bool value = (SANE_Bool)scanParam.deviceConfigs[i].boolValue;
sane_control_option(m_devHandle, j, SANE_ACTION_SET_VALUE, &value, NULL);
}
break;
}
}
}
// 3. 获取DPI
for (int i = 1; i < num_dev_options; ++i)
{
const SANE_Option_Descriptor* desp = sane_get_option_descriptor(m_devHandle, i);
if (nullptr == desp)
continue;
if (SANE_TYPE_INT == desp->type)
{
SANE_Int value = 0;
sane_control_option(m_devHandle, i, SANE_ACTION_GET_VALUE, &value, nullptr);
if (0 == strcmp(desp->name, SANE_STD_OPT_NAME_RESOLUTION))
{
m_dpi = (HGUInt)value;
}
}
}
// 4.保存配置
assert(m_scanFileName.isEmpty());
assert(nullptr == m_scanImgFmtWriter);
m_aquireIntoSaveParam = scanParam.saveParam;
m_aquireIntoInBlank = true;
m_aquireIntoBatchStartIndex = 0;
m_aquireIntoPageIndex = 0;
m_aquireIntoMultiPageCount = 0;
QDateTime dateTime = QDateTime::currentDateTime();
if (m_aquireIntoSaveParam.m_isUseSubfolderByTime)
{
m_aquireIntoSaveParam.m_savePath = getStdFileName(m_aquireIntoSaveParam.m_savePath + dateTime.toString("yyyy-MM-dd") + "/");
}
if (m_aquireIntoSaveParam.m_isOcr)
{
HGBase_CreateMsgPump(&m_ocrMsgPump);
HGBase_OpenThread(ocrThreadFunc, this, &m_ocrThread);
}
m_scanning = true;
m_quitAction->setEnabled(false);
ui->pushButtonAdd->setEnabled(false);
ui->pushButtonModify->setEnabled(false);
ui->pushButtonRemove->setEnabled(false);
ui->comboBox->setEnabled(false);
ui->pushButtonScan->setEnabled(false);
status = sane_start(m_devHandle);
if (SANE_STATUS_GOOD != status)
{
if (NULL != m_ocrMsgPump)
{
HGBase_ExitMsgPump(m_ocrMsgPump);
HGBase_CloseThread(m_ocrThread);
m_ocrThread = NULL;
HGBase_DestroyMsgPump(m_ocrMsgPump);
m_ocrMsgPump = NULL;
}
m_scanning = false;
m_quitAction->setEnabled(true);
ui->pushButtonAdd->setEnabled(true);
ui->pushButtonModify->setEnabled(true);
ui->pushButtonRemove->setEnabled(true);
ui->comboBox->setEnabled(true);
ui->pushButtonScan->setEnabled(true);
m_dpi = 200;
emit scanWorking();
emit scanInfo((const char*)sane_strstatus(status), true);
emit scanFinish();
return;
}
}
void MainWindow::AddManualScanParam()
{
assert(NULL != m_devHandle);
std::string deviceType = m_devName.toStdString();
char v[256] = {0};
SANE_Status status = sane_control_option(m_devHandle, (SANE_Int)0x886D, SANE_ACTION_GET_VALUE, v, NULL);
if (SANE_STATUS_GOOD == status) if (SANE_STATUS_GOOD == status)
{ {
deviceType = v; deviceType = v;
@ -146,72 +417,353 @@ void MainWindow::AddManualScanParam()
ui->tableWidget->selectRow(index); ui->tableWidget->selectRow(index);
} }
bool MainWindow::GetScanParam(const std::string &deviceType, unsigned int buttonId, ScanParam &scanParam) void MainWindow::SaveImage(HGImage image)
{ {
for (int i = 0; i < (int)m_vScanParams.size(); ++i) if (m_aquireIntoSaveParam.m_isSaveAsMultiPage)
{ {
if (deviceType == m_vScanParams[i].deviceType && buttonId == m_vScanParams[i].buttonId) if (nullptr == m_scanImgFmtWriter)
{ {
scanParam = m_vScanParams[i]; assert(m_scanFileName.isEmpty());
return true; HGBase_CreateDir(getStdString(m_aquireIntoSaveParam.m_savePath).c_str());
}
}
return false; QString scanFileName;
} while (1)
bool MainWindow::FindScanParam(const std::string &deviceType, unsigned int buttonId, int ignoreIndex)
{
for (int i = 0; i < (int)m_vScanParams.size(); ++i)
{
if (deviceType == m_vScanParams[i].deviceType && buttonId == m_vScanParams[i].buttonId)
{
if (-1 == ignoreIndex) // 表示均不忽略
{ {
return true; scanFileName = m_aquireIntoSaveParam.m_savePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2")
.arg(m_aquireIntoSaveParam.m_fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0'))
.arg(m_aquireIntoSaveParam.m_fileNameExt);
QFileInfo fileInfo(scanFileName);
if (fileInfo.isFile())
{
++m_aquireIntoSaveParam.m_fileNameStartIndex;
}
else
{
break;
}
} }
else if (i != ignoreIndex)
HGUInt fmtType = 0;
if (nullptr != m_ocrMsgPump)
{ {
return true; fmtType = HGIMGFMT_TYPE_TIFF;
}
HGImgFmt_OpenImageWriter(getStdString(getStdFileName(scanFileName)).c_str(), fmtType, &m_scanImgFmtWriter);
if (nullptr != m_scanImgFmtWriter)
{
m_scanFileName = scanFileName;
}
}
if (nullptr != m_scanImgFmtWriter)
{
HGImgFmtSaveInfo saveInfo;
saveInfo.jpegQuality = (HGUInt)m_aquireIntoSaveParam.m_jpegQuality;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = (HGUInt)m_aquireIntoSaveParam.m_tiffQuality;
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
if (HGBASE_IMGTYPE_BINARY == imgInfo.type)
{
if (1 == m_aquireIntoSaveParam.m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == m_aquireIntoSaveParam.m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_CCITTFAX4;
}
else
{
if (1 == m_aquireIntoSaveParam.m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == m_aquireIntoSaveParam.m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_JPEG;
}
if (nullptr != m_ocrMsgPump)
{
saveInfo.jpegQuality = 100;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = 100;
}
if (HGBASE_ERR_OK == HGImgFmt_SaveImageToWriter(m_scanImgFmtWriter, image, &saveInfo))
{
++m_aquireIntoMultiPageCount;
if (1 == m_aquireIntoSaveParam.m_multiPagesType && m_aquireIntoMultiPageCount == m_aquireIntoSaveParam.m_customMultiPages)
{
HGImgFmt_CloseImageWriter(m_scanImgFmtWriter);
m_scanImgFmtWriter = nullptr;
if (nullptr != m_ocrMsgPump)
{
QString *filePath = new QString(m_scanFileName);
HGMsg msg;
msg.id = 1;
msg.data = filePath;
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_ocrMsgPump, &msg))
{
delete filePath;
}
}
m_scanFileName.clear();
++m_aquireIntoSaveParam.m_fileNameStartIndex;
m_aquireIntoMultiPageCount = 0;
}
} }
} }
} }
else
{
assert(nullptr == m_scanImgFmtWriter);
return false; QString savePath = m_aquireIntoSaveParam.m_savePath;
if (m_aquireIntoSaveParam.m_isUseSubfolderByBlankPages)
{
HGBool isBlank = HGFALSE;
HGImgProc_ImageBlankCheck(image, nullptr, &isBlank);
if (isBlank)
{
m_aquireIntoInBlank = true;
}
else
{
if (m_aquireIntoInBlank)
{
++m_aquireIntoBatchStartIndex;
}
m_aquireIntoInBlank = false;
}
char batchDir[20];
sprintf(batchDir, "batch%d", m_aquireIntoBatchStartIndex);
savePath = getStdFileName(savePath + batchDir + "/");
}
if (m_aquireIntoSaveParam.m_isUseSubfolderByColor)
{
QString colorModeName;
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
if (HGBASE_IMGTYPE_BINARY == imgInfo.type)
colorModeName = tr("binary");
else if (HGBASE_IMGTYPE_GRAY == imgInfo.type)
colorModeName = tr("gray");
else
colorModeName = tr("rgb");
savePath = getStdFileName(savePath + colorModeName + "/");
}
HGBase_CreateDir(getStdString(savePath).c_str());
while (1)
{
m_scanFileName = savePath + m_aquireIntoSaveParam.m_fileNamePrefix + QString("%1.%2")
.arg(m_aquireIntoSaveParam.m_fileNameStartIndex, m_aquireIntoSaveParam.m_fileNameDigits, 10, QLatin1Char('0'))
.arg(m_aquireIntoSaveParam.m_fileNameExt);
QFileInfo fileInfo(m_scanFileName);
if (fileInfo.isFile())
{
++m_aquireIntoSaveParam.m_fileNameStartIndex;
}
else
{
break;
}
}
HGImgFmtSaveInfo saveInfo;
saveInfo.jpegQuality = (HGUInt)m_aquireIntoSaveParam.m_jpegQuality;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = (HGUInt)m_aquireIntoSaveParam.m_tiffQuality;
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
if (HGBASE_IMGTYPE_BINARY == imgInfo.type)
{
if (1 == m_aquireIntoSaveParam.m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == m_aquireIntoSaveParam.m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_CCITTFAX4;
}
else
{
if (1 == m_aquireIntoSaveParam.m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == m_aquireIntoSaveParam.m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_JPEG;
}
HGUInt fmtType = 0;
if (nullptr != m_ocrMsgPump)
{
fmtType = HGIMGFMT_TYPE_TIFF;
saveInfo.jpegQuality = 100;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = 100;
}
if (HGBASE_ERR_OK == HGImgFmt_SaveImage(image, fmtType, &saveInfo, getStdString(m_scanFileName).c_str()))
{
if (nullptr != m_ocrMsgPump)
{
QString *filePath = new QString(m_scanFileName);
HGMsg msg;
msg.id = 1;
msg.data = filePath;
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_ocrMsgPump, &msg))
{
delete filePath;
}
}
++m_aquireIntoSaveParam.m_fileNameStartIndex;
}
m_scanFileName.clear();
}
} }
void MainWindow::LoadCfg() int MainWindow::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param)
{ {
// TODO 加载配置文件到m_vScanParams (void)hdev;
(void)len;
MainWindow* p = (MainWindow*)param;
switch (code)
{
case SANE_EVENT_DEVICE_ARRIVED:
{
SANE_Device* sane_dev = (SANE_Device*)data;
emit p->deviceArrive(sane_dev->name);
}
break;
case SANE_EVENT_DEVICE_LEFT:
{
SANE_Device* sane_dev = (SANE_Device*)data;
emit p->deviceRemove(sane_dev->name);
}
break;
case SANE_EVENT_WORKING:
{
emit p->scanWorking();
emit p->scanInfo((const char*)data, false);
}
break;
case SANE_EVENT_SCAN_FINISHED:
{
emit p->scanInfo((const char*)data, (0 != *len));
emit p->scanFinish();
}
break;
case SANE_EVENT_STATUS:
{
//emit p->scanInfo((const char*)data, false);
}
break;
case SANE_EVENT_ERROR:
{
//emit p->scanInfo((const char*)data, (0 != *len));
}
break;
case SANE_EVENT_DEV_KEY_PRESSED:
{
emit p->keyPress(*len);
}
break;
case SANE_EVENT_IMAGE_OK:
{
++p->m_aquireIntoPageIndex;
emit p->scanImage(p->m_aquireIntoPageIndex);
if ((1 == p->m_aquireIntoSaveParam.m_fileNameOddEventType && 1 != p->m_aquireIntoPageIndex % 2)
|| (2 == p->m_aquireIntoSaveParam.m_fileNameOddEventType && 0 != p->m_aquireIntoPageIndex % 2))
{
// 跳过
}
else
{
SANE_Image* sane_img = (SANE_Image*)data;
HGUInt imgType = 0;
if (sane_img->header.format == SANE_FRAME_GRAY)
{
if (1 == sane_img->header.depth)
imgType = HGBASE_IMGTYPE_BINARY;
else if (8 == sane_img->header.depth)
imgType = HGBASE_IMGTYPE_GRAY;
}
else if (sane_img->header.format == SANE_FRAME_RGB)
imgType = HGBASE_IMGTYPE_RGB;
HGByte* data = sane_img->data;
HGImageInfo imgInfo = { (HGUInt)sane_img->header.pixels_per_line, (HGUInt)sane_img->header.lines,
imgType, (HGUInt)sane_img->header.bytes_per_line, HGBASE_IMGORIGIN_TOP };
HGImage img = NULL;
HGBase_CreateImageFromData(data, &imgInfo, NULL, 0, HGBASE_IMGORIGIN_TOP, &img);
if (NULL != img)
{
HGBase_SetImageDpi(img, p->m_dpi, p->m_dpi);
p->SaveImage(img);
HGBase_DestroyImage(img);
}
}
}
break;
}
return 0;
} }
void MainWindow::SaveCfg() void MainWindow::ocrThreadFunc(HGThread thread, HGPointer param)
{ {
// TODO 保存m_vScanParams到配置文件 MainWindow *p = (MainWindow*)param;
HGBase_RunMsgPump(p->m_ocrMsgPump, ocrMsgPumpFunc, param);
} }
QString MainWindow::GetDesc(const std::vector<DeviceConfig> &deviceConfigs) void MainWindow::ocrMsgPumpFunc(HGMsgPump msgPump, const HGMsg *msg, HGPointer param)
{ {
QString desc; MainWindow *p = (MainWindow*)param;
if (msg->id == 1)
{
QString *filePath = (QString *)msg->data;
return desc; HGOCRMgr ocrMgr = NULL;
} HGImgProc_CreateOCRMgr(HGIMGPROC_OCRALGO_DEFAULT, &ocrMgr);
if (NULL != ocrMgr)
{
HGImgFmtReader reader = NULL;
HGImgFmt_OpenImageReader(filePath->toLocal8Bit().toStdString().c_str(), 0, &reader);
if (NULL != reader)
{
HGUInt count = 0;
HGImgFmt_GetImagePageCount(reader, &count);
for (HGUInt i = 0; i < count; ++i)
{
HGImage image = NULL;
HGImgFmt_LoadImageFromReader(reader, i, NULL, 0, 0, &image);
if (NULL != image)
{
HGImgProc_AddToImageOCRList(ocrMgr, image);
HGBase_DestroyImage(image);
}
}
QString MainWindow::GetDesc(const SaveParam &saveParam) HGImgFmt_CloseImageReader(reader);
{ }
QString desc;
desc += "["; //HGBase_DeleteFile(filePath->toLocal8Bit().toStdString().c_str());
desc += tr("Save path:"); HGImgProc_ImageListOCRToFile(ocrMgr, 0, filePath->toLocal8Bit().toStdString().c_str(), NULL, NULL);
desc += saveParam.m_savePath;
desc += "]";
desc += " ";
desc += "[";
desc += tr("Image format:");
desc += saveParam.m_fileNameExt;
desc += "]";
return desc; HGImgProc_DestroyOCRMgr(ocrMgr);
}
delete filePath;
}
} }
void MainWindow::closeEvent(QCloseEvent *e) void MainWindow::closeEvent(QCloseEvent *e)
@ -223,6 +775,118 @@ void MainWindow::closeEvent(QCloseEvent *e)
} }
} }
void MainWindow::on_deviceArrive(QString devName)
{
ui->comboBox->addItem(devName);
}
void MainWindow::on_deviceRemove(QString devName)
{
if (devName == m_devName)
{
assert(nullptr != m_devHandle);
StopScan();
if (nullptr != m_dlgAdd)
m_dlgAdd->reject();
sane_close(m_devHandle);
m_devHandle = NULL;
m_devName.clear();
}
for (int i = 0; i < ui->comboBox->count(); ++i)
{
if (ui->comboBox->itemText(i) == devName)
{
ui->comboBox->removeItem(i);
break;
}
}
}
void MainWindow::on_keyPress(unsigned int buttonId)
{
assert(nullptr != m_devHandle);
StartScan(buttonId);
}
void MainWindow::on_scanWorking()
{
m_dlgScanInfo->Start();
}
void MainWindow::on_scanInfo(QString info, bool error)
{
m_dlgScanInfo->SetScanInfo(info, error);
}
void MainWindow::on_scanImage(unsigned int count)
{
m_dlgScanInfo->SetScanCount(count);
}
void MainWindow::on_scanFinish()
{
if (nullptr != m_scanImgFmtWriter)
{
HGImgFmt_CloseImageWriter(m_scanImgFmtWriter);
m_scanImgFmtWriter = nullptr;
if (nullptr != m_ocrMsgPump)
{
QString *filePath = new QString(m_scanFileName);
HGMsg msg;
msg.id = 1;
msg.data = filePath;
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(m_ocrMsgPump, &msg))
{
delete filePath;
}
}
m_scanFileName.clear();
++m_aquireIntoSaveParam.m_fileNameStartIndex;
m_aquireIntoMultiPageCount = 0;
}
if (NULL != m_ocrMsgPump)
{
HGBase_ExitMsgPump(m_ocrMsgPump);
HGBase_CloseThread(m_ocrThread);
m_ocrThread = NULL;
HGBase_DestroyMsgPump(m_ocrMsgPump);
m_ocrMsgPump = NULL;
}
StopScan();
m_dlgScanInfo->Finish();
}
void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
if (NULL != m_devHandle)
{
StopScan();
if (nullptr != m_dlgAdd)
m_dlgAdd->reject();
sane_close(m_devHandle);
m_devHandle = NULL;
m_devName.clear();
}
SANE_Status status = sane_open(arg1.toStdString().c_str(), &m_devHandle);
if (SANE_STATUS_GOOD == status)
{
m_devName = arg1;
AddManualScanParam();
}
}
void MainWindow::on_pushButtonScan_clicked()
{
assert(!m_scanning && nullptr == m_dlgAdd);
StartScan(0);
}
void MainWindow::on_trayActivated(QSystemTrayIcon::ActivationReason reason) void MainWindow::on_trayActivated(QSystemTrayIcon::ActivationReason reason)
{ {
if (QSystemTrayIcon::Trigger == reason) if (QSystemTrayIcon::Trigger == reason)
@ -240,12 +904,6 @@ void MainWindow::on_trayActivated(QSystemTrayIcon::ActivationReason reason)
} }
} }
void MainWindow::on_showScanSettingDlg()
{
m_scanDlg->show();
m_scanDlg->raise();
}
void MainWindow::on_showMainWindowDlg() void MainWindow::on_showMainWindowDlg()
{ {
show(); show();
@ -254,24 +912,25 @@ void MainWindow::on_showMainWindowDlg()
void MainWindow::on_pushButtonAdd_clicked() void MainWindow::on_pushButtonAdd_clicked()
{ {
if (NULL == m_scanDlg->m_devHandle) if (nullptr == m_devHandle)
{ {
QMessageBox::information(this, tr("Tips"), tr("Device is offline")); QMessageBox::information(this, tr("Tips"), tr("Device is offline"));
return; return;
} }
std::string deviceType = m_scanDlg->m_devName.toStdString(); std::string deviceType = m_devName.toStdString();
char v[256] = {0}; char v[256] = {0};
SANE_Status status = sane_control_option(m_scanDlg->m_devHandle, (SANE_Int)0x886D, SANE_ACTION_GET_VALUE, v, NULL); SANE_Status status = sane_control_option(m_devHandle, (SANE_Int)0x886D, SANE_ACTION_GET_VALUE, v, NULL);
if (SANE_STATUS_GOOD == status) if (SANE_STATUS_GOOD == status)
{ {
deviceType = v; deviceType = v;
} }
Dialog_Add dlg(m_scanDlg->m_devHandle, this, deviceType); assert(nullptr == m_dlgAdd);
if (dlg.exec()) m_dlgAdd = new Dialog_Add(m_devHandle, this, deviceType);
if (m_dlgAdd->exec())
{ {
ScanParam scanParam = dlg.GetScanParam(); ScanParam scanParam = m_dlgAdd->GetScanParam();
m_vScanParams.push_back(scanParam); m_vScanParams.push_back(scanParam);
SaveCfg(); SaveCfg();
@ -299,6 +958,9 @@ void MainWindow::on_pushButtonAdd_clicked()
ui->tableWidget->selectRow(index); ui->tableWidget->selectRow(index);
} }
delete m_dlgAdd;
m_dlgAdd = nullptr;
} }
void MainWindow::on_pushButtonModify_clicked() void MainWindow::on_pushButtonModify_clicked()
@ -310,15 +972,15 @@ void MainWindow::on_pushButtonModify_clicked()
return; return;
} }
if (NULL == m_scanDlg->m_devHandle) if (nullptr == m_devHandle)
{ {
QMessageBox::information(this, tr("Tips"), tr("Device is offline")); QMessageBox::information(this, tr("Tips"), tr("Device is offline"));
return; return;
} }
std::string deviceType = m_scanDlg->m_devName.toStdString(); std::string deviceType = m_devName.toStdString();
char v[256] = {0}; char v[256] = {0};
SANE_Status status = sane_control_option(m_scanDlg->m_devHandle, (SANE_Int)0x886D, SANE_ACTION_GET_VALUE, v, NULL); SANE_Status status = sane_control_option(m_devHandle, (SANE_Int)0x886D, SANE_ACTION_GET_VALUE, v, NULL);
if (SANE_STATUS_GOOD == status) if (SANE_STATUS_GOOD == status)
{ {
deviceType = v; deviceType = v;
@ -330,10 +992,11 @@ void MainWindow::on_pushButtonModify_clicked()
return; return;
} }
Dialog_Add dlg(m_scanDlg->m_devHandle, this, m_vScanParams[index], index); assert(nullptr == m_dlgAdd);
if (dlg.exec()) m_dlgAdd = new Dialog_Add(m_devHandle, this, m_vScanParams[index], index);
if (m_dlgAdd->exec())
{ {
ScanParam scanParam = dlg.GetScanParam(); ScanParam scanParam = m_dlgAdd->GetScanParam();
m_vScanParams[index] = scanParam; m_vScanParams[index] = scanParam;
SaveCfg(); SaveCfg();
@ -345,6 +1008,9 @@ void MainWindow::on_pushButtonModify_clicked()
ui->tableWidget->item(index, 2)->setText(GetDesc(m_vScanParams[index].deviceConfigs)); ui->tableWidget->item(index, 2)->setText(GetDesc(m_vScanParams[index].deviceConfigs));
ui->tableWidget->item(index, 3)->setText(GetDesc(m_vScanParams[index].saveParam)); ui->tableWidget->item(index, 3)->setText(GetDesc(m_vScanParams[index].saveParam));
} }
delete m_dlgAdd;
m_dlgAdd = nullptr;
} }
void MainWindow::on_pushButtonRemove_clicked() void MainWindow::on_pushButtonRemove_clicked()

View File

@ -6,6 +6,12 @@
#include <QMenu> #include <QMenu>
#include <string> #include <string>
#include <vector> #include <vector>
#include "sane/sane_ex.h"
#include "sane/sane_option_definitions.h"
#include "base/HGImage.h"
#include "base/HGThread.h"
#include "base/HGMsgPump.h"
#include "imgfmt/HGImgFmt.h"
#include "dialog_add.h" #include "dialog_add.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -20,23 +26,44 @@ public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
void SetBusy(bool busy);
void AddManualScanParam();
bool GetScanParam(const std::string &deviceType, unsigned int buttonId, ScanParam &scanParam);
bool FindScanParam(const std::string &deviceType, unsigned int buttonId, int ignoreIndex); bool FindScanParam(const std::string &deviceType, unsigned int buttonId, int ignoreIndex);
void StopScan();
private: private:
void LoadCfg(); void LoadCfg();
void SaveCfg(); void SaveCfg();
QString GetDesc(const std::vector<DeviceConfig> &deviceConfigs); QString GetDesc(const std::vector<DeviceConfig> &deviceConfigs);
QString GetDesc(const SaveParam &saveParam); QString GetDesc(const SaveParam &saveParam);
void StartScan(unsigned int buttonId);
void AddManualScanParam();
void SaveImage(HGImage image);
static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param);
static void HGAPI ocrThreadFunc(HGThread thread, HGPointer param);
static void HGAPI ocrMsgPumpFunc(HGMsgPump msgPump, const HGMsg* msg, HGPointer param);
protected: protected:
virtual void closeEvent(QCloseEvent *e) override; virtual void closeEvent(QCloseEvent *e) override;
signals:
void deviceArrive(QString devName);
void deviceRemove(QString devName);
void keyPress(unsigned int buttonId);
void scanWorking();
void scanInfo(QString info, bool error);
void scanImage(unsigned int count);
void scanFinish();
private slots: private slots:
void on_deviceArrive(QString devName);
void on_deviceRemove(QString devName);
void on_keyPress(unsigned int buttonId);
void on_scanWorking();
void on_scanInfo(QString info, bool error);
void on_scanImage(unsigned int count);
void on_scanFinish();
void on_comboBox_currentIndexChanged(const QString &arg1);
void on_pushButtonScan_clicked();
void on_trayActivated(QSystemTrayIcon::ActivationReason reason); void on_trayActivated(QSystemTrayIcon::ActivationReason reason);
void on_showScanSettingDlg();
void on_showMainWindowDlg(); void on_showMainWindowDlg();
void on_pushButtonAdd_clicked(); void on_pushButtonAdd_clicked();
void on_pushButtonModify_clicked(); void on_pushButtonModify_clicked();
@ -45,12 +72,27 @@ private slots:
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
class Dialog_Scan *m_scanDlg;
QSystemTrayIcon* m_trayIcon; QSystemTrayIcon* m_trayIcon;
QAction *m_scanAction; QAction *m_showAction;
QAction *m_btnAction;
QAction *m_quitAction; QAction *m_quitAction;
QMenu *m_trayIconMenu; QMenu *m_trayIconMenu;
std::vector<ScanParam> m_vScanParams; std::vector<ScanParam> m_vScanParams;
class Dialog_Add *m_dlgAdd;
class Dialog_ScanInfo *m_dlgScanInfo;
QString m_devName;
SANE_Handle m_devHandle;
bool m_scanning;
HGUInt m_dpi;
QString m_scanFileName;
HGImgFmtWriter m_scanImgFmtWriter;
SaveParam m_aquireIntoSaveParam;
bool m_aquireIntoInBlank;
int m_aquireIntoBatchStartIndex;
int m_aquireIntoPageIndex;
int m_aquireIntoMultiPageCount;
HGMsgPump m_ocrMsgPump;
HGThread m_ocrThread;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -19,6 +19,23 @@
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QComboBox" name="comboBox">
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonScan">
<property name="text">
<string>Scan</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="horizontalSpacer"> <spacer name="horizontalSpacer">
<property name="orientation"> <property name="orientation">

View File

@ -101,7 +101,6 @@ SOURCES += \
../../../app/scantool/HGUIGlobal.cpp \ ../../../app/scantool/HGUIGlobal.cpp \
../../../app/scantool/app_cfg.cpp \ ../../../app/scantool/app_cfg.cpp \
../../../app/scantool/dialog_add.cpp \ ../../../app/scantool/dialog_add.cpp \
../../../app/scantool/dialog_scan.cpp \
../../../app/scantool/dialog_scaninfo.cpp \ ../../../app/scantool/dialog_scaninfo.cpp \
../../../app/scantool/dialog_writesettings.cpp \ ../../../app/scantool/dialog_writesettings.cpp \
../../../app/scantool/form_deviceconfig.cpp \ ../../../app/scantool/form_deviceconfig.cpp \
@ -117,7 +116,6 @@ HEADERS += \
../../../app/scantool/HGUIGlobal.h \ ../../../app/scantool/HGUIGlobal.h \
../../../app/scantool/app_cfg.h \ ../../../app/scantool/app_cfg.h \
../../../app/scantool/dialog_add.h \ ../../../app/scantool/dialog_add.h \
../../../app/scantool/dialog_scan.h \
../../../app/scantool/dialog_scaninfo.h \ ../../../app/scantool/dialog_scaninfo.h \
../../../app/scantool/dialog_writesettings.h \ ../../../app/scantool/dialog_writesettings.h \
../../../app/scantool/form_deviceconfig.h \ ../../../app/scantool/form_deviceconfig.h \
@ -129,7 +127,6 @@ HEADERS += \
FORMS += \ FORMS += \
../../../app/scantool/dialog_add.ui \ ../../../app/scantool/dialog_add.ui \
../../../app/scantool/dialog_scan.ui \
../../../app/scantool/dialog_scaninfo.ui \ ../../../app/scantool/dialog_scaninfo.ui \
../../../app/scantool/dialog_writesettings.ui \ ../../../app/scantool/dialog_writesettings.ui \
../../../app/scantool/form_saveparam.ui \ ../../../app/scantool/form_saveparam.ui \