code_app/app/scanner/dialog_exportimagefile.cpp

274 lines
9.8 KiB
C++
Raw Normal View History

2022-05-03 10:25:52 +00:00
#include "dialog_exportimagefile.h"
#include "ui_dialog_exportimagefile.h"
#include "mainwindow.h"
2022-05-03 10:25:52 +00:00
#include "imgfmt/HGImgFmt.h"
2022-08-23 15:35:53 +00:00
#include "imgproc/HGOCR.h"
2022-05-03 10:25:52 +00:00
#include "HGUIGlobal.h"
2022-05-18 03:39:16 +00:00
#include "HGString.h"
2022-06-13 08:04:19 +00:00
#include <QMessageBox>
2022-05-03 10:25:52 +00:00
Dialog_ExportImageFile::Dialog_ExportImageFile(const QString &destPath, const QString &destName, const QString &destExt,
2022-05-16 09:00:08 +00:00
bool isSaveAsMulti, int jpegQuality, int tiffCompressionBW, int tiffCompression, int tiffQuality, bool isOcr,
const QStringList &srcFiles, QWidget *parent)
2022-05-03 10:25:52 +00:00
: QDialog(parent)
, ui(new Ui::Dialog_ExportImageFile)
, m_destPath(destPath)
, m_destName(destName)
, m_destExt(destExt)
, m_isSaveAsMulti(isSaveAsMulti)
, m_jpegQuality(jpegQuality)
, m_tiffCompressionBW(tiffCompressionBW)
, m_tiffCompression(tiffCompression)
, m_tiffQuality(tiffQuality)
2022-05-16 09:00:08 +00:00
, m_isOcr(isOcr)
2022-05-03 10:25:52 +00:00
, m_srcFiles(srcFiles)
{
ui->setupUi(this);
ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(srcFiles.size());
ui->progressBar->setValue(0);
connect(this, SIGNAL(updateProgress(int)), this, SLOT(on_updateProgress(int)), Qt::QueuedConnection);
connect(this, SIGNAL(finish()), this, SLOT(on_finish()), Qt::QueuedConnection);
m_stopThread = false;
HGBase_OpenThread(ThreadFunc, this, &m_thread);
}
Dialog_ExportImageFile::~Dialog_ExportImageFile()
{
if (nullptr != m_thread)
{
HGBase_CloseThread(m_thread);
m_thread = nullptr;
}
delete ui;
}
2022-10-12 03:51:41 +00:00
void HGAPI Dialog_ExportImageFile::ThreadFunc(HGThread thread, HGPointer param)
2022-05-03 10:25:52 +00:00
{
(void)thread;
Dialog_ExportImageFile *p = (Dialog_ExportImageFile *)param;
if (p->m_isSaveAsMulti)
{
QString fileName = p->m_destPath + p->m_destName + p->m_destExt;
2022-08-23 15:35:53 +00:00
if (p->m_isOcr)
2022-05-03 10:25:52 +00:00
{
2022-08-23 15:35:53 +00:00
HGOCRMgr ocrMgr = nullptr;
2022-10-22 06:27:50 +00:00
HGImgProc_CreateOCRMgr(0, &ocrMgr);
2022-08-23 15:35:53 +00:00
if (nullptr != ocrMgr)
2022-05-03 10:25:52 +00:00
{
2022-08-23 15:35:53 +00:00
for (int i = 0; i < p->m_srcFiles.size(); ++i)
2022-05-03 10:25:52 +00:00
{
2022-08-23 15:35:53 +00:00
if (p->m_stopThread)
{
break;
}
2022-05-03 10:25:52 +00:00
2022-08-23 15:35:53 +00:00
emit p->updateProgress(i);
2022-05-03 10:25:52 +00:00
2022-08-23 15:35:53 +00:00
HGImgFmtReader imgFmtReader = nullptr;
HGImgFmt_OpenImageReader(getStdString(p->m_srcFiles[i]).c_str(), 0, &imgFmtReader);
if (nullptr != imgFmtReader)
2022-05-03 10:25:52 +00:00
{
2022-08-23 15:35:53 +00:00
HGUInt count = 0;
HGImgFmt_GetImagePageCount(imgFmtReader, &count);
for (HGUInt j = 0; j < count; ++j)
2022-05-03 10:25:52 +00:00
{
2022-08-23 15:35:53 +00:00
if (p->m_stopThread)
{
break;
}
HGImage img = nullptr;
HGImgFmt_LoadImageFromReader(imgFmtReader, j, nullptr, 0, HGBASE_IMGORIGIN_TOP, &img);
if (nullptr != img)
{
HGImgProc_AddToImageOCRList(ocrMgr, img);
HGBase_DestroyImage(img);
}
2022-05-03 10:25:52 +00:00
}
2022-08-23 15:35:53 +00:00
HGImgFmt_CloseImageReader(imgFmtReader);
}
}
HGImgProc_ImageListOCRToFile(ocrMgr, 0, getStdString(fileName).c_str(), NULL, NULL);
HGImgProc_DestroyOCRMgr(ocrMgr);
}
}
else
{
HGImgFmtWriter imgFmtWriter = nullptr;
HGImgFmt_OpenImageWriter(getStdString(fileName).c_str(), 0, &imgFmtWriter);
if (nullptr != imgFmtWriter)
{
for (int i = 0; i < p->m_srcFiles.size(); ++i)
{
if (p->m_stopThread)
{
break;
}
emit p->updateProgress(i);
HGImgFmtReader imgFmtReader = nullptr;
HGImgFmt_OpenImageReader(getStdString(p->m_srcFiles[i]).c_str(), 0, &imgFmtReader);
if (nullptr != imgFmtReader)
{
HGUInt count = 0;
HGImgFmt_GetImagePageCount(imgFmtReader, &count);
for (HGUInt j = 0; j < count; ++j)
2022-05-03 10:25:52 +00:00
{
2022-08-23 15:35:53 +00:00
if (p->m_stopThread)
{
2022-08-23 15:35:53 +00:00
break;
}
2022-08-23 15:35:53 +00:00
HGImage img = nullptr;
HGImgFmt_LoadImageFromReader(imgFmtReader, j, nullptr, 0, HGBASE_IMGORIGIN_TOP, &img);
if (nullptr != img)
{
2022-08-23 15:35:53 +00:00
HGImgFmtSaveInfo saveInfo;
saveInfo.jpegQuality = (HGUInt)p->m_jpegQuality;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = (HGUInt)p->m_tiffQuality;
HGImageInfo imgInfo;
HGBase_GetImageInfo(img, &imgInfo);
if (HGBASE_IMGTYPE_BINARY == imgInfo.type)
{
if (1 == p->m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == p->m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_CCITTFAX4;
}
else
{
if (1 == p->m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == p->m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_JPEG;
}
HGImgFmt_SaveImageToWriter(imgFmtWriter, img, &saveInfo);
HGBase_DestroyImage(img);
}
2022-05-03 10:25:52 +00:00
}
2022-08-23 15:35:53 +00:00
HGImgFmt_CloseImageReader(imgFmtReader);
}
2022-05-03 10:25:52 +00:00
}
2022-08-23 15:35:53 +00:00
HGImgFmt_CloseImageWriter(imgFmtWriter);
}
2022-05-03 10:25:52 +00:00
}
}
else
{
int index = 1;
for (int i = 0; i < p->m_srcFiles.size(); ++i)
{
if (p->m_stopThread)
{
break;
}
emit p->updateProgress(i);
HGImgFmtReader imgFmtReader = nullptr;
HGImgFmt_OpenImageReader(getStdString(p->m_srcFiles[i]).c_str(), 0, &imgFmtReader);
if (nullptr != imgFmtReader)
{
HGUInt count = 0;
HGImgFmt_GetImagePageCount(imgFmtReader, &count);
for (HGUInt j = 0; j < count; ++j)
{
if (p->m_stopThread)
{
break;
}
HGImage img = nullptr;
HGImgFmt_LoadImageFromReader(imgFmtReader, j, nullptr, 0, HGBASE_IMGORIGIN_TOP, &img);
2022-05-03 10:25:52 +00:00
if (nullptr != img)
{
char fileName[512];
if(p->m_srcFiles.size() > 1)
{
sprintf(fileName, "%s%03d", getStdString(p->m_destName).c_str(), index);
}else
sprintf(fileName, "%s", getStdString(p->m_destName).c_str());
2022-05-03 10:25:52 +00:00
++index;
QString saveFileName = p->m_destPath + QString(StdStringToUtf8(fileName).c_str()) + p->m_destExt;
HGImgFmtSaveInfo saveInfo;
saveInfo.jpegQuality = (HGUInt)p->m_jpegQuality;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
saveInfo.tiffJpegQuality = (HGUInt)p->m_tiffQuality;
HGImageInfo imgInfo;
HGBase_GetImageInfo(img, &imgInfo);
if (HGBASE_IMGTYPE_BINARY == imgInfo.type)
{
if (1 == p->m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == p->m_tiffCompressionBW)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_CCITTFAX4;
}
else
{
if (1 == p->m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
else if (2 == p->m_tiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_JPEG;
}
2022-05-16 09:00:08 +00:00
MainWindow::saveImage(img, &saveInfo, p->m_isOcr, getStdString(saveFileName).c_str());
2022-05-03 10:25:52 +00:00
HGBase_DestroyImage(img);
}
}
HGImgFmt_CloseImageReader(imgFmtReader);
}
}
}
emit p->finish();
}
void Dialog_ExportImageFile::on_updateProgress(int value)
{
ui->progressBar->setValue(value);
}
void Dialog_ExportImageFile::on_finish()
{
2022-06-13 08:04:19 +00:00
QMessageBox msg(QMessageBox::Information, tr("tip"), tr("export succeed"), QMessageBox::Ok, this);
msg.setButtonText(QMessageBox::Ok, tr("ok"));
2022-05-03 10:25:52 +00:00
close();
2022-06-13 08:04:19 +00:00
msg.exec();
2022-05-03 10:25:52 +00:00
}
void Dialog_ExportImageFile::on_pushButton_clicked()
{
m_stopThread = true;
HGBase_CloseThread(m_thread);
m_thread = nullptr;
}
void Dialog_ExportImageFile::closeEvent(QCloseEvent *e)
{
(void)e;
m_stopThread = true;
HGBase_CloseThread(m_thread);
m_thread = nullptr;
}