oscarchen

oscarchen

  • NA
  • 3
  • 66.6k

ASP.net C# : How to create a QR code scanner with webcam?

Dec 4 2012 2:52 PM
Facing problem in detecting and decoding, current phase just can open the webcam, but can't detect the QR code. Any suggestion?

I was using the zxing for the QR code decode, and Aforge to connected the webcam.

Here the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using com.google.zxing.qrcode;
using com.google.zxing.qrcode.detector;
using COMMON = com.google.zxing.common;
using com.google.zxing;
using AForge.Video;
using AForge.Video.DirectShow;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public QRCodeReader reader = new QRCodeReader();
        public Bitmap current { get; private set; }
        //public event EventHandler ResultFound;
        Bitmap video;
        public bool Run { get; set; }
        public bool newBitmap { get; set; }
       private FilterInfoCollection VideoCaptureDevices;
        private VideoCaptureDevice FinalVideo;
       // com.google.zxing.Reader reader = new com.google.zxing.MultiFormatReader();
       public Form1()
        {
            InitializeComponent();
       }
        private void Form1_Load(object sender, EventArgs e)
        {
            VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices) 
            {
              comboBox1.Items.Add(VideoCaptureDevice.Name);
            }
            comboBox1.SelectedIndex = 0;
            FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
            FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
            FinalVideo.Start();       
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //GetQRValue(video);
            LuminanceSource source = new RGBLuminanceSource(video, video.Width, video.Height);
            com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source));
            Result result;
            try
            {
                result = new MultiFormatReader().decode(bitmap);
                label1.Text = result.Text;
            }
            catch (ReaderException re)
            {
                label1.Text = re.Message;
            }           
        }
        void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            video = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = video;
            if (Detect( video)!="")
            {
                button2.Visible = false;
            }
            if (GetQRValue(video) != "")
            { 
            }            
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (FinalVideo.IsRunning)
            {
                FinalVideo.Stop();
            }
        }
public string Detect(Bitmap bitmap)
        {
            try
            {
               com.google.zxing.LuminanceSource source = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height);
               var binarizer = new HybridBinarizer(source);
               var binBitmap = new BinaryBitmap(binarizer);
             BitMatrix bm = binBitmap.BlackMatrix;
                  Detector detector = new Detector(bm);
                  DetectorResult result = detector.detect();
                 string retStr = "Found at points ";
                  foreach (ResultPoint point in result.Points)
                  {
                      retStr += point.ToString() + ", ";
                  }
                  return retStr;
              }
              catch
              {
                  return "Failed to detect QR code.";
              }
          }
     }
}

Answers (10)