-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
161 lines (152 loc) · 6.86 KB
/
Copy pathProgram.cs
File metadata and controls
161 lines (152 loc) · 6.86 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
using Dynamsoft.Core;
using Dynamsoft.CVR;
using Dynamsoft.DBR;
using Dynamsoft.License;
using Dynamsoft.Utility;
using OpenCvSharp;
using System.Runtime.InteropServices;
namespace VideoDecoding
{
internal class MyCapturedResultReceiver : CapturedResultReceiver
{
public override void OnDecodedBarcodesReceived(DecodedBarcodesResult result)
{
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()}");
}
var tag = result.GetOriginalImageTag();
var items = result.GetItems();
if (items.Length > 0)
{
if (tag != null)
{
Console.WriteLine($"ImageID: {tag.GetImageId()}");
}
Console.WriteLine($"Decoded {items.Length} barcodes");
for (int i = 0; i < items.Length; i++)
{
Console.WriteLine($"Result {i}:");
Console.WriteLine($"Barcode Format: {items[i].GetFormatString()}");
Console.WriteLine($"Barcode Text: {items[i].GetText()}");
}
Console.WriteLine();
}
}
}
internal class MyVideoFetcher : ImageSourceAdapter
{
public override bool HasNextImageToFetch()
{
return true;
}
public void MyAddImageToBuffer(ImageData imageData)
{
AddImageToBuffer(imageData);
}
}
internal class Program
{
private static TimeSpan _intervalMs = TimeSpan.FromMilliseconds(33);
static void Main(string[] args)
{
Console.WriteLine("Opening camera...");
try
{
VideoCapture capture = new VideoCapture(0);
if (!capture.IsOpened())
{
Console.WriteLine("ERROR: Can't initialize camera capture.");
return;
}
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())
{
MyVideoFetcher fetcher = new MyVideoFetcher();
fetcher.SetMaxImageCount(100);
fetcher.SetBufferOverflowProtectionMode(EnumBufferOverflowProtectionMode.BOPM_UPDATE);
fetcher.SetColourChannelUsageType(EnumColourChannelUsageType.CCUT_AUTO);
errorCode = cvRouter.SetInput(fetcher);
MyCapturedResultReceiver crr = new MyCapturedResultReceiver();
cvRouter.AddResultReceiver(crr);
errorCode = cvRouter.StartCapturing(PresetTemplate.PT_READ_BARCODES, false, out errorMsg);
if (errorCode != (int)EnumErrorCode.EC_OK)
{
Console.WriteLine($"StartCapturing failed: ErrorCode: {errorCode}, ErrorString: {errorMsg}");
return;
}
var mat = new Mat();
int i = 0;
while (true)
{
i++;
var start = DateTime.Now;
capture.Read(mat);
if (mat.Empty())
{
Console.WriteLine("ERROR: Can't grab camera frame.");
continue;
}
int height = mat.Rows;
int width = mat.Cols;
int stride = (int)mat.Step();
int channels = mat.Channels();
int totalBytes = height * stride;
byte[] data = new byte[totalBytes];
Marshal.Copy(mat.Data, data, 0, totalBytes);
VideoFrameTag tag = new VideoFrameTag();
tag.SetImageId(i);
using ImageData imageData = new ImageData(
data,
width,
height,
stride,
EnumImagePixelFormat.IPF_RGB_888);
imageData.SetImageTag(tag);
fetcher.MyAddImageToBuffer(imageData);
Cv2.ImShow("Frame", mat);
var end = DateTime.Now;
var usingTime = end - start;
int key;
if (usingTime < _intervalMs)
{
key = Cv2.WaitKey((int)(_intervalMs - usingTime).TotalMilliseconds);
}
else
{
key = Cv2.WaitKey(1);
}
if (key == 27)
break;
}
mat.Dispose();
cvRouter.StopCapturing(false, true);
}
}
Console.WriteLine("Press Enter to quit...");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message);
return;
}
}
}
}