FrmCodeLearn.cs
6.5 KB
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
using HalconDotNet;
using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CodeLibrary;
namespace CodeTest
{
public partial class FrmCodeLearn : Form
{
public FrmCodeLearn()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
string filePath = txtParamPath.Text;
string cameraName = cmbCameraList.Text;
string codeType = this.cmbCodeType.Text;
int count = cmbCount.SelectedIndex + 1;
if (chbUseCamera.Checked)
{
if (cameraName.Equals(""))
{
MessageBox.Show("请选择摄像机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
else
{
cameraName = "";
if (pictureBox1.Image == null)
{
MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Bitmap bitmap = new Bitmap( pictureBox1.Image);
HObject Image = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
HDCodeLearnHelper.DefaultImage = Image;
}
Task.Factory.StartNew(delegate ()
{
HDCodeLearnHelper.StartLearn(this.hWindowControl1.HalconWindow, cameraName, codeType, filePath, count,5000);
});
FormStatus(true);
}
private void FormStatus(bool open)
{
btnOpen.Enabled = !open;
btnStop.Enabled = open;
}
private void btnStop_Click(object sender, EventArgs e)
{
HDCodeLearnHelper.StopLearn();
FormStatus(false);
}
private void FrmCamera_Load(object sender, EventArgs e)
{
cmbCount.SelectedIndex = 0;
chbTest .Checked= HDCodeLearnHelper.isNeedTest;
CheckForIllegalCrossThreadCalls = false;
LogUtil.logBox = this.richTextBox2;
HDCodeLearnHelper.LoadConfig(ConfigAppSettings.GetValue(Setting_Init.CameraName),ConfigAppSettings.GetValue(Setting_Init.CodeType));
FormStatus(false);
cmbCameraList.DataSource = HDCodeLearnHelper.cameraNameList;
if (HDCodeLearnHelper.cameraNameList.Count > 0)
{
cmbCameraList.SelectedIndex = 0;
}
cmbCodeType.DataSource = HDCodeLearnHelper.codeTypeList;
if (HDCodeLearnHelper.codeTypeList.Count > 0)
{
cmbCodeType.SelectedIndex = 0;
}
else
{
cmbCodeType.Items.Add("QR Code");
cmbCodeType.SelectedIndex = 0;
}
string filePath =FrmMain.GetParamFilePathName(cmbCodeType.Text);
txtParamPath.Text = filePath;
cmbCount.SelectedIndex = 0;
timer1.Start();
}
private void FrmCamera_FormClosed(object sender, FormClosedEventArgs e)
{
if (btnStop.Enabled.Equals(true))
{
btnStop_Click(null, null);
LogUtil.logBox = null;
FormStatus(false);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (HDCodeLearnHelper.IsRun)
{
if (btnOpen.Enabled)
{
btnOpen.Enabled = false ;
}
}
else
{
if (btnOpen.Enabled.Equals(false))
{
btnOpen.Enabled = true;
}
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void cmbCodeType_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbCodeType.SelectedIndex >= 0)
{
txtParamPath.Text = FrmMain.GetParamFilePathName(cmbCodeType.Text );
}
}
private void btnClearLog_Click(object sender, EventArgs e)
{
LogUtil.ClearLog();
}
private void chbTest_CheckedChanged(object sender, EventArgs e)
{
HDCodeLearnHelper.isNeedTest = chbTest.Checked;
}
private void btnSelImage_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
openDialog.Title = "选择二维码图片";
openDialog.Filter = "图片(*.png)|*.png|图片(*.jpg)|*.jpg";
//openDialog.DefaultExt = "png";
System.Windows.Forms.DialogResult result = openDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
txtPath.Text = openDialog.FileName;
string filename = txtPath.Text;
if (string.IsNullOrEmpty(filename))
{
MessageBox.Show("获取二维码图片为空");
}
pictureBox1.Image = null;
//读取图片内容
Image img = (Image)Image.FromFile(filename).Clone();
pictureBox1.Image = img;
}
private void txtPath_TextChanged(object sender, EventArgs e)
{
}
private void chbUseCamera_CheckedChanged(object sender, EventArgs e)
{
if (chbUseCamera.Checked)
{
cmbCameraList.Enabled = true ;
btnSelImage.Enabled = false;
}
else
{
cmbCameraList.Enabled = false ;
btnSelImage.Enabled = true ;
}
}
private void btnDelOld_Click(object sender, EventArgs e)
{
string path = txtParamPath.Text;
if (path.Equals(""))
{
return;
}
FileInfo file = new FileInfo(path);
DialogResult result = MessageBox.Show("确定删除文件:" +file.Name+ "", "确认删除", MessageBoxButtons.YesNo);
if (result.Equals(DialogResult.Yes))
{
File.Delete(path);
}
}
}
}