A simple QR Decoding software is going to be implemented and explained in this article.
Program Overview:
The notion behind developing a sample C# code is to illustrate the basic steps need to be taken for .Net developers to build a simple QR Decoder, and show the developers how to use the famous zxing library to decode 2D barcode library in realtime, either by capturing pictures from a webcam or by uploading a static 2D barcode picture.
This program should use the first camera found in your Camera’s profile, and start streaming video and taking snapshots every second, and process the collected image looking for a QR Code within your camera’s scope, and once a QR Code is being detected the program will decode this QR and display the result and the speed of decoding on the result text box shown below at the GUI.
Program initialization:
// General Initialization // This is an initialization for synchronous textBox update delegate void SetTextCallback(string text); string filePath; // A stopWatch to test the processing speed. Stopwatch stopwatch = new Stopwatch(); // Webcam variables to handle the image captured by the webcam. FilterInfoCollection videoSources; VideoCaptureDevice videoStream; // Bitmap buffers Bitmap streamBitmap; Bitmap snapShotBitmap; Bitmap safeTempstreamBitmap; // Sound to be played when successful detection take a place. SoundPlayer player = new SoundPlayer("Resources/connect.wav"); // Thread for decoding in parallel with the webacm video streaming. Thread decodingThread; // The QR Decoder variable from ZXing MDecoder decoder;
Main Function:
// Initialize sound variable player.Stream = Properties.Resources.connect; decoder = new MDecoder(); // Start a decoding process decodingThread = new Thread(new ThreadStart(decodeLoop)); decodingThread.Start(); try { // enumerate video devices videoSources = new FilterInfoCollection(FilterCategory.VideoInputDevice); // create video source // Choose only the first attached camera from the profile list videoStream = new VideoCaptureDevice(videoSources[0].MonikerString); // Set NewFrame event handler // Whenever the program catches a frame this event will be triggered videoStream.NewFrame += new NewFrameEventHandler(videoSource_NewFrame); // Start the video streaming from the source videoStream.Start(); }catch(VideoException exp) { Console.Write(exp.Message); }
Last but the not least the Decoding Loop (Thread)
// Decoding endless thread process public void decodeLoop() { while (true) { // 1 second pause for the thread. This could be changed manually to a prefereable decoding interval. Thread.Sleep(1000); if (streamBitmap != null) snapShotBitmap = (Bitmap)safeTempstreamBitmap.Clone(); else return; // Reset watch before decoding the streamed image. stopwatch.Reset(); stopwatch.Start(); // Decode the snapshot. string decodeStr = decoder.MDecode(snapShotBitmap); stopwatch.Stop(); //string decode = Detect(b); // If decodeStr is null then there was no QR detected, otherwise show the result of detection and play the sound. if (decodeStr == null) { this.SetResultText("There is no QR Code!"); } else { this.SetResultText(decodeStr); this.SetSpeedText( stopwatch.Elapsed.TotalMilliseconds.ToString()); player.Play(); } } }
To download the c# (Visual Studio) source code:
Download from Github
I am interested in trying this out, but i am running into an issue on line 132 of the Form1.cs:
Cross-thread operation not valid: Control ‘pictureBox1’ accessed from a thread other than the thread it was created on.
I can’t seem to get it to read a QRCode from my webcam.