Before in the project used zxing generated two-dimensional code, recently another project also need to use two-dimensional code, so re-learning to use zxing to generate two-dimensional code
The next step is to prepare the work, because I was developed with vs2013, so I chose the. net4.5 version of the Zxing.dll. In addition, a ready-to-embed image is shown in the following code:
1 usingSystem;2 usingSystem.Collections;3 usingSystem.Collections.Generic;4 usingSystem.Drawing;5 usingSystem.Linq;6 usingsystem.web;7 usingZXing;8 usingZXing.QrCode.Internal;9 usingZxing.common;Ten usingZxing.datamatrix; One usingSystem.IO; A usingzxing.rendering; - usingZxing.qrcode; - the - namespaceUsezxingcreateqrcode.models - { - Public classCreateqrcode + { - Public Static byte[] getqrcodebyzxing (stringMsgstringlogo) + { A at //construction of two-dimensional code write code device -Qrcodewriter Qrwriter =NewQrcodewriter (); -Idictionary<encodehinttype,Object> hints =NewDictionary<encodehinttype,Object>(); -Hints. ADD (Encodehinttype.character_set,"UTF-8"); -Hints. ADD (Encodehinttype.error_correction, ErrorCorrectionLevel.H);//Fault tolerance level -Hints. ADD (Encodehinttype.margin,1);//Two-dimensional code left white margin inBarcodewriter bw =NewBarcodewriter (); -Bitmatrix BM = Qrwriter.encode (msg, Barcodeformat.qr_code, -, -, hints); toBitmap img =bw. Write (BM); + //Get logo -Image Logoimage =image.fromfile (logo); the //calculates the size and position of the inserted picture and calculates the ratio of the logo to the image of the entire QR code * //Customize the proportion of your logo according to your actual needs $ intLogoimagew = Math.min ((int) (IMG. Size.width/4), logoimage.width);Panax Notoginseng intLogoimageh = Math.min ((int) (IMG. Size.Height/4), logoimage.height); - the intLogoimagex = (img. WIDTH-LOGOIMAGEW)/2; + intLogoimagey = (img. Height-logoimageh)/2; A the //convert img to BMP format and create a Graphics object +Bitmap bmpimg =NewBitmap (img. Width, IMG. Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); - using(Graphics g =graphics.fromimage (bmpimg)) $ { $G.interpolationmode =System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; -G.smoothingmode =System.Drawing.Drawing2D.SmoothingMode.HighQuality; -G.compositingquality =System.Drawing.Drawing2D.CompositingQuality.HighQuality; theG.drawimage (IMG,0,0); - }Wuyi the //inserting a picture in a two-dimensional code -System.Drawing.Graphics mygraphic =System.Drawing.Graphics.FromImage (bmpimg); Wu //White background - Mygraphic.fillrectangle (Brushes.white, Logoimagex, Logoimagey, Logoimagew, Logoimageh); AboutMygraphic.drawimage (Logoimage, Logoimagex, Logoimagey, Logoimagew, Logoimageh);//Embed logo $MemoryStream ms =NewMemoryStream (); - bmpimg. Save (ms,system.drawing.imaging.imageformat.jpeg); - byte[] Buffur =Ms. ToArray (); - Ms. Dispose (); A returnBuffur; + } the } -}
View Code
Use the above method, for example:
1 Publicactionresult Createqrcode ()2 {3 stringLogo = AppDomain.CurrentDomain.BaseDirectory +"images\\cloud.jpg";4Viewbag.img ="Data:image/jpeg;base64,"+ convert.tobase64string (UsezxingCreateQRcode.Models.CreateQrCode.GetQrCodeByZXing ("NO070133333", logo));5 returnView ();6}
View Code
........ The time is late, the last bar:
Remember to use open source zxing to generate two-dimensional code images with embedded logo