-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDlgQRDecode.cpp
305 lines (260 loc) · 7.99 KB
/
DlgQRDecode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// DlgQRDecode.cpp : 实现文件
//
#include "stdafx.h"
#include "3DCode.h"
#include "DlgQRDecode.h"
#include "afxdialogex.h"
#include "3DCodeDoc.h"
#include "AfxGlobal.h"
#include "FunctionsUsingMFC.h"
#include "CodeTransform.h"
#include "DMDecoder.h"
#include "PDF417Decoder.h"
#include "AztecDecoder.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CDlgQRDecode 对话框
IMPLEMENT_DYNAMIC(CDlgQRDecode, CDialogEx)
CDlgQRDecode::CDlgQRDecode(CWnd* pParent) : CDialogEx(CDlgQRDecode::IDD, pParent)
, m_bUseHybrid(TRUE)
, m_bTryHarder(TRUE)
{
m_pImage = NULL;
// 编码颜色
m_BackgroundColor = RGB(0, 0, 0);
ImageInfo::Init("yuanyuanxiang");
}
CDlgQRDecode::~CDlgQRDecode()
{
}
void CDlgQRDecode::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Check(pDX, IDC_CHECK_USE_HYBRID, m_bUseHybrid);
DDX_Check(pDX, IDC_CHECK_TRY_HARDER, m_bTryHarder);
DDX_Control(pDX, IDC_CHECK_USE_HYBRID, m_checkUseHybrid);
DDX_Control(pDX, IDC_CHECK_TRY_HARDER, m_checkTryHarder);
}
void CDlgQRDecode::SetImage()
{
m_pImage = AfxGetImage();
C3DCodeDoc* pDoc = (C3DCodeDoc*)AfxGetActiveDoc();
if (pDoc != NULL)
{
m_pImage = pDoc->GetImage();
m_roi = pDoc->GetROI();
}
else
{
m_pImage = NULL;
m_roi = RoiRect(0, 0, 0, 0);
}
}
/// 解码当前图像
BOOL CDlgQRDecode::Decode()
{
SetImage();
if (m_pImage == NULL || m_pImage->IsNull())
return FALSE;
// 更新或者获取控件关联变量
m_strPublicString.Empty();
m_strPrivateString.Empty();
m_bUseHybrid = m_checkUseHybrid.GetCheck();
m_bTryHarder = m_checkTryHarder.GetCheck();
// 获取解码参数
int nWidth = 0, nHeight = 0, nRowlen = 0;
// m_pImage->ChangeBPP(32);
m_pImage->GetInfomation(nWidth, nHeight, nRowlen);
int nChannel = m_pImage->GetChannel();
int nFloatRowlen = m_pImage->GetFloatDataRowlen();
BYTE *pHead = m_pImage->GetHeadAddress();
memset(&qr, 0, sizeof(BarCodeInfo));
memset(&inner, 0, sizeof(BarCodeInfo));
// 解码整幅图像
BeginWaitCursor();
ImageInfo pImage(pHead, nWidth, nHeight, nChannel, m_roi, m_BackgroundColor);
BOOL success = DecodeWholeImage(DecodeSrcInfo(pImage, TRUE, TRUE), &qr, &inner);
if (*qr.m_pData == NULL)
{
// 尝试进行DM、PDF417、Aztec二维码解码
DMDecoder dm;
dm.SetImgSrc(pImage.GetDecBuffer(), nWidth, nHeight, pImage.GetDecChannel(), m_roi);
if (dm.DecodeHard(m_BackgroundColor))
{
strcpy(qr.m_pData, dm.GetData());
}
else
{
PDF417Decoder pdf417;
pdf417.CopyOf(dm);
if (pdf417.DecodeHard(m_BackgroundColor))
{
strcpy(qr.m_pData, pdf417.GetData());
}
else
{
AztecDecoder az;
az.CopyOf(dm);
if (az.DecodeHard(m_BackgroundColor))
{
strcpy(qr.m_pData, az.GetData());
}
}
}
}
EndWaitCursor();
if (*qr.m_pData == NULL)
{
GetDlgItem(IDC_EDITSOURCEDATA_PUBLIC)->SetWindowText(_T("*** Decode failed ***"));
return FALSE;
}
int m_ncLength = 0;
m_strPublicString = UTF8Convert2Unicode(qr.m_pData, m_ncLength);
UpdateDecodeInfo();
if (*inner.m_pData != NULL)
{
m_strPrivateString = UTF8Convert2Unicode(inner.m_pData, m_ncLength);
GetDlgItem(IDC_EDITSOURCEDATA_PRIVATE)->SetWindowText(m_strPrivateString);
SetWindowInt(GetDlgItem(IDC_EDIT_INNER_ECLEVEL), inner.m_nEcLevel);
SetWindowInt(GetDlgItem(IDC_EDIT_INNER_MASK), inner.m_nMaskingNo);
UpdateecLevelTipText(inner.m_nEcLevel);
return TRUE;
}
else
{
GetDlgItem(IDC_EDITSOURCEDATA_PRIVATE)->SetWindowText(_T("*** Decode failed ***"));
}
return TRUE;
}
void CDlgQRDecode::UpdateDecodeInfo()
{
// 更新控件状态
CString ecLevel;
switch (qr.m_nEcLevel)
{
case 0:ecLevel = _T("L(7%)"); break;
case 1:ecLevel = _T("M(15%)"); break;
case 2:ecLevel = _T("Q(25%)"); break;
case 3:ecLevel = _T("H(30%)"); break;
default:break;
}
GetDlgItem(IDC_EDIT_EC_LEVEL)->SetWindowText(ecLevel);
SetWindowInt(GetDlgItem(IDC_EDIT_QR_VERSION), qr.m_nVersion);
SetWindowInt(GetDlgItem(IDC_EDIT_MASK_VERSION), qr.m_nMaskingNo);
SetWindowFloat(GetDlgItem(IDC_EDIT_MODULESIZE), qr.m_fModuleSize);
GetDlgItem(IDC_EDITSOURCEDATA_PUBLIC)->SetWindowText(m_strPublicString);
}
BEGIN_MESSAGE_MAP(CDlgQRDecode, CDialogEx)
ON_BN_CLICKED(IDC_BUTTON_DECODE, &CDlgQRDecode::OnBnClickedButtonDecode)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_BN_CLICKED(IDC_BUTTON_SAVE_IMAGE, &CDlgQRDecode::OnBnClickedButtonSaveImage)
ON_BN_CLICKED(IDC_BN_BACKGROUND, &CDlgQRDecode::OnBnClickedBnBackground)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, &CDlgQRDecode::OnBnClickedButtonClear)
END_MESSAGE_MAP()
// CDlgQRDecode 消息处理程序
void CDlgQRDecode::OnBnClickedButtonDecode()
{
BOOL result = Decode();
TRACE(" * 解码结果: %d\n", result);
}
BOOL CDlgQRDecode::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 工具提示
EnableToolTips(true);
m_ToolTip.Create(this);
m_ToolTip.Activate(true);
m_ToolTip.SetDelayTime(TTDT_INITIAL, 200); //延迟200ms后显示
m_ToolTip.SetDelayTime(TTDT_AUTOPOP, 3000);//显示持续3000 ms
m_ToolTip.AddTool(GetDlgItem(IDC_CHECK_USE_HYBRID), _T("启用混合二值化"));
m_ToolTip.AddTool(GetDlgItem(IDC_CHECK_TRY_HARDER), _T("启用强力搜索功能"));
m_ToolTip.AddTool(GetDlgItem(IDC_EDIT_EC_LEVEL), _T("QR码纠错能力的大小"));
m_ToolTip.AddTool(GetDlgItem(IDC_EDIT_MODULESIZE), _T("模块大小通常大于1"));
m_ToolTip.AddTool(GetDlgItem(IDC_EDIT_QR_VERSION), _T("QR码版本从1到40"));
m_ToolTip.AddTool(GetDlgItem(IDC_EDIT_MASK_VERSION), _T("QR码共有8种掩码"));
m_ToolTip.AddTool(GetDlgItem(IDC_BUTTON_DECODE), _T("对当前图像解码"));
m_ToolTip.AddTool(GetDlgItem(IDC_BUTTON_SAVE_IMAGE), _T("保存这个二维码"));
m_ToolTip.AddTool(GetDlgItem(IDC_BUTTON_CLEAR), _T("清除解码结果"));
m_ToolTip.AddTool(GetDlgItem(IDC_EDIT_INNER_ECLEVEL), _T("密文的纠错等级"));
m_ToolTip.AddTool(GetDlgItem(IDC_EDIT_INNER_MASK), _T("密文的掩码版本"));
m_ToolTip.AddTool(GetDlgItem(IDC_BN_BACKGROUND), _T("设置背景色(默认黑色)"));
return TRUE;
}
void CDlgQRDecode::OnOK()
{
TRACE(" * OnOK is called.\n");
return;
}
BOOL CDlgQRDecode::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
return TRUE;
return CDialogEx::PreTranslateMessage(pMsg);
}
void CDlgQRDecode::OnPaint()
{
CPaintDC dc(this); // device context for painting
}
/// 使得绘制图像不闪烁
BOOL CDlgQRDecode::OnEraseBkgnd(CDC* pDC)
{
return CDialog::OnEraseBkgnd(pDC);
}
void CDlgQRDecode::OnBnClickedButtonSaveImage()
{
if (!m_pImage->IsNull())
{
CyImage *roi = m_pImage->ROI(m_roi);
roi->Save(this);
delete roi;
}
}
void CDlgQRDecode::UpdateecLevelTipText(int nEcLevel)
{
switch (nEcLevel)
{
case 1:m_ToolTip.UpdateTipText(_T("1/15"), GetDlgItem(IDC_EDIT_INNER_ECLEVEL)); break;
case 2:m_ToolTip.UpdateTipText(_T("2/15"), GetDlgItem(IDC_EDIT_INNER_ECLEVEL)); break;
case 3:m_ToolTip.UpdateTipText(_T("3/15"), GetDlgItem(IDC_EDIT_INNER_ECLEVEL)); break;
case 4:m_ToolTip.UpdateTipText(_T("4/15"), GetDlgItem(IDC_EDIT_INNER_ECLEVEL)); break;
case 5:m_ToolTip.UpdateTipText(_T("5/15"), GetDlgItem(IDC_EDIT_INNER_ECLEVEL)); break;
case 6:m_ToolTip.UpdateTipText(_T("6/15"), GetDlgItem(IDC_EDIT_INNER_ECLEVEL)); break;
case 7:m_ToolTip.UpdateTipText(_T("7/15"), GetDlgItem(IDC_EDIT_INNER_ECLEVEL)); break;
default:break;
}
}
void CDlgQRDecode::Reset()
{
m_pImage = NULL;
m_strPublicString.Empty();
m_strPrivateString.Empty();
GetDlgItem(IDC_EDIT_EC_LEVEL)->SetWindowText(_T(""));
GetDlgItem(IDC_EDIT_MODULESIZE)->SetWindowText(_T(""));
GetDlgItem(IDC_EDIT_QR_VERSION)->SetWindowText(_T(""));
GetDlgItem(IDC_EDIT_MASK_VERSION)->SetWindowText(_T(""));
GetDlgItem(IDC_EDIT_INNER_ECLEVEL)->SetWindowText(_T(""));
GetDlgItem(IDC_EDIT_INNER_MASK)->SetWindowText(_T(""));
GetDlgItem(IDC_EDITSOURCEDATA_PUBLIC)->SetWindowText(_T(""));
GetDlgItem(IDC_EDITSOURCEDATA_PRIVATE)->SetWindowText(_T(""));
}
void CDlgQRDecode::OnBnClickedBnBackground()
{
CColorDialog m_setClrDlg;
m_setClrDlg.m_cc.Flags |= CC_FULLOPEN | CC_RGBINIT;
m_setClrDlg.m_cc.rgbResult = m_BackgroundColor;
if (IDOK == m_setClrDlg.DoModal())
{
if (m_BackgroundColor == m_setClrDlg.m_cc.rgbResult)
return;
m_BackgroundColor = m_setClrDlg.m_cc.rgbResult;
}
}
void CDlgQRDecode::OnBnClickedButtonClear()
{
Reset();
}