QRCode, qrcodeapi documentation

Source: Internet
Author: User

QRCode, qrcodeapi documentation

This week, the leaders asked me to summarize some of the technologies used in the project and then deliver the documents.

QR code, I believe that many projects will require this, and then the mobile terminal will scan, or store some details, or store a link for quick access.

I. Example

Public ActionResult QrCode () {var s = CreateQr ("CE", "123456789", "male", DateTime. now. addYears (-20), "15112341234"); return File (s, "image/jpeg, image/png ");} /// <summary> /// generate a QR code /// </summary> /// <returns> relative path of the QR code </returns> public string CreateQr (string name, string code, string sex, DateTime birthday, string phone) {var codeParams = CodeDescriptor. init (HttpContext. request); var fileName = code + ". pn G "; var path = Server. MapPath ("~ /QRCode/"); var fullFileName = path + fileName; string content = string. format ("Name: {0} \ n no.: {1} \ n Gender: {2} \ n age: {3} \ n contact number: {4} \ n ", name, code, sex, (birthday. year> 1000? (DateTime. now. year-birthday. year ). toString (): ""), phone); codeParams. content = content; // Encode the content codeParams. tryEncode (); using (var MS = new MemoryStream () {codeParams. render (MS); # region Save Image var img = Image. fromStream (MS); if (! Directory. Exists (path) {Directory. CreateDirectory (path);} img. Save (fullFileName); # endregion} return fullFileName ;}

Scan the QR code to view the relevant information.

 

Ii. Parameter Parsing

/// <summary>/// Parse QueryString that define the QR code properties/// </summary>/// <param name="request">HttpRequest containing HTTP GET data</param>/// <returns>A QR code descriptor object</returns>public static CodeDescriptor Init(HttpRequestBase request){    var cp = new CodeDescriptor();    // Error correction level    if (!Enum.TryParse(request.QueryString["e"], out cp.Ecl))        cp.Ecl = ErrorCorrectionLevel.L;    // Code content to encode    cp.Content = request.QueryString["t"];    // Size of the quiet zone    if (!Enum.TryParse(request.QueryString["q"], out cp.QuietZones))        cp.QuietZones = QuietZoneModules.Two;    // Module size    if (!int.TryParse(request.QueryString["s"], out cp.ModuleSize))        cp.ModuleSize = 6;    return cp;}

1. renewal rate

There are four levels of the QR code's attention rate, but I have to first introduce what is the two-dimensional code's attention rate.

The QR code encoding rate is used to perform redundant operations when the QR code is encoded. The purpose of this operation is to scan the QR code for correct results if it is partially blocked. it is like abc encoding into abcabc.

Public enum ErrorCorrectionLevel {L = 0, // The character code of low 7% can be corrected M = 1, // medium 15% Q = 2, // quartile 25% H = 3, // high 30%}

The test method is to scan the QR code and scan the QR code. When scanning the QR code, you can slowly scan the QR code and find that you do not need to scan the QR code completely, the result is displayed.

The higher the scanning rate, the easier it is to scan quickly. The lower the cost is that the content of the QR code is increased, increasing the complexity of the QR code.

By default, L is selected.

 

2. Blank

public enum QuietZoneModules{    Zero = 0,    Two = 2,    Four = 4,}

This attribute indicates the thickness of the blank area on the side of the QR code, Zero indicates that there is no blank border, and the last Border thickness is obtained by Two * 2.

 

3. Dimensions

The ModuleSize here is the size of the QR code image. The larger the size, the more information it can hold.

 

4. Content Encoding

The default encoding of the QR code is UTF-8,

There are some other attributes, such as the background color and the color of the image.

 

The content length limit of the QR code is not found in the document. In the Api documentation, the QR code can be displayed normally if it contains less than 1 character and more than 900 characters. of course, it is not easy to store too many QR codes. the shorter the better.

If there is too much content, you can use the QR code to provide links to allow users to request the interface, instead of scanning the QR code to directly obtain the content.

The specific method is

 codeParams.Content = "http://www.baidu.com";

Http: // is required. Otherwise, the content will be parsed as a common character.

Finally, paste the complete encapsulation:

/// <summary>/// Class containing the description of the QR code and wrapping encoding and rendering./// </summary>public class CodeDescriptor{    public ErrorCorrectionLevel Ecl;    public string Content;    public QuietZoneModules QuietZones;    public int ModuleSize;    public BitMatrix Matrix;    public string ContentType;    /// <summary>    /// Parse QueryString that define the QR code properties    /// </summary>    /// <param name="request">HttpRequest containing HTTP GET data</param>    /// <returns>A QR code descriptor object</returns>    public static CodeDescriptor Init(HttpRequestBase request)    {        var cp = new CodeDescriptor();        // Error correction level        if (!Enum.TryParse(request.QueryString["e"], out cp.Ecl))            cp.Ecl = ErrorCorrectionLevel.L;        // Code content to encode        cp.Content = request.QueryString["t"];        // Size of the quiet zone        if (!Enum.TryParse(request.QueryString["q"], out cp.QuietZones))            cp.QuietZones = QuietZoneModules.Two;        // Module size        if (!int.TryParse(request.QueryString["s"], out cp.ModuleSize))            cp.ModuleSize = 6;        return cp;    }    /// <summary>    /// Parse QueryString that define the QR code properties    /// </summary>    /// <param name="request">HttpRequest containing HTTP GET data</param>    /// <returns>A QR code descriptor object</returns>    public static CodeDescriptor Init(HttpRequest request)    {        var cp = new CodeDescriptor();        // Error correction level        if (!Enum.TryParse(request.QueryString["e"], out cp.Ecl))            cp.Ecl = ErrorCorrectionLevel.L;        // Code content to encode        cp.Content = request.QueryString["t"];        // Size of the quiet zone        if (!Enum.TryParse(request.QueryString["q"], out cp.QuietZones))            cp.QuietZones = QuietZoneModules.Two;        // Module size        if (!int.TryParse(request.QueryString["s"], out cp.ModuleSize))            cp.ModuleSize = 6;        return cp;    }    /// <summary>    /// Encode the content with desired parameters and save the generated Matrix    /// </summary>    /// <returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>    public bool TryEncode()    {        var encoder = new QrEncoder(Ecl);        QrCode qr;        if (!encoder.TryEncode(Content, out qr))            return false;        Matrix = qr.Matrix;        return true;    }    /// <summary>    /// Render the Matrix as a PNG image    /// </summary>    /// <param name="ms">MemoryStream to store the image bytes into</param>    public void Render(MemoryStream ms)    {        var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));        render.WriteToStream(Matrix, ImageFormat.Png, ms);        ContentType = "image/png";    }}
QR code

 

Refer:

QR code details

Some basic knowledge and help in qrcodenet code

Api

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.