Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
280 lines (254 loc) · 13 KB
/
Copy pathProgram.cs
File metadata and controls
280 lines (254 loc) · 13 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
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
using Dynamsoft.Core;
using Dynamsoft.CVR;
using Dynamsoft.DBR;
using Dynamsoft.License;
using Dynamsoft.Utility;
using System;
using System.IO;
namespace ParameterTuner
{
internal class Program
{
private static int InputImage(out string imagePath)
{
int imageChoice = 0;
imagePath = string.Empty;
ImageIO imageIO = new ImageIO();
while (true)
{
Console.WriteLine(
"\nAvailable Sample Scenarios:\n" +
"[1] Blurred 1D barcode\n" +
"[2] Multiple barcodes\n" +
"[3] Colour Inverted Barcode\n" +
"[4] Direct Part Marking\n" +
"[5] Retail 1D barcode\n" +
"[6] Industrial 1D barcode\n" +
"[7] Custom image\n");
Console.WriteLine("Enter the number of the image to test, or provide a full path to your own image:");
string strChoice = Console.ReadLine();
if (int.TryParse(strChoice, out imageChoice))
{
if (imageChoice == 1)
imagePath = "../../../../../Images/blurry.png";
else if (imageChoice == 2)
imagePath = "../../../../../Images/GeneralBarcodes.png";
else if (imageChoice == 3)
imagePath = "../../../../../Images/inverted-barcode.png";
else if (imageChoice == 4)
imagePath = "../../../../../Images/DPM.png";
else if (imageChoice == 5)
imagePath = "../../../../../Images/EAN-13.jpg";
else if (imageChoice == 6)
imagePath = "../../../../../Images/OneDIndustrial.jpg";
else if (imageChoice == 7)
{
Console.WriteLine("Please enter the full path to your custom image:");
imagePath = Console.ReadLine();
}
else
{
Console.WriteLine("Invalid choice. Please try again.");
continue;
}
}
else
{
imagePath = strChoice;
}
imagePath = imagePath.Trim('\"');
if (!System.IO.File.Exists(imagePath))
{
Console.WriteLine("The specified image file does not exist. Please try again.");
continue;
}
imagePath = Path.GetFullPath(imagePath);
return imageChoice;
}
}
private static void InputTemplate(CaptureVisionRouter cvRouter, int imageChoice, out string templatePath)
{
string firstTemplateDescription = null;
switch (imageChoice)
{
case 1: firstTemplateDescription = "[1] ReadBlurry1DBarcode.json (Suitable for blurred 1D barcode)"; break;
case 2: firstTemplateDescription = "[1] ReadMultipleBarcode.json (Suitable for multiple barcodes)"; break;
case 3: firstTemplateDescription = "[1] ReadInvertedBarcode.json (Suitable for colour inverted barcode)"; break;
case 4: firstTemplateDescription = "[1] ReadDPM.json (Suitable for Direct Part Marking barcode)"; break;
case 5: firstTemplateDescription = "[1] ReadOneDRetail.json (Suitable for retail 1D barcode such as EAN13, UPC-A)"; break;
case 6: firstTemplateDescription = "[1] ReadOneDIndustrial.json (Suitable for industrial 1D barcode such as Code128, Code39)"; break;
default: firstTemplateDescription = null; break;
}
templatePath = string.Empty;
while (true)
{
Console.WriteLine(
"\nSelect template for this image:\n" +
(firstTemplateDescription != null ? firstTemplateDescription + "\n" : "") +
"[2] ReadBarcodes_Default.json (General purpose settings)\n" +
"[3] Custom template (Use your own template)\n");
Console.WriteLine("Enter the number of the template to test, or provide a full path to your own template:");
string strChoice = Console.ReadLine();
if (int.TryParse(strChoice, out int number))
{
if (number == 1 && firstTemplateDescription != null)
{
switch (imageChoice)
{
case 1: templatePath = "../../../../../CustomTemplates/ReadBlurry1DBarcode.json"; break;
case 2: templatePath = "../../../../../CustomTemplates/ReadMultipleBarcode.json"; break;
case 3: templatePath = "../../../../../CustomTemplates/ReadInvertedBarcode.json"; break;
case 4: templatePath = "../../../../../CustomTemplates/ReadDPM.json"; break;
case 5: templatePath = "../../../../../CustomTemplates/ReadOneDRetail.json"; break;
case 6: templatePath = "../../../../../CustomTemplates/ReadOneDIndustrial.json"; break;
default: templatePath = ""; break;
}
}
else if (number == 2)
{
templatePath = "../../../../../CustomTemplates/ReadBarcodes_Default.json";
}
else if (number == 3)
{
Console.WriteLine("Please enter the full path to your custom template:");
templatePath = Console.ReadLine();
}
else
{
Console.WriteLine("Invalid choice. Please try again.");
continue;
}
}
else
{
templatePath = strChoice;
}
templatePath = templatePath.Trim('\"');
if (!System.IO.File.Exists(templatePath))
{
Console.WriteLine("The specified template file does not exist. Please try again.");
continue;
}
templatePath = Path.GetFullPath(templatePath);
int errorCode = cvRouter.InitSettingsFromFile(templatePath, out string errorMsg);
if (errorCode == (int)EnumErrorCode.EC_UNSUPPORTED_JSON_KEY_WARNING)
{
Console.WriteLine("Warning: " + errorCode + ", " + errorMsg);
}
else if (errorCode != (int)EnumErrorCode.EC_OK)
{
Console.WriteLine($"\nFailed to initialize settings, Error: {errorCode}, {errorMsg}. Please try again.");
continue;
}
return;
}
}
private static int InputNextState()
{
while (true)
{
Console.WriteLine(
"\nWhat would you like to do next?\n" +
"[1] Try a different template\n" +
"[2] Load another image\n" +
"[3] Exit\n");
Console.WriteLine("Enter your choice:");
string strChoice = Console.ReadLine();
if (int.TryParse(strChoice, out int number))
{
if (number >= 1 && number <= 3)
return number;
else
Console.WriteLine("Invalid choice. Please try again.");
}
else
{
Console.WriteLine("Invalid input. Please enter a number.");
}
}
}
internal static void Main(string[] args)
{
System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
int errorCode = 1;
string errorMsg;
// Initialize license.
// You can request and extend a trial license from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=samples&package=dotnet
// The string 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9' here is a free public trial license. Note that network connection is required for this license to work.
errorCode = LicenseManager.InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", out errorMsg);
if (errorCode != (int)EnumErrorCode.EC_OK && errorCode != (int)EnumErrorCode.EC_UNSUPPORTED_JSON_KEY_WARNING)
{
Console.WriteLine("License initialization failed: ErrorCode: " + errorCode + ", ErrorString: " + errorMsg);
}
else
{
Console.WriteLine("Welcome to ParameterTuner!");
int state = 2;
int imageChoice = 0;
string imagePath = string.Empty;
string templatePath = string.Empty;
while (state != 3)
{
if (state == 2)
{
// 2. Input image.
imageChoice = InputImage(out imagePath);
}
using (CaptureVisionRouter cvRouter = new CaptureVisionRouter())
{
// 3. Input template.
InputTemplate(cvRouter, imageChoice, out templatePath);
// 4. Decode barcodes from the image.
DateTime beginTime = DateTime.Now;
CapturedResult[] results = cvRouter.CaptureMultiPages(imagePath);
DateTime endTime = DateTime.Now;
TimeSpan timeElapsed = endTime - beginTime;
Console.WriteLine(
"\nRunning with\n" +
$"Image: {imagePath}\n" +
$"Template: {templatePath}");
if (results == null || results.Length == 0)
{
Console.WriteLine("No captured result.");
}
else
{
for (int index = 0; index < results.Length; index++)
{
CapturedResult result = results[index];
if (result.GetErrorCode() == (int)EnumErrorCode.EC_UNSUPPORTED_JSON_KEY_WARNING)
Console.WriteLine("Warning: " + result.GetErrorCode() + ", " + result.GetErrorString());
else if (result.GetErrorCode() != (int)EnumErrorCode.EC_OK)
Console.WriteLine("Error: " + result.GetErrorCode() + ", " + result.GetErrorString());
FileImageTag tag = result.GetOriginalImageTag() as FileImageTag;
int pageNumber = tag != null ? tag.GetPageNumber() : index;
// 5. Output the barcode format and barcode text.
DecodedBarcodesResult barcodeResult = result.GetDecodedBarcodesResult();
BarcodeResultItem[] items = barcodeResult != null ? barcodeResult.GetItems() : null;
if (items == null || items.Length == 0)
{
Console.WriteLine("Page-" + (pageNumber + 1) + " No barcode found.");
}
else
{
Console.WriteLine("Page-" + (pageNumber + 1) + " Decoded " + items.Length + " barcodes.");
for (int i = 0; i < items.Length; i++)
{
Console.WriteLine("Result " + (i + 1));
Console.WriteLine("Barcode Format: " + items[i].GetFormatString());
Console.WriteLine("Barcode Text: " + items[i].GetText());
}
}
}
}
Console.WriteLine($"Time used: {timeElapsed.TotalMilliseconds} ms...");
}
state = InputNextState();
}
}
Console.WriteLine("Press Enter to quit...");
Console.ReadLine();
}
}
}