C # how to create and read qrcode-[C # instance]

Source: Internet
Author: User

I am a project engineer at the school's logistics lab and asked me if I knew something about qrcode. I did some small applications in this area before, so I summarized the methods for operating qrcode in C #. First, I want to know what qrcode is. With the concept of IOT, more and more hardware entities and software applications related to qrcode are around us, I wonder if we will change our way of life one day?

 

A QR code is a two-dimensional barcode. a QR code comes from the abbreviation of "Quick Response", meaning a quick response. It is derived from the inventor's hope that the QR code can be decoded quickly. QR codes can store more information than normal codes, and do not need to be directly aligned with the scanner when scanning like normal codes. The QR code is square and only black and white are available. Three of the four corners are printed with a small square pattern like "back. The three are patterns that help decode software positioning. users do not need to aim at them, and the data can still be correctly read no matter what angle they scan. More details.

Qrcode has many applications in daily life. For example, qrcode on train tickets can be used to store information. Currently, many mobile phone software provides qrcode scanning for downloading, the No. 1 online store opened a "wall sticker Supermarket" on the Shanghai Metro. Passers-by only need to use their mobile phones to scan the qrcode of a product and then place an order to purchase it. From the developer's point of view, there are two main requirements: Creating qrcode and reading qrcode information. Here, we mainly consider using C # To create and read QR code.

 

I. method 1 of QR code creation

There are many ways to create a QR code. It is easier to directly reference the compiled DLL to implement the function. Download the DLL and add the reference before using it.

KeyCodeAs follows:

Private void qrcode_click (Object sender, routedeventargs e) {qrcodewriter qrwrite = new qrcodewriter (); bytematrix Bm = qrwrite. encode (txtsource. text, barcodeformat. qr_code 150,150); this. qrimg. source = convertbytemartixtowriteablebitmap (BM);} public writeablebitmap convertbytemartixtowriteablebitmap (bytematrix BM) {writeablebitmap WB = new writeablebitmap (BM. width, BM. height); For (INT x = 0; x <= WB. pixelwidth-1; X ++) {for (INT y = 0; y <= WB. pixelheight-1; y ++) {If (BM. array [y] [x] =-1) {// white WB. pixels [WB. pixelwidth * Y + x] = bitconverter. toint32 (bitconverter. getbytes (0 xffffffff), 0);} else {// black WB. pixels [WB. pixelwidth * Y + x] = bitconverter. toint32 (bitconverter. getbytes (0xff000000), 0) ;}} return WB ;}

 

Ii. method 2 for creating QR code

Another simpler method is to generate a QR code through the Google chart API. The call code is as follows:

 
Private void btncreate_click (Object sender, routedeventargs e) {var imgsrc = "http://chart.apis.google.com/chart? CHS = "+ this.txt size. text + "& chlorophyll =" + this.txt content. text + & Choe = UTF-8 & CHT = QR "; imgsource. source = (New bitmapimage (New uri (imgsrc )));}

That is to say, You need to provide two parameters, one for the size of the generated QR code image and the content contained in the generated QR code.

 

3. QR code reading

After the QR code is created, it is necessary to read the information in it. Similar to method 1 for creating the QR code, we need to use the function provided by the DLL to identify the content in the QR code, the Code is as follows:

Private void upload (Object sender, routedeventargs e) {writeablebitmap WB = new writeablebitmap (imagesource, null); required qrread = new qrcodereader (); required luminiance = new upload (WB, WB. pixelwidth, WB. pixelheight); hybridbinarizer binarizer = new hybridbinarizer (luminiance); binarybitmap binbitmap = new binarybitmap (binarizer); Result results; try {Results = qrread. decode (binbitmap); txtshow. TEXT = results. text;} catch (exception ex) {txtshow. TEXT = "error:" + ex. getType () + ":" + ex. message ;}}

 

Add and load the QR code image to be queried, and then you can obtain the information in the QR code in this way.

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.