如何在 C# 中使用 Aforge.NET 和 ZXing.NET 从 WebCamera 解码 QRCode
How to Decode QRCode from WebCamera using Aforge.NET and ZXing.NET in C#
我正在尝试开发一个 WindowsForm 应用程序,它将使用网络摄像头检测 QRCode 并对消息进行解码。为此,我使用 AForge.NET 和 ZXing.NET.
到目前为止,我已经能够弄清楚如何从 URI 中解码 QRCode,但我想检测来自网络摄像头的 QRCode,然后对其进行解码。
下面是我的代码示例。
public String Decode(Uri uri)
{
Bitmap image;
try
{
image = (Bitmap)Bitmap.FromFile(uri.LocalPath);
}
catch (Exception ex)
{
throw new FileNotFoundException("Resource not found: " + uri);
}
using (image)
{
String text = "";
LuminanceSource source = new BitmapLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
if (result != null)
{
text = result.Text;
return text;
}
text = "Provided QR couldn't be read.";
return text;
}
}
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;
private Bitmap capturedImage;
private String message = "";
public FormQRCodeScanner()
{
InitializeComponent();
}
private void FormQRCodeScanner_Load(object sender, EventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo device in videoDevices)
{
comboBoxCameraSource.Items.Add(device.Name);
}
comboBoxCameraSource.SelectedIndex = 0;
videoSource = new VideoCaptureDevice();
buttonStartStop.Text = "Start";
buttonCapture.Enabled = false;
buttonDecode.Enabled = false;
}
private void FormQRCodeScanner_FormClosing(object sender, FormClosingEventArgs e)
{
if (videoSource.IsRunning)
{
videoSource.Stop();
}
}
void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap image = (Bitmap) eventArgs.Frame.Clone();
pictureBoxSource.Image = image;
}
private void buttonStartStop_Click(object sender, EventArgs e)
{
if (videoSource.IsRunning)
{
videoSource.Stop();
pictureBoxSource.Image = null;
pictureBoxCaptured.Image = null;
pictureBoxSource.Invalidate();
pictureBoxCaptured.Invalidate();
buttonStartStop.Text = "Start";
buttonCapture.Enabled = false;
buttonDecode.Enabled = false;
}
else
{
videoSource = new VideoCaptureDevice(videoDevices[comboBoxCameraSource.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
videoSource.Start();
buttonStartStop.Text = "Stop";
buttonCapture.Enabled = true;
buttonDecode.Enabled = true;
}
}
private void buttonCapture_Click(object sender, EventArgs e)
{
if (videoSource.IsRunning)
{
pictureBoxCaptured.Image = (Bitmap)pictureBoxSource.Image.Clone();
capturedImage = (Bitmap)pictureBoxCaptured.Image;
}
}
private void buttonDecode_Click(object sender, EventArgs e)
{
if (capturedImage != null)
{
ExtractQRCodeMessageFromImage(capturedImage);
richTextBoxMessage.Text = message;
richTextBoxMessage.ReadOnly = true;
}
}
private string ExtractQRCodeMessageFromImage(Bitmap bitmap)
{
try
{
BarcodeReader reader = new BarcodeReader
(null, newbitmap => new BitmapLuminanceSource(bitmap), luminance => new GlobalHistogramBinarizer(luminance));
reader.AutoRotate = true;
reader.TryInverted = true;
reader.Options = new DecodingOptions { TryHarder = true };
var result = reader.Decode(bitmap);
if (result != null)
{
message = result.Text;
return message;
}
else
{
message = "QRCode couldn't be decoded.";
return message;
}
}
catch (Exception ex)
{
message = "QRCode couldn't be detected.";
return message;
}
}
**For this you should install these packages
Install-Package AForge
Install-Package AForge.Video
Install-Package AForge.Video.DirectShow
Install-Package ZXing.Net
you can watch this video for more help
https://www.youtube.com/watch?v=wcoy0Gwxr50**
using System.IO;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using ZXing;
using ZXing.Aztec;
private void Form1_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
}
private void button1_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
}
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void timer1_Tick(object sender, EventArgs e)
{
BarcodeReader Reader = new BarcodeReader();
Result result = Reader.Decode((Bitmap)pictureBox1.Image);
try
{
string decoded = result.ToString().Trim();
if (decoded != "")
{
timer1.Stop();
MessageBox.Show(decoded);
Form2 form = new Form2();
form.Show();
this.Hide();
}
}
catch(Exception ex){
}
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
}
我正在尝试开发一个 WindowsForm 应用程序,它将使用网络摄像头检测 QRCode 并对消息进行解码。为此,我使用 AForge.NET 和 ZXing.NET.
到目前为止,我已经能够弄清楚如何从 URI 中解码 QRCode,但我想检测来自网络摄像头的 QRCode,然后对其进行解码。
下面是我的代码示例。
public String Decode(Uri uri)
{
Bitmap image;
try
{
image = (Bitmap)Bitmap.FromFile(uri.LocalPath);
}
catch (Exception ex)
{
throw new FileNotFoundException("Resource not found: " + uri);
}
using (image)
{
String text = "";
LuminanceSource source = new BitmapLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result = new MultiFormatReader().decode(bitmap);
if (result != null)
{
text = result.Text;
return text;
}
text = "Provided QR couldn't be read.";
return text;
}
}
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;
private Bitmap capturedImage;
private String message = "";
public FormQRCodeScanner()
{
InitializeComponent();
}
private void FormQRCodeScanner_Load(object sender, EventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo device in videoDevices)
{
comboBoxCameraSource.Items.Add(device.Name);
}
comboBoxCameraSource.SelectedIndex = 0;
videoSource = new VideoCaptureDevice();
buttonStartStop.Text = "Start";
buttonCapture.Enabled = false;
buttonDecode.Enabled = false;
}
private void FormQRCodeScanner_FormClosing(object sender, FormClosingEventArgs e)
{
if (videoSource.IsRunning)
{
videoSource.Stop();
}
}
void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap image = (Bitmap) eventArgs.Frame.Clone();
pictureBoxSource.Image = image;
}
private void buttonStartStop_Click(object sender, EventArgs e)
{
if (videoSource.IsRunning)
{
videoSource.Stop();
pictureBoxSource.Image = null;
pictureBoxCaptured.Image = null;
pictureBoxSource.Invalidate();
pictureBoxCaptured.Invalidate();
buttonStartStop.Text = "Start";
buttonCapture.Enabled = false;
buttonDecode.Enabled = false;
}
else
{
videoSource = new VideoCaptureDevice(videoDevices[comboBoxCameraSource.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
videoSource.Start();
buttonStartStop.Text = "Stop";
buttonCapture.Enabled = true;
buttonDecode.Enabled = true;
}
}
private void buttonCapture_Click(object sender, EventArgs e)
{
if (videoSource.IsRunning)
{
pictureBoxCaptured.Image = (Bitmap)pictureBoxSource.Image.Clone();
capturedImage = (Bitmap)pictureBoxCaptured.Image;
}
}
private void buttonDecode_Click(object sender, EventArgs e)
{
if (capturedImage != null)
{
ExtractQRCodeMessageFromImage(capturedImage);
richTextBoxMessage.Text = message;
richTextBoxMessage.ReadOnly = true;
}
}
private string ExtractQRCodeMessageFromImage(Bitmap bitmap)
{
try
{
BarcodeReader reader = new BarcodeReader
(null, newbitmap => new BitmapLuminanceSource(bitmap), luminance => new GlobalHistogramBinarizer(luminance));
reader.AutoRotate = true;
reader.TryInverted = true;
reader.Options = new DecodingOptions { TryHarder = true };
var result = reader.Decode(bitmap);
if (result != null)
{
message = result.Text;
return message;
}
else
{
message = "QRCode couldn't be decoded.";
return message;
}
}
catch (Exception ex)
{
message = "QRCode couldn't be detected.";
return message;
}
}
**For this you should install these packages
Install-Package AForge
Install-Package AForge.Video
Install-Package AForge.Video.DirectShow
Install-Package ZXing.Net
you can watch this video for more help
https://www.youtube.com/watch?v=wcoy0Gwxr50**
using System.IO;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using ZXing;
using ZXing.Aztec;
private void Form1_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
}
private void button1_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
}
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void timer1_Tick(object sender, EventArgs e)
{
BarcodeReader Reader = new BarcodeReader();
Result result = Reader.Decode((Bitmap)pictureBox1.Image);
try
{
string decoded = result.ToString().Trim();
if (decoded != "")
{
timer1.Stop();
MessageBox.Show(decoded);
Form2 form = new Form2();
form.Show();
this.Hide();
}
}
catch(Exception ex){
}
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
}