-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProgram.cs
More file actions
186 lines (165 loc) · 8.13 KB
/
Program.cs
File metadata and controls
186 lines (165 loc) · 8.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
using Dynamsoft.Core;
using Dynamsoft.CVR;
using Dynamsoft.DBR;
using Dynamsoft.DBR.intermediate_results;
using Dynamsoft.License;
using Dynamsoft.Utility;
using System;
using System.Collections.Generic;
using System.IO;
namespace ShowLocalizedVSDecodedBarcodes
{
internal class MyIntermediateResultReceiver : IntermediateResultReceiver
{
public List<Quadrilateral> locations = new List<Quadrilateral>();
public override void OnLocalizedBarcodesReceived(LocalizedBarcodesUnit result, IntermediateResultExtraInfo info)
{
if (info.isSectionLevelResult)
{
LocalizedBarcodeElement[] elements = result.GetLocalizedBarcodes();
foreach (var element in elements)
locations.Add(element.GetLocation());
}
}
}
internal class Program
{
private static bool IsWithin10Percent(int area1, int area2)
{
if (area1 == 0 || area2 == 0)
return false;
double ratio = (double)Math.Abs(area1 - area2) / Math.Max(area1, area2);
return ratio <= 0.10;
}
private static bool SeemAsSameLocation(Quadrilateral location, Quadrilateral resultLoc)
{
int x = 0, y = 0;
foreach (var point in location.points)
{
x += point[0];
y += point[1];
}
x = (x + location.points.Length / 2) / location.points.Length;
y = (y + location.points.Length / 2) / location.points.Length;
if (!resultLoc.Contains(new Point(x, y)))
return false;
if (IsWithin10Percent(location.GetArea(), resultLoc.GetArea()))
return true;
return false;
}
private static List<Quadrilateral> RemoveIfLocExistsInResultLocs(List<Quadrilateral> locations, List<Quadrilateral> resultLocs)
{
if (locations == null || resultLocs == null)
return null;
List<Quadrilateral> retLoc = new List<Quadrilateral>();
HashSet<int> excludedLoc = new HashSet<int>();
for (int i = 0; i < locations.Count; i++)
{
for (int j = 0; j < resultLocs.Count; j++)
{
if (SeemAsSameLocation(locations[i], resultLocs[j]))
{
excludedLoc.Add(i);
break;
}
}
}
for (int i = 0; i < locations.Count; i++)
{
if (!excludedLoc.Contains(i))
retLoc.Add(locations[i]);
}
return retLoc;
}
private static ImageData DrawOnImage(ImageData image, List<Quadrilateral> locations, List<Quadrilateral> resultLocs)
{
ImageDrawer drawer = new ImageDrawer();
locations = RemoveIfLocExistsInResultLocs(locations, resultLocs);
if (locations != null)
image = drawer.DrawOnImage(image, locations.ToArray(), unchecked((int)0xFFFF0000), 2);
if (resultLocs != null)
image = drawer.DrawOnImage(image, resultLocs.ToArray(), unchecked((int)0xFF00FF00), 2);
return image;
}
public static void Main(string[] args)
{
System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
int errorCode = 1;
string errorMsg;
// 1. 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
{
// 2. Create an instance of CaptureVisionRouter.
using (CaptureVisionRouter cvRouter = new CaptureVisionRouter())
{
IntermediateResultManager irm = cvRouter.GetIntermediateResultManager();
MyIntermediateResultReceiver irr = new MyIntermediateResultReceiver();
irm.AddResultReceiver(irr);
// 3. Replace by your own image path.
string imagePath = "../../../../../Images/GeneralBarcodes.png";
ImageIO imageIO = new ImageIO();
// 4. Read image from file.
ImageData image = imageIO.ReadFromFile(imagePath, out errorCode);
if (errorCode == (int)EnumErrorCode.EC_OK)
{
cvRouter.GetSimplifiedSettings(PresetTemplate.PT_READ_BARCODES, out SimplifiedCaptureVisionSettings settings);
settings.outputOriginalImage = 1;
cvRouter.UpdateSettings(PresetTemplate.PT_READ_BARCODES, settings);
// 5. Decode barcodes from the image.
CapturedResult result = cvRouter.Capture(image, PresetTemplate.PT_READ_BARCODES);
foreach (CapturedResultItem capturedResultItem in result.GetItems())
{
if (capturedResultItem is OriginalImageResultItem originalImageResultItem)
{
image = originalImageResultItem.GetImageData();
break;
}
}
// 6. Check error code.
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());
List<Quadrilateral> resultLocs = new List<Quadrilateral>();
DecodedBarcodesResult decodedBarcodes = result.GetDecodedBarcodesResult();
BarcodeResultItem[] decodedItems = decodedBarcodes != null ? decodedBarcodes.GetItems() : null;
if (decodedItems != null && decodedItems.Length > 0)
{
foreach (BarcodeResultItem item in decodedItems)
resultLocs.Add(item.GetLocation());
}
// 7. Draw the outline border of the barcodes on the image.
// The barcodes that have been decode successfully are marked as green(0xFF00FF00)
// The barcodes that only have been localized are marked as red(0xFFFF0000)
if (irr.locations.Count > 0 || resultLocs.Count > 0)
{
ImageData imageComplete = DrawOnImage(image, irr.locations, resultLocs);
// 8. Save the image.
string resultPath = "result.png";
imageIO.SaveToFile(imageComplete, resultPath);
Console.WriteLine("Image saved to: " + Path.GetFullPath(resultPath));
}
else
{
Console.WriteLine("No barcodes detected.");
}
}
else
{
Console.WriteLine("Failed to read image.");
}
}
}
Console.WriteLine("Press Enter to quit...");
Console.ReadLine();
}
}
}