1

我尝试了各种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
        }

    }
4

3 回答 3

2

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

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

至于 QRs 解码,我使用了以下每 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 
        {
        }
    }`
于 2016-11-08T08:38:14.510 回答
1
**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();
                }
            }
于 2017-12-11T12:31:18.390 回答
0

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

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 是开始扫描

于 2021-12-13T18:22:09.270 回答