-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathForm1.cs
629 lines (531 loc) · 22.7 KB
/
Form1.cs
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
using System;
using System.Drawing;
using System.Windows.Forms;
using Dynamsoft.DBR;
using Dynamsoft.Core;
using Dynamsoft.CVR;
using Dynamsoft.License;
using DirectShowLib;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Drawing.Imaging;
namespace BarcodeReaderApp
{
public partial class Form1 : Form, ISampleGrabberCB
{
private CaptureVisionRouter cvr;
private int _previewWidth = 640;
private int _previewHeight = 480;
private int _previewStride = 0;
private int _previewFPS = 30;
private volatile bool isFinished = true;
private bool isCameraMode = false;
IVideoWindow videoWindow = null;
IMediaControl mediaControl = null;
IFilterGraph2 graphBuilder = null;
ICaptureGraphBuilder2 captureGraphBuilder = null;
DsROTEntry rot = null;
public Form1()
{
InitializeComponent();
// Initialize Dynamsoft Barcode Reader
string errorMsg;
int errorCode = LicenseManager.InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==", out errorMsg);
if (errorCode != (int)EnumErrorCode.EC_OK)
Console.WriteLine("License initialization error: " + errorMsg);
cvr = new CaptureVisionRouter();
}
private void button1_Click(object sender, EventArgs e)
{
isCameraMode = false;
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
//dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
Bitmap bitmap = null;
try
{
bitmap = new Bitmap(dlg.FileName);
}
catch (Exception exception)
{
MessageBox.Show("File not supported.");
return;
}
pictureBox1.Image = new Bitmap(dlg.FileName);
ReadBarcode((Bitmap)pictureBox1.Image);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
isCameraMode = true;
string button_text = button3.Text;
if (button_text.Equals("Start Webcam"))
{
button3.Text = "Stop Webcam";
StartCamera();
}
else
{
button3.Text = "Start Webcam";
StopCamera();
}
}
private void StartCamera()
{
// Get connected devices
DsDevice[] devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
if (devices.Length == 0)
{
MessageBox.Show("No USB webcam connected");
button3.Text = "Start Webcam";
return;
}
else
{
//DsDevice dev = devices[0] as DsDevice;
//MessageBox.Show("Device: " + dev.Name);
//CaptureVideo();
CaptureVideo(devices[0]);
}
}
private void StopCamera()
{
button3.Text = "Start Webcam";
CloseInterfaces();
}
private Bitmap EnsureNonIndexedFormat(Bitmap original)
{
if (original.PixelFormat == PixelFormat.Format24bppRgb ||
original.PixelFormat == PixelFormat.Format32bppArgb ||
original.PixelFormat == PixelFormat.Format32bppPArgb)
{
return original;
}
// Convert to 24bppRgb format
Bitmap newBitmap = new Bitmap(original.Width, original.Height, PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(newBitmap))
{
g.DrawImage(original, 0, 0);
}
return newBitmap;
}
private void GetBitmapData(Bitmap bitmap, out byte[] bytes, out int width, out int height, out int stride, out PixelFormat pixelFormat)
{
// Lock the bitmap's bits
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
// Get the address of the first line
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap
int bytesCount = Math.Abs(bmpData.Stride) * bitmap.Height;
bytes = new byte[bytesCount];
// Copy the RGB values into the array
System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, bytesCount);
// Retrieve the data
width = bitmap.Width;
height = bitmap.Height;
stride = bmpData.Stride;
pixelFormat = bitmap.PixelFormat;
// Unlock the bits
bitmap.UnlockBits(bmpData);
}
private void ReadBarcode(Bitmap bitmap)
{
Bitmap nonIndexedBitmap = EnsureNonIndexedFormat(bitmap);
// Convert Bitmap to ImageData
byte[] bytes;
int width;
int height;
int stride;
PixelFormat pixelFormat;
GetBitmapData(nonIndexedBitmap, out bytes, out width, out height, out stride, out pixelFormat);
EnumImagePixelFormat format = EnumImagePixelFormat.IPF_RGB_888;
switch (pixelFormat)
{
case PixelFormat.Format24bppRgb:
format = EnumImagePixelFormat.IPF_RGB_888;
break;
case PixelFormat.Format32bppArgb:
format = EnumImagePixelFormat.IPF_ARGB_8888;
break;
default:
MessageBox.Show("Unsupported pixel format.");
return;
}
ImageData data = new ImageData(bytes, width, height, stride, format);
CapturedResult result = cvr.Capture(data, PresetTemplate.PT_READ_BARCODES);
textBox1.Clear();
if (result == null)
{
MessageBox.Show("No barcode detected.");
}
else if (result.GetErrorCode() != 0)
{
MessageBox.Show("Error: " + result.GetErrorCode() + ", " + result.GetErrorString());
}
else
{
DecodedBarcodesResult barcodesResult = result.GetDecodedBarcodesResult();
if (barcodesResult != null)
{
BarcodeResultItem[] items = barcodesResult.GetItems();
foreach (BarcodeResultItem barcodeItem in items)
{
Console.WriteLine("Result " + (Array.IndexOf(items, barcodeItem) + 1));
Console.WriteLine("Barcode Format: " + barcodeItem.GetFormatString());
Console.WriteLine("Barcode Text: " + barcodeItem.GetText());
}
using (Graphics g = Graphics.FromImage(nonIndexedBitmap))
{
foreach (BarcodeResultItem barcodeItem in items)
{
string output = "Text: " + barcodeItem.GetFormatString() + Environment.NewLine + "Format: " + barcodeItem.GetFormatString() + Environment.NewLine;
Dynamsoft.Core.Point[] points = barcodeItem.GetLocation().points;
// Draw lines based on four points
for (int i = 0; i < points.Length; i++)
{
Dynamsoft.Core.Point p1 = points[i];
Dynamsoft.Core.Point p2 = points[(i + 1) % points.Length];
g.DrawLine(new Pen(Color.Red, 3), p1[0], p1[1], p2[0], p2[1]);
}
textBox1.AppendText(output);
}
}
if (!isCameraMode)
{
pictureBox1.Image = nonIndexedBitmap;
}
}
else
{
textBox1.AppendText("No barcode detected!" + Environment.NewLine);
}
}
}
public void CaptureVideo(DsDevice device)
{
pictureBox1.Image = null;
int hr = 0;
IBaseFilter sourceFilter = null;
ISampleGrabber sampleGrabber = null;
try
{
// Get DirectShow interfaces
GetInterfaces();
// Attach the filter graph to the capture graph
hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder);
DsError.ThrowExceptionForHR(hr);
// Use the system device enumerator and class enumerator to find
// a video capture/preview device, such as a desktop USB video camera.
sourceFilter = SelectCaptureDevice(device);
// Add Capture filter to graph.
hr = this.graphBuilder.AddFilter(sourceFilter, "Video Capture");
DsError.ThrowExceptionForHR(hr);
// Initialize SampleGrabber.
sampleGrabber = new SampleGrabber() as ISampleGrabber;
// Configure SampleGrabber. Add preview callback.
ConfigureSampleGrabber(sampleGrabber);
// Add SampleGrabber to graph.
hr = this.graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "Frame Callback");
DsError.ThrowExceptionForHR(hr);
// Configure preview settings.
SetConfigParams(this.captureGraphBuilder, sourceFilter, _previewFPS, _previewWidth, _previewHeight);
// Render the preview
hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, (sampleGrabber as IBaseFilter), null);
DsError.ThrowExceptionForHR(hr);
SaveSizeInfo(sampleGrabber);
// Set video window style and position
SetupVideoWindow();
// Add our graph to the running object table, which will allow
// the GraphEdit application to "spy" on our graph
rot = new DsROTEntry(this.graphBuilder);
// Start previewing video data
hr = this.mediaControl.Run();
DsError.ThrowExceptionForHR(hr);
}
catch
{
MessageBox.Show("An unrecoverable error has occurred.");
}
finally
{
if (sourceFilter != null)
{
Marshal.ReleaseComObject(sourceFilter);
sourceFilter = null;
}
if (sampleGrabber != null)
{
Marshal.ReleaseComObject(sampleGrabber);
sampleGrabber = null;
}
}
}
public void CaptureVideo()
{
pictureBox1.Image = null;
int hr = 0;
IBaseFilter sourceFilter = null;
ISampleGrabber sampleGrabber = null;
try
{
// Get DirectShow interfaces
GetInterfaces();
// Attach the filter graph to the capture graph
hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder);
DsError.ThrowExceptionForHR(hr);
// Use the system device enumerator and class enumerator to find
// a video capture/preview device, such as a desktop USB video camera.
sourceFilter = FindCaptureDevice();
// Add Capture filter to graph.
hr = this.graphBuilder.AddFilter(sourceFilter, "Video Capture");
DsError.ThrowExceptionForHR(hr);
// Initialize SampleGrabber.
sampleGrabber = new SampleGrabber() as ISampleGrabber;
// Configure SampleGrabber. Add preview callback.
ConfigureSampleGrabber(sampleGrabber);
// Add SampleGrabber to graph.
hr = this.graphBuilder.AddFilter(sampleGrabber as IBaseFilter, "Frame Callback");
DsError.ThrowExceptionForHR(hr);
// Configure preview settings.
SetConfigParams(this.captureGraphBuilder, sourceFilter, _previewFPS, _previewWidth, _previewHeight);
// Render the preview
hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, (sampleGrabber as IBaseFilter), null);
DsError.ThrowExceptionForHR(hr);
SaveSizeInfo(sampleGrabber);
// Set video window style and position
SetupVideoWindow();
// Add our graph to the running object table, which will allow
// the GraphEdit application to "spy" on our graph
rot = new DsROTEntry(this.graphBuilder);
// Start previewing video data
hr = this.mediaControl.Run();
DsError.ThrowExceptionForHR(hr);
}
catch
{
MessageBox.Show("An unrecoverable error has occurred.");
}
finally
{
if (sourceFilter != null)
{
Marshal.ReleaseComObject(sourceFilter);
sourceFilter = null;
}
if (sampleGrabber != null)
{
Marshal.ReleaseComObject(sampleGrabber);
sampleGrabber = null;
}
}
}
public IBaseFilter SelectCaptureDevice(DsDevice device)
{
object source = null;
Guid iid = typeof(IBaseFilter).GUID;
device.Mon.BindToObject(null, null, ref iid, out source);
return (IBaseFilter)source;
}
public IBaseFilter FindCaptureDevice()
{
int hr = 0;
#if USING_NET11
UCOMIEnumMoniker classEnum = null;
UCOMIMoniker[] moniker = new UCOMIMoniker[1];
#else
IEnumMoniker classEnum = null;
IMoniker[] moniker = new IMoniker[1];
#endif
object source = null;
// Create the system device enumerator
ICreateDevEnum devEnum = (ICreateDevEnum)new CreateDevEnum();
// Create an enumerator for the video capture devices
hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, 0);
DsError.ThrowExceptionForHR(hr);
// The device enumerator is no more needed
Marshal.ReleaseComObject(devEnum);
// If there are no enumerators for the requested type, then
// CreateClassEnumerator will succeed, but classEnum will be NULL.
if (classEnum == null)
{
throw new ApplicationException("No video capture device was detected.\r\n\r\n" +
"This sample requires a video capture device, such as a USB WebCam,\r\n" +
"to be installed and working properly. The sample will now close.");
}
// Use the first video capture device on the device list.
// Note that if the Next() call succeeds but there are no monikers,
// it will return 1 (S_FALSE) (which is not a failure). Therefore, we
// check that the return code is 0 (S_OK).
#if USING_NET11
int i;
if (classEnum.Next (moniker.Length, moniker, IntPtr.Zero) == 0)
#else
if (classEnum.Next(moniker.Length, moniker, IntPtr.Zero) == 0)
#endif
{
// Bind Moniker to a filter object
Guid iid = typeof(IBaseFilter).GUID;
moniker[0].BindToObject(null, null, ref iid, out source);
}
else
{
throw new ApplicationException("Unable to access video capture device!");
}
// Release COM objects
Marshal.ReleaseComObject(moniker[0]);
Marshal.ReleaseComObject(classEnum);
// An exception is thrown if cast fail
return (IBaseFilter)source;
}
public void GetInterfaces()
{
int hr = 0;
// An exception is thrown if cast fail
this.graphBuilder = (IFilterGraph2)new FilterGraph();
this.captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
this.mediaControl = (IMediaControl)this.graphBuilder;
this.videoWindow = (IVideoWindow)this.graphBuilder;
DsError.ThrowExceptionForHR(hr);
}
public void CloseInterfaces()
{
if (mediaControl != null)
{
int hr = mediaControl.StopWhenReady();
DsError.ThrowExceptionForHR(hr);
}
if (videoWindow != null)
{
videoWindow.put_Visible(OABool.False);
videoWindow.put_Owner(IntPtr.Zero);
}
// Remove filter graph from the running object table.
if (rot != null)
{
rot.Dispose();
rot = null;
}
// Release DirectShow interfaces.
Marshal.ReleaseComObject(this.mediaControl); this.mediaControl = null;
Marshal.ReleaseComObject(this.videoWindow); this.videoWindow = null;
Marshal.ReleaseComObject(this.graphBuilder); this.graphBuilder = null;
Marshal.ReleaseComObject(this.captureGraphBuilder); this.captureGraphBuilder = null;
}
public void SetupVideoWindow()
{
int hr = 0;
// Set the video window to be a child of the PictureBox
hr = this.videoWindow.put_Owner(pictureBox1.Handle);
DsError.ThrowExceptionForHR(hr);
hr = this.videoWindow.put_WindowStyle(WindowStyle.Child);
DsError.ThrowExceptionForHR(hr);
// Make the video window visible, now that it is properly positioned
hr = this.videoWindow.put_Visible(OABool.True);
DsError.ThrowExceptionForHR(hr);
// Set the video position
Rectangle rc = pictureBox1.ClientRectangle;
hr = videoWindow.SetWindowPosition(0, 0, _previewWidth, _previewHeight);
DsError.ThrowExceptionForHR(hr);
}
public int SampleCB(double SampleTime, IMediaSample pSample)
{
throw new NotImplementedException();
}
private void SetConfigParams(ICaptureGraphBuilder2 capGraph, IBaseFilter capFilter, int iFrameRate, int iWidth, int iHeight)
{
int hr;
object config;
AMMediaType mediaType;
// Find the stream config interface
hr = capGraph.FindInterface(
PinCategory.Capture, MediaType.Video, capFilter, typeof(IAMStreamConfig).GUID, out config);
IAMStreamConfig videoStreamConfig = config as IAMStreamConfig;
if (videoStreamConfig == null)
{
throw new Exception("Failed to get IAMStreamConfig");
}
// Get the existing format block
hr = videoStreamConfig.GetFormat(out mediaType);
DsError.ThrowExceptionForHR(hr);
// copy out the videoinfoheader
VideoInfoHeader videoInfoHeader = new VideoInfoHeader();
Marshal.PtrToStructure(mediaType.formatPtr, videoInfoHeader);
// if overriding the framerate, set the frame rate
if (iFrameRate > 0)
{
videoInfoHeader.AvgTimePerFrame = 10000000 / iFrameRate;
}
// if overriding the width, set the width
if (iWidth > 0)
{
videoInfoHeader.BmiHeader.Width = iWidth;
}
// if overriding the Height, set the Height
if (iHeight > 0)
{
videoInfoHeader.BmiHeader.Height = iHeight;
}
// Copy the media structure back
Marshal.StructureToPtr(videoInfoHeader, mediaType.formatPtr, false);
// Set the new format
hr = videoStreamConfig.SetFormat(mediaType);
DsError.ThrowExceptionForHR(hr);
DsUtils.FreeAMMediaType(mediaType);
mediaType = null;
}
private void SaveSizeInfo(ISampleGrabber sampleGrabber)
{
int hr;
// Get the media type from the SampleGrabber
AMMediaType media = new AMMediaType();
hr = sampleGrabber.GetConnectedMediaType(media);
DsError.ThrowExceptionForHR(hr);
if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
{
throw new NotSupportedException("Unknown Grabber Media Format");
}
// Grab the size info
VideoInfoHeader videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader));
_previewStride = _previewWidth * (videoInfoHeader.BmiHeader.BitCount / 8);
DsUtils.FreeAMMediaType(media);
media = null;
}
private void ConfigureSampleGrabber(ISampleGrabber sampleGrabber)
{
AMMediaType media;
int hr;
// Set the media type to Video/RBG24
media = new AMMediaType();
media.majorType = MediaType.Video;
media.subType = MediaSubType.RGB24;
media.formatType = FormatType.VideoInfo;
hr = sampleGrabber.SetMediaType(media);
DsError.ThrowExceptionForHR(hr);
DsUtils.FreeAMMediaType(media);
media = null;
hr = sampleGrabber.SetCallback(this, 1);
DsError.ThrowExceptionForHR(hr);
}
public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
Bitmap v = new Bitmap(_previewWidth, _previewHeight, _previewStride,
PixelFormat.Format24bppRgb, pBuffer);
v.RotateFlip(RotateFlipType.Rotate180FlipX);
if (isFinished)
{
this.BeginInvoke((MethodInvoker)delegate
{
isFinished = false;
ReadBarcode(v);
isFinished = true;
});
}
return 0;
}
}
}