. NET uses zxing.net to generate a two-dimensional code with pictures in the middle __.net

Source: Internet
Author: User

turn from: http://www.mamicode.com/info-detail-350727.html . NET uses zxing.net to generate middle-band picture two-dimension time: 2014-12-04 00:59:31 reading: 101 Comments: 0 Favorites: 0 [Point my collection +]

A long time ago wrote such a code, but has not been recorded, accidentally wanted to write a blog.

Encapsulate the previous code into functions to facilitate understanding and invocation.

Based on the open source Zxing.net component, the code is as follows:

Add a reference to the Zxing.net first, and then add the appropriate namespaces:

Using System;
Using System.Collections.Generic;
Using System.Drawing;
Using Zxing;
Using Zxing.common;
Using Zxing.qrcode;
Using ZXing.QrCode.Internal;

Method one is to generate a two-dimensional code picture:

///<summary>///generate two-dimensional code picture///</summary>///<param name= "Contents" > to generate the information that the two-dimensional code contains </param>// /<param name= "width" > generated two-dimensional code width (default 300 pixels) </param>///<param name= "height" > generated two-dimensional code height (default 300 pixels) </ param>///<returns> Two-dimensional code picture </returns> public static Bitmap Generatorqrimage (string contents, int width = 300 , int height = +) {if (string).
    IsNullOrEmpty (contents)) {return null;
    //This article address: http://www.cnblogs.com/Interkey/p/qrcode.html encodingoptions options = null;
    Barcodewriter writer = null; Options = new Qrcodeencodingoptions {Disableeci = true, CharacterSet = "UTF-8", Width = width
    , height = height, errorcorrection = ErrorCorrectionLevel.H,};
    writer = new Barcodewriter (); Writer.
    Format = Barcodeformat.qr_code; Writer.

    options = options; Bitmap Bitmap = writer.
    Write (contents);
return bitmap; }

Method Two is to generate a two-dimensional code picture in the middle with pictures:

<summary>///generation of two-dimensional code picture with picture///</summary>///<param name= "Contents" > to generate the information that the two-dimensional code contains </param> <param name= "Middleimg" > to be generated into two-dimensional code in the middle of the picture </param>///<param name= "width" > generated two-dimensional code width (default 300 pixels) </ param>///<param name= "height" > generated two-dimensional code height (default 300 pixels) </param>///<returns> Two-dimensional code with pictures in the middle </returns > public static Bitmap generatorqrimage (string contents, Image middleimg, int width = +, int height = $) {if ( String.
    IsNullOrEmpty (contents)) {return null;
        } if (middleimg = = null) {//return null;
    Return Generatorqrimage (contents); ////This article address: http://www.cnblogs.com/Interkey/p/qrcode.html//construction of two-dimensional code coder Multiformatwriter Mutiwriter = new Multifo
    Rmatwriter ();
    Dictionary<encodehinttype, object> hint = new Dictionary<encodehinttype, object> (); Hint.
    ADD (Encodehinttype.character_set, "UTF-8"); Hint.

   ADD (Encodehinttype.error_correction, ErrorCorrectionLevel.H); Generate two-dimensional code Bitmatrix BM = Mutiwriter.encode (contents, Barcodeformat.qr_code, width, height, hint);
    Barcodewriter barcodewriter = new Barcodewriter ();
    Bitmap Bitmap = barcodewriter.write (BM);

    Gets the actual size of the two-dimensional code (removes the actual size of the two-dimensional code from both sides) int[] Rectangle = bm.getenclosingrectangle ();
    Calculates the size and position of the inserted picture int MIDDLEIMGW = math.min ((int) (RECTANGLE[2)/3.5), middleimg.width);
    int middleimgh = math.min ((int) (RECTANGLE[3]/3.5), middleimg.height); int MIDDLEIMGL = (bitmap.
    WIDTH-MIDDLEIMGW)/2; int middleimgt = (bitmap.

    HEIGHT-MIDDLEIMGH)/2; The IMG is converted to BMP format, otherwise the Graphics object Bitmap bmpimg = new Bitmap (BITMAP) cannot be created later. Width, Bitmap.
    Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = graphics.fromimage (bmpimg)) {G.interpolationmode = System.Drawing.Drawing2D.Interpolation
        G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; G.compositingquality = System.drawing.Drawing2D.CompositingQuality.HighQuality;
    G.drawimage (bitmap, 0, 0);
    ///Insert Picture in two-dimensional code Graphics mygraphic = Graphics.fromimage (bmpimg);
    White Bottom Mygraphic.fillrectangle (brushes.white, MIDDLEIMGL, Middleimgt, MIDDLEIMGW, MIDDLEIMGH);
    Mygraphic.drawimage (middleimg, MIDDLEIMGL, Middleimgt, MIDDLEIMGW, MIDDLEIMGH);
return bmpimg; }

The effect is as follows:

There is a picture and no picture of the generation of the same, the reason is not clear, initially thought that the middle of the picture is because of the original fault-tolerant rate.

The corresponding parsing code is also simple:

Create a barcode reader instance
ibarcodereader reader = new Barcodereader ();
Load a bitmap
bitmap barcodebitmap = (bitmap) image.fromfile ("C:\\sample-barcode-image.png");
Detect and decode the barcode inside the bitmap result
= reader. Decode (BARCODEBITMAP);
Do something with the
"result if" (Result!= null)
    This.txtDecoderType.Text = result. Barcodeformat.tostring ();
    This.txtDecoderContent.Text = result. Text;

The above is the generation and parsing of two-dimensional code, as the generation of one-dimensional code and corresponding parsing is also very simple.

Other commonly used to generate two-dimensional code components are: qrcode.net (open source), zxing (open source), Thoughtworks.qrcode and so on.

After understanding the data storage of two-dimensional code, we can generate creative two-dimensional code based on these technologies.

Reference: QR two-dimensional code generator source code (middle can insert small picture) barcode and two-dimensional code codec implemented in C # How to asp.net and WPF display two-dimensional code C # generating two-dimensional code (QR code) two-dimensional code with qrcode.net details and principles to take you to explore the mysteries of the barcode world Android two-dimensional code recognition Open source project Zxing compilation

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.