duanjianjun 2 anni fa
parent
commit
0922b98f5d

+ 84 - 0
MFCApplication1/Config.cpp

@@ -0,0 +1,84 @@
+// Config.cpp: implementation of the CConfig class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#include "pch.h"
+#include <atlbase.h>
+#include "Config.h"
+
+#ifdef _DEBUG
+#undef THIS_FILE
+static char THIS_FILE[]=__FILE__;
+#define new DEBUG_NEW
+#endif
+
+//////////////////////////////////////////////////////////////////////
+// Construction/Destruction
+//////////////////////////////////////////////////////////////////////
+CConfig CConfig::s_Config;
+
+
+CConfig::CConfig()
+{
+	
+}
+
+
+BOOL CConfig::SaveSetting()
+{
+	BOOL bRet=FALSE;
+	bRet=SaveToINI();
+	return bRet;
+}
+
+BOOL CConfig::LoadFromINI()
+{
+	TCHAR szData[_MAX_PATH];
+	memset(szData, 0, sizeof(szData));
+	CString strIni = GetAppPath() + _T("config.ini");
+	
+	GetPrivateProfileString(_T("Set"), _T("Txt"), _T(""), szData, sizeof(szData), strIni);
+	m_strTxt = szData;
+	GetPrivateProfileString(_T("Set"), _T("Pdf"), _T(""), szData, sizeof(szData), strIni);
+	m_strPdf = szData;
+	GetPrivateProfileString(_T("Set"), _T("Mode"), _T(""), szData, sizeof(szData), strIni);
+	m_strMode = szData;
+	GetPrivateProfileString(_T("Set"), _T("Img"), _T(""), szData, sizeof(szData), strIni);
+	m_strImg = szData;
+	GetPrivateProfileString(_T("Set"), _T("Cut"), _T(""), szData, sizeof(szData), strIni);
+	m_strCut = szData;
+	return TRUE;
+}
+
+BOOL CConfig::SaveToINI()
+{
+	TCHAR sTmp[64] = {0};
+	std::string strPwd;
+	CString strTmp = _T("");
+	CString strIni = GetAppPath() + _T("config.ini");
+	
+	WritePrivateProfileString(_T("Set"), _T("Txt"), m_strTxt, strIni);
+	WritePrivateProfileString(_T("Set"), _T("Pdf"), m_strPdf, strIni);
+
+	WritePrivateProfileString(_T("Set"), _T("Mode"), m_strMode, strIni);
+	WritePrivateProfileString(_T("Set"), _T("Img"), m_strImg, strIni);
+	WritePrivateProfileString(_T("Set"), _T("Cut"), m_strCut, strIni);
+	return TRUE;
+}
+
+
+CString CConfig::GetAppPath()
+{
+	CString strAppPath;
+	TCHAR *pBuf=strAppPath.GetBuffer(_MAX_PATH);
+
+	GetModuleFileName(AfxGetApp()->m_hInstance,pBuf,_MAX_PATH);
+
+	TCHAR* pch=_tcsrchr(pBuf,_T('\\'));
+	if(pch)
+		*(pch+1)=0;
+	else
+		_tcscat(pBuf,_T("\\"));
+	strAppPath.ReleaseBuffer();
+	return strAppPath;
+}

+ 46 - 0
MFCApplication1/Config.h

@@ -0,0 +1,46 @@
+// Config.h: interface for the CConfig class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#if !defined(AFX_CONFIG_H__FB63E5FE_85CD_4155_B44F_37652790F1D6__INCLUDED_)
+#define AFX_CONFIG_H__FB63E5FE_85CD_4155_B44F_37652790F1D6__INCLUDED_
+
+#if _MSC_VER > 1000
+#pragma once
+#endif // _MSC_VER > 1000
+
+
+#include <string>
+#include <map>
+
+using namespace std;
+
+class CConfig  
+{
+public:
+	static CConfig* Instance(){return &s_Config;}
+	~CConfig() {};
+
+public:
+	static CString GetAppPath();
+	BOOL SaveSetting();
+
+
+	BOOL LoadFromINI();
+	BOOL SaveToINI();
+
+protected:
+	CConfig();
+	static CConfig s_Config;
+
+public:
+	CString m_strTxt;
+	CString m_strPdf;
+
+	CString m_strMode;
+	CString m_strImg;
+	CString m_strCut;
+
+};
+
+#endif // !defined(AFX_CONFIG_H__FB63E5FE_85CD_4155_B44F_37652790F1D6__INCLUDED_)

+ 2 - 0
MFCApplication1/MFCApplication1.vcxproj

@@ -194,6 +194,7 @@
     <ClInclude Include="AssignWords.h" />
     <ClInclude Include="BaseUtility.h" />
     <ClInclude Include="Buffers.h" />
+    <ClInclude Include="Config.h" />
     <ClInclude Include="CvxText.h" />
     <ClInclude Include="framework.h" />
     <ClInclude Include="Instance.h" />
@@ -208,6 +209,7 @@
     <ClCompile Include="AssignWords.cpp" />
     <ClCompile Include="BaseUtility.cpp" />
     <ClCompile Include="Buffers.cpp" />
+    <ClCompile Include="Config.cpp" />
     <ClCompile Include="CvxText.cpp" />
     <ClCompile Include="Log.cpp" />
     <ClCompile Include="MFCApplication1.cpp" />

+ 6 - 0
MFCApplication1/MFCApplication1.vcxproj.filters

@@ -51,6 +51,9 @@
     <ClInclude Include="AssignWords.h">
       <Filter>头文件</Filter>
     </ClInclude>
+    <ClInclude Include="Config.h">
+      <Filter>头文件</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="MFCApplication1.cpp">
@@ -77,6 +80,9 @@
     <ClCompile Include="AssignWords.cpp">
       <Filter>源文件</Filter>
     </ClCompile>
+    <ClCompile Include="Config.cpp">
+      <Filter>源文件</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="MFCApplication1.rc">

+ 50 - 13
MFCApplication1/MFCApplication1Dlg.cpp

@@ -14,6 +14,8 @@
 #include "lib_common_depence.h"
 #include <direct.h>
 #include <io.h>
+#include "Buffers.h"
+#include "Config.h"
 
 #if defined(GNUC)
 #pragma GCC diagnostic push
@@ -164,7 +166,13 @@ HANDLE _threadScan = NULL;
 DWORD WINAPI ScanThread(void *param)
 {
 	CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
-
+	auto exitfun = [&]() {
+		//通知发送消息到窗口
+		CString strErrorMsg = L"";
+		strErrorMsg.Format(L"扫描线程退出!\r\n");
+		SendMessage(pWnd->m_hWnd, WM_SHOWSCANINFO, (WPARAM)strErrorMsg.GetBuffer(), 1);
+		strErrorMsg.ReleaseBuffer();
+	};
 	std::vector<std::string> imgPath;    //图片本地路径(和上面url地址一一对应)
 	preinfo::templatesInfo  temeplatInfo;
 	preinfo::SubjChiInfo sujChiInfo;
@@ -173,8 +181,14 @@ DWORD WINAPI ScanThread(void *param)
 	{
 		_mkdir(strTempDir.c_str());
 	}
-	PareseModeJson(temeplatInfo);
-	int nRet = api_processing_images("",
+	int nRet = PareseModeJson(temeplatInfo);
+	if (nRet)
+	{
+		pWnd->FormatScanMsg("模板解析识别");
+		exitfun();
+		return 0;
+	}
+	nRet = api_processing_images("",
 		strTempDir.c_str(),
 		temeplatInfo,
 		sujChiInfo,
@@ -187,16 +201,9 @@ DWORD WINAPI ScanThread(void *param)
 	////启动算法扫描功能
 	if (nRet != RT_OK)
 	{
-		// 扫描异常
-		CString strErrorMsg = L"";
-		strErrorMsg.Format(L"扫描失败,errorCode:%d!\r\n", nRet);
-		SendMessage(pWnd->m_hWnd, WM_SHOWSCANINFO, (WPARAM)strErrorMsg.GetBuffer(), 1);
+		pWnd->FormatScanMsg("扫描失败,errorCode:%d", nRet);
 	}
-	//通知发送消息到窗口
-	CString strErrorMsg = L"";
-	strErrorMsg.Format(L"扫描完成!\r\n");
-	SendMessage(pWnd->m_hWnd, WM_SHOWSCANINFO, (WPARAM)strErrorMsg.GetBuffer(), 1);
-	strErrorMsg.ReleaseBuffer();
+	exitfun();
 	return  0L;
 }
 CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=nullptr*/)
@@ -261,7 +268,13 @@ BOOL CMFCApplication1Dlg::OnInitDialog()
 	SetIcon(m_hIcon, TRUE);			// 设置大图标
 	SetIcon(m_hIcon, FALSE);		// 设置小图标
 
-	// TODO: 在此添加额外的初始化代码
+	CConfig::Instance()->LoadFromINI();
+	GetDlgItem(IDC_EDIT_TXT)->SetWindowText(CConfig::Instance()->m_strTxt);
+	GetDlgItem(IDC_EDIT_PDF)->SetWindowText(CConfig::Instance()->m_strPdf);
+
+	GetDlgItem(IDC_EDIT_MODEPATH)->SetWindowText(CConfig::Instance()->m_strMode);
+	GetDlgItem(IDC_EDIT_IMGPATH)->SetWindowText(CConfig::Instance()->m_strImg);
+	GetDlgItem(IDC_EDIT_CUTPATH)->SetWindowText(CConfig::Instance()->m_strCut);
 
 	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
 }
@@ -403,6 +416,7 @@ void CMFCApplication1Dlg::OnBnClickedCancel()
 {
 	SAFETY_EXIT_THREAD(_threadProcess, 100);
 	SAFETY_EXIT_THREAD(_threadScan, 100);
+	CConfig::Instance()->SaveSetting();
 	CDialogEx::OnCancel();
 }
 
@@ -452,6 +466,7 @@ void CMFCApplication1Dlg::OnBnClickedBtnTxt()
 		return;
 	}
 	pEdit->SetWindowText(strText);
+	CConfig::Instance()->m_strTxt = strText;
 }
 
 
@@ -469,6 +484,7 @@ void CMFCApplication1Dlg::OnBnClickedBtnPdf()
 		return;
 	}
 	pEdit->SetWindowText(strText);
+	CConfig::Instance()->m_strPdf = strText;
 }
 
 
@@ -486,6 +502,7 @@ void CMFCApplication1Dlg::OnBnClickedBtnModepath()
 		return;
 	}
 	pEdit->SetWindowText(strText);
+	CConfig::Instance()->m_strMode = strText;
 }
 
 
@@ -503,6 +520,7 @@ void CMFCApplication1Dlg::OnBnClickedBtnImgpath()
 		return;
 	}
 	pEdit->SetWindowText(strText);
+	CConfig::Instance()->m_strImg = strText;
 }
 
 
@@ -520,6 +538,7 @@ void CMFCApplication1Dlg::OnBnClickedBtnCutpath()
 		return;
 	}
 	pEdit->SetWindowText(strText);
+	CConfig::Instance()->m_strCut = strText;
 }
 
 
@@ -550,3 +569,21 @@ void CMFCApplication1Dlg::OnBnClickedBtnScan()
 	SAFETY_EXIT_THREAD(_threadScan, 100);
 	_threadScan = CreateThread(NULL, 0, ScanThread, NULL, 0, NULL);
 }
+
+void CMFCApplication1Dlg::FormatScanMsg(const char *szFmt, ...)
+{
+	if (!szFmt)
+		return;
+
+	va_list ap;
+	va_start(ap, szFmt);
+	size_t nLength = _vsnprintf(nullptr, 0, szFmt, ap) + 1;
+	CBuffers buffer;
+	char* szLogMsg = buffer.GetBuf(nLength);
+	_vsnprintf(szLogMsg, nLength, szFmt, ap);
+	va_end(ap);
+	CString strMsg;
+	// 扫描异常
+	strMsg.Format(L"%s!\r\n", GB2312ToTstring(szLogMsg).c_str());
+	SendMessage(WM_SHOWSCANINFO, (WPARAM)strMsg.GetBuffer(), 0);
+}

+ 1 - 0
MFCApplication1/MFCApplication1Dlg.h

@@ -58,4 +58,5 @@ public:
 	afx_msg void OnBnClickedBtnImgpath();
 	afx_msg void OnBnClickedBtnCutpath();
 	afx_msg void OnBnClickedBtnScan();
+	void FormatScanMsg(const char * szFmt, ...);
 };

+ 132 - 0
MFCApplication1/PBQLock.h

@@ -0,0 +1,132 @@
+
+#ifndef __PBQLOCK_H__
+#define __PBQLOCK_H__
+
+#include <windows.h>
+
+class PBQLock
+{
+//	¹¹ÔìºÍÎö¹¹
+public:
+	PBQLock()
+	{
+		::InitializeCriticalSection( &_cs );
+	}
+	virtual ~PBQLock()
+	{
+		::DeleteCriticalSection( &_cs );
+	}
+
+//	·½·¨
+public:
+	void Acquire()
+	{
+		::EnterCriticalSection( &_cs );
+	}
+	void Release()
+	{
+		::LeaveCriticalSection( &_cs );
+	}
+
+private:
+	CRITICAL_SECTION		_cs;
+};
+
+class PBQAutoLock
+{
+public:
+	PBQAutoLock( PBQLock& lock ) : _lock( lock )
+	{
+		_lock.Acquire();
+	}
+
+	virtual ~PBQAutoLock()
+	{
+		_lock.Release();
+	}
+
+private:
+	PBQLock&			_lock;
+	PBQAutoLock& operator=( const PBQAutoLock& );
+
+};
+
+class PBQRWLock
+{
+public:
+	class RWLockImpl
+	{
+		friend class PBQRWLock;
+	public:
+		RWLockImpl()
+			: _nInstance(0)
+		{
+			_mutex = CreateMutex(NULL, FALSE, NULL);
+			_evtRead = CreateEvent(NULL, TRUE, TRUE, NULL);
+			_evtWrite = CreateEvent(NULL, TRUE, TRUE, NULL);
+		}
+		virtual ~RWLockImpl()
+		{
+			CloseHandle(_mutex);
+			CloseHandle(_evtRead);
+			CloseHandle(_evtWrite);
+		}
+
+	private:
+		HANDLE			_evtRead;
+		HANDLE			_evtWrite;
+		HANDLE			_mutex;
+
+		int				_nInstance;
+
+	protected:
+		void	Aqurie(bool bWrite = true)
+		{
+			if (bWrite)
+			{
+				HANDLE	handles[2] = { _mutex, _evtWrite };
+				WaitForMultipleObjects(2, handles, TRUE, INFINITE);
+				ResetEvent(_evtRead);
+				ResetEvent(_evtWrite);
+				_nInstance++;
+				ReleaseMutex(_mutex);
+			}
+			else
+			{
+				HANDLE	handles[2] = { _mutex, _evtRead };
+				WaitForMultipleObjects(2, handles, TRUE, INFINITE);
+				ResetEvent(_evtWrite);
+				_nInstance++;
+				ReleaseMutex(_mutex);
+			}
+		}
+
+		void	Release(void)
+		{
+			WaitForSingleObject(_mutex, INFINITE);
+			_nInstance--;
+			if (_nInstance == 0)
+			{
+				SetEvent(_evtWrite);
+				SetEvent(_evtRead);
+			}
+			ReleaseMutex(_mutex);
+		}
+	};
+
+public:
+	PBQRWLock(RWLockImpl& impl, bool bWrite = true)
+		: _impl(impl)
+	{
+		_impl.Aqurie(bWrite);
+	}
+	virtual ~PBQRWLock()
+	{
+		_impl.Release();
+	}
+
+protected:
+	RWLockImpl&	_impl;
+};
+
+#endif	/*__PBQLOCK_H__*/