二维码网络摄像头扫描仪c#

问题描述 投票:0回答:4

我尝试了各种QR码库和网络摄像头捕获技术。在特定时间间隔内拍摄照片,然后将其发送到QR码库似乎是个好主意,但检测到QR码的成功率极低。谁能推荐一种通过网络摄像头检测QR码的更好方法?非常感谢:)

代码:

void FinalVideo_NewFrame(对象发送者,NewFrameEventArgs eventArgs) {

        Bitmap video = (Bitmap)eventArgs.Frame.Clone();

        pictureBox1.Image = video;
        try
        {
            com.google.zxing.qrcode.decoder.Decoder objDecoder = new com.google.zxing.qrcode.decoder.Decoder();
            Bitmap bitmap = new Bitmap(pictureBox1.Image);
            com.google.zxing.LuminanceSource source = new RGBLuminanceSource(bitmap, bitmap.Width,bitmap.Height); 
            var binarizer = new HybridBinarizer(source);
            var binBitmap = new BinaryBitmap(binarizer);
            QRCodeReader qrCodeReader = new QRCodeReader();
            string str = new MultiFormatReader().decode(binBitmap).Text;
            MessageBox.Show(str);

        }
        catch
        {

        }

}

我还使用了messaging.toolkit.qrcode.dll。 代码如下:

private void mainWinForm_Load(对象发送者,EventArgs e)

    {

        webcam = new WebCam();
        webcam.InitializeWebCam(ref imgVideo);
        QRCodeDecoder decoder = new QRCodeDecoder();
        try
        {

            MessageBox.Show(decoder.decode(new QRCodeBitmapImage(imgCapture.Image as Bitmap)));
        }

        catch
        {
            //Do nothing
        }

    }
c# webcam qr-code barcode-scanner
4个回答
5
投票

尝试使用 AForge.NET 库从网络摄像头捕获视频,然后使用 ZXing.Net 库读取 QR 码。

您可以按照类似的一些 Youtube 教程进行操作,这些教程将展示如何使用 AForge.Net 从网络摄像头获取视频。 https://www.youtube.com/watch?v=osAOpsRYqVs&t=311s

对于 QR 解码,我使用了以下代码,每 1 秒执行一次:

`

    private void decode_QRtag()
    {
        try
        {
            //pictureBox1 shows the web cam video
            Bitmap bitmap = new Bitmap(pictureBox1.Image);

            BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryHarder = true };
            Result result = reader.Decode(bitmap);
            string decoded = result.ToString().Trim();        
            //capture a snapshot if there is a match
            PictureBox2.Image = bitmap;
            textBox1.Text = decoded;
        }
        catch 
        {
        }
    }`

2
投票
**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();
                }
            }

1
投票

如果有人想要在此处复制和粘贴代码,只需确保您制作了表格并安装了此处列出的软件包

Install-Package AForge
Install-Package AForge.Video
Install-Package AForge.Video.DirectShow
Install-Package ZXing.Net

要复制和粘贴的代码

using System.IO;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using ZXing;
using ZXing.Aztec;
using System.Windows.Forms;
using System.Drawing;

namespace QR_Reader
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection CaptureDevice;
        private VideoCaptureDevice FinalFrame;
        public Form1()
        {
            InitializeComponent();

        }

        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 button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();
            Console.WriteLine("Scanner Strated");
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FinalFrame.IsRunning == true)
            {
                FinalFrame.Stop();
            }
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            BarcodeReader Reader = new BarcodeReader();
            Result result = Reader.Decode((Bitmap)pictureBox1.Image);
            try
            {
                if(result == null) { return; }
                string decoded = result.ToString().Trim();
                Console.WriteLine(decoded);
                if (decoded != "")
                {
                    timer1.Stop();
                    MessageBox.Show(decoded);
                    //Form2 form = new Form2();
                    //form.Show();
                    //this.Hide();

                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}

将下面列出的这些组件添加到表单中

Timer
CompoundBox
PictureBox
Button1
Button2

按钮用于显示摄像头 按钮2是开始扫描


0
投票

谢谢,你的代码让我的生活变得轻松。

效果很好,我添加了第三个按钮,用于每 2 秒自动扫描一次。

 private void BtnEscaneoAuto_Click(object sender, EventArgs e)
 {
     timer1.Stop();
     timer1.Interval = 2000;
     timer1.Enabled = true;
     timer1.Start();
     EscaneoAuto = true;
     textBox1.Text = "Escaneo Automático Iniciado..." + Environment.NewLine;
 }

我修改了一个tick事件

 private void timer1_Tick(object sender, System.EventArgs e)
 {
     BarcodeReader Reader = new BarcodeReader();
     Result result = Reader.Decode((Bitmap)pictureBox1.Image);
     try
     {
         if (result == null) { return; }
         string decoded = result.ToString().Trim();
         Console.WriteLine(decoded);
         if (decoded != "")
         {
             if (EscaneoAuto)
             {
                 textBox1.Text = textBox1.Text + decoded + Environment.NewLine;
             }
             else
             {
                 timer1.Stop();
                 MessageBox.Show(decoded);
             }                    
             //Form2 form = new Form2();
             //form.Show();
             //this.Hide();

         }
     }
     catch (Exception ex)
     {

     }
 }

问候。

© www.soinside.com 2019 - 2024. All rights reserved.