QR code generation, QR code generator

Source: Internet
Author: User

QR code generation, QR code generator

Recently, the project encountered a need to generate a QR code to share the system link with other users. Then, they searched for the information and made some Reading Notes:

The main task is to use ThoughtWorks. QRCode. dll. ThoughtWorks. QRCode is very powerful and easy to use,

The following is a detailed description of the principles of the QR code. I have read that there are no algorithms that are hard to understand, but there are too many computations involved. If you are interested, you can study them.

Http://developer.51cto.com/art/201310/414082_all.htm

 

To generate a QR code, use the QRCodeEncoder class. namespace:ThoughtWorks. QRCode. Codec


Main attributes of the class:

Public virtual ColorQRCodeBackgroundColor{Get; set;} // background color of the QR code. The default value is white.

Public virtual ENCODE_MODEQRCodeEncodeMode{Get; set;} // The default value isENCODE_MODE.BYTE

Public virtual ERROR_CORRECTIONQRCodeErrorCorrect{Get; set;} // The default value isERROR_CORRECTION. M

Public virtual ColorQRCodeForegroundColor{Get; set;} // QR code color. The default value is black.

Public virtual intQRCodeScale{Get; set;} // zoom in ratio. In the QR code, the minimum size of the Black Block is 4 by default.

Public virtual intQRCodeVersion{Get; set;} // QR code version. The default value is 7 and the value range is 0 ~ 40 (including 0 and 40) (I think the size of the QR code is more colloquial. The size of the information contained in the QR code is determined by the attribute. if the content is too long, an array out-of-bounds exception will be thrown)

Two enumerations are defined:

public enum ENCODE_MODE // Encoding mode (my own understanding, the following are similar)

         {
             ALPHA_NUMERIC,
             NUMERIC,
             BYTE
         }
 
         public enum ERROR_CORRECTION // (error correction level)
         {
             L,
             M,
             Q,
             H
         }

The two most important methods are as follows:

BiteMap Encode (string content );

BiteMap Encode (string content, Encoding encoding );

The content string and encoding method of the incoming QR code.

By default, the system determines the input content parameter. If the input content is a Unicode character, the system uses Unicode encoding by default. Otherwise, the system uses ASCII encoding.

Chinese is supported on the Internet, but I tested that Chinese is not supported by default. I don't know if my class library is faulty. If you want to support Chinese, please take the initiative to pass the encoding method parameter (utf8 ).

public virtual void setStructureappend(int m, int n, int p)

// 0 <= N <= 16 & 0 <= M <= 16 & 0 <= P <= 255 is useful. What is the purpose of this method, I don't understand either. If you know something, please give me some advice.

 

To make it easier to use, I made a simple encapsulation for this class, mainlyThe default encoding is modified to UTF8, And the QRCodeVersion can be automatically determined based on the number of bytes passed in. After the encoding is modified, the base64 string of the Two-dimensional code image is returned.:

1 public ErWeiMa ()
 2         {
 3 _QRCode = new QRCodeEncoder ();
 4 _QRCode.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
 5 _QRCode.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
 6 _QRCode.QRCodeScale = 4;
 7
 8 encoding = Encoding.UTF8;
 9         }
10
11
12 /// <summary>
13 /// Get the base64 string of the QR code image, which can be directly assigned to the src display of the img object
14 /// The default encoding UTF8, QRCodeEncoder default value is Unicode
15 /// </ summary>
16 /// <param name = "content"> Coded content </ param>
17 /// <returns> two-dimensional code image base64 string </ returns>
18 public string Encode (string content)
19 {
20 this.InitVersion (content);
21 using (var ms = new MemoryStream ())
22 using (Bitmap image = _QRCode.Encode (content, encoding))
twenty three             {
24 image.Save (ms, System.Drawing.Imaging.ImageFormat.Jpeg);
25 return "data: image / jpeg; base64," + Convert.ToBase64String (ms.ToArray ());
26}
27}
28 /// <summary>
29 /// Get the base64 string of the QR code image, which can be directly assigned to the src display of the img object
30 /// </ summary>
31 /// <param name = "content"> Coded content </ param>
32 /// <param name = "encoding"> Encoding type </ param>
33 /// <returns> two-dimensional code image base64 string </ returns>
34 public string Encode (string content, Encoding encoding)
35 {
36 this.encoding = encoding;
37 this.InitVersion (content);
38 using (var ms = new MemoryStream ())
39 using (Bitmap image = _QRCode.Encode (content, encoding))
40 {
41 image.Save (ms, System.Drawing.Imaging.ImageFormat.Jpeg);
42 return "data: image / jpeg; base64," + Convert.ToBase64String (ms.ToArray ());
43}
44}

 

Usage:

1 ErWeiMa.ErWeiMa er = new ErWeiMa.ErWeiMa();
2  this.img1.Src = er.Encode("China");

Code details:

Http://pan.baidu.com/s/1nvCwlfj

ThoughtWorks. QRCode. dll:

Http://pan.baidu.com/s/1c2zNrMS


Related Article

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.