C # WinForm calls the camera to recognize the QR code

Source: Internet
Author: User

First we need to refer to two third-party components: Aforge and zxing.

The Aforge is a camera operation component, and the zxing is a QR Code identification component. are open source projects. Avoid making wheels repeatedly.

In fact, some operating code I also refer to others, if the infringement of your copyright, please contact me.

This blog is for technical exchange only.

Download and usage you can search by yourself.

First, get all the available camera devices and add them to the ComboBox1

1         Private voidgetcamlist ()2         {3             Try4             {5                 //AForge.Video.DirectShow.FilterInfoCollection Device Enumeration class6Videodevices =Newfilterinfocollection (filtercategory.videoinputdevice);7                 //Empty list box8 comboBox1.Items.Clear ();9                 if(Videodevices.count = =0)Ten                     Throw Newapplicationexception (); One                 //global variable indicating whether the device camera device exists ADeviceexist =true; -                 //Join the device -                 foreach(FilterInfo deviceinchvideodevices) the                 { - COMBOBOX1.ITEMS.ADD (device. Name); -                 } -                 //The first item is selected by default +Combobox1.selectedindex =0; -             } +             Catch(ApplicationException) A             { atDeviceexist =false; -COMBOBOX1.ITEMS.ADD ("no available devices found"); -             } -}

The following is the Startup button event code and some other code.

1         Private voidStart_click (Objectsender, EventArgs e)2         {3             if(Start. Text = ="Start")4             {5                 if(deviceexist)6                 {7                     //Video Capture Devices8VideoSource =NewVideocapturedevice (Videodevices[combobox1.selectedindex]. monikerstring);9                     //triggered when a new picture is capturedTenVideosource.newframe + =NewNewframeeventhandler (video_newframe); One                     //close it first and open it below. Avoid repeated open errors A Closevideosource (); -                     //Set Screen Size -Videosource.desiredframesize =NewSize ( the, -); the                     //start the video component - Videosource.start (); -Start. Text ="Stop"; -                     //Start timing parsing QR code +Timer1. Enabled =true; -                     //start a scan line in a drawing video +Timer2. Enabled =true; A                 } at             } -             Else -             { -                 if(videosource.isrunning) -                 { -Timer2. Enabled =false; inTimer1. Enabled =false; - Closevideosource (); toStart. Text ="Start"; +                 } -             } the}
        /// <summary>        ///Global variables that record the distance from the top of the scan line/// </summary>        inttop =0; /// <summary>        ///global variables, saving each captured image/// </summary>Bitmap img =NULL; Private voidVideo_newframe (Objectsender, Newframeeventargs EventArgs) {img=(Bitmap) EventArgs.Frame.Clone (); }        //close the device safely        Private voidClosevideosource () {if(! (VideoSource = =NULL))                if(videosource.isrunning) {videosource.signaltostop (); VideoSource=NULL; }        }

The following code draws a scan line in the screen.

1         Private voidTimer2_tick (Objectsender, EventArgs e)2         {3             if(img = =NULL)4             {5                 return;6             }7Bitmap Img2 =(Bitmap) img. Clone ();8Pen p =NewPen (color.red);9Graphics g =graphics.fromimage (IMG2);TenPoint P1 =NewPoint (0, top); OnePoint P2 =NewPoint (Picturebox1.width, top); A G.drawline (P, p1, p2); - g.dispose (); -Top + =2; the  -top = top%Picturebox1.height; -pictureBox1.Image =Img2; -  +}

Here is the decoding QR code:

1         Private voidTimer1_Tick (Objectsender, EventArgs e)2         {3             if(img = =NULL)4             {5                 return;6             }7             #regionConvert a picture into a byte array8MemoryStream ms =NewMemoryStream ();9 img. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP);Ten             byte[] bt =Ms. GetBuffer (); One Ms. Close ();  A             #endregion -Luminancesource Source =Newrgbluminancesource (BT, IMG. Width, IMG. Height); -Binarybitmap bitmap =NewBinarybitmap (NewZXing.Common.HybridBinarizer (source)); the result result; -             Try -             { -                 //Start decoding +result =NewMultiformatreader (). Decode (bitmap); -             } +             Catch(Readerexception re) A             { at                 return; -             } -             if(Result! =NULL) -             { -TextBox1.Text =result. Text; -  in             } -}

Using third-party components, the development difficulty is really straight down. The internal specific how to decode, really is a little do not know. Also hope that the master of experience is not hesitate to enlighten.

C # WinForm calls the camera to recognize the QR code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.