QR code webcam scanner c#

做~自己de王妃 提交于 2019-12-23 06:26:21

问题


I Have tried various QR code libraries and webcam capturing techniques. Capturing photos within a specific time interval and then sending it to the QR code library seemed as a good idea but the success ratio for detecting the QR code is extremely low. Could anyone recommend a better approach for detecting the QR code through webcam? Thanks a lot :)

Code:

void FinalVideo_NewFrame(object sender, 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
        {

        }

}

I also used messaging.toolkit.qrcode.dll. The code follows:

private void mainWinForm_Load(object sender, 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
        }

    }

回答1:


Try using AForge.NET library for capturing the video from your webcam, and then ZXing.Net library for reading the QR codes.

You can follow some Youtube tutorials like these that will show how to get video from the webcam using AForge.Net . https://www.youtube.com/watch?v=osAOpsRYqVs&t=311s

As for the QRs decoding I used the following code which I execute every 1 second:

`

    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();
                }
            }


来源:https://stackoverflow.com/questions/17424360/qr-code-webcam-scanner-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!