Use the open-source zxing to generate a QR code image with embedded logo.

Source: Internet
Author: User

Use the open-source zxing to generate a QR code image with embedded logo.

I used zxing to generate a QR code in the project. Recently, another project also needs a QR code, so I learned to use zxing to generate a QR code again.


Next, let's prepare for it. Because I developed it on vs2013, I chose zxing. dll of. net4.5. I also prepared an image for embedding. The Code is as follows:

1 using System; 2 using System. collections; 3 using System. collections. generic; 4 using System. drawing; 5 using System. linq; 6 using System. web; 7 using ZXing; 8 using ZXing. qrCode. internal; 9 using ZXing. common; 10 using ZXing. datamatrix; 11 using System. IO; 12 using ZXing. rendering; 13 using ZXing. qrCode; 14 15 16 namespace UsezxingCreateQRcode. models17 {18 public class CreateQrCode19 {20 public static byte [] GetQrCodeByZXing (string msg, string logo) 21 {22 23 // construct a QR code to Write 24 QRCodeWriter qrwriter = new QRCodeWriter (); 25 IDictionary <EncodeHintType, object> hints = new Dictionary <EncodeHintType, object> (); 26 hints. add (EncodeHintType. CHARACTER_SET, "UTF-8"); 27 hints. add (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. h); // error tolerance level 28 hints. add (EncodeHintType. MARGIN, 1); // The white MARGIN of the QR code 29 BarcodeWriter bw = new BarcodeWriter (); 30 BitMatrix bm = qrwriter. encode (msg, BarcodeFormat. QR_CODE, 300,300, hints); 31 Bitmap img = bw. write (bm); 32 // get logo33 Image logoImage = Image. fromFile (logo); 34 // calculate the size and position of the inserted image, calculate the ratio of logo to the image of the whole QR code. The ratio of logo to the image is 35. // you can customize the proportion of logo to 36. int logoImageW = Math. min (int) (img. size. width/4), logoImage. width); 37 int logoImageH = Math. min (int) (img. size. height/4), logoImage. height); 38 39 int logoImageX = (img. width-logoImageW)/2; 40 int logoImageY = (img. height-logoImageH)/2; 41 42 // convert img to bmp format, and create a Graphics object 43 Bitmap bmp img = new Bitmap (img. width, img. height, System. drawing. imaging. pixelFormat. format32bppArgb); 44 using (Graphics g = Graphics. fromImage (bmp img) 45 {46g. interpolationMode = System. drawing. drawing2D. interpolationMode. highQualityBicubic; 47g. smoothingMode = System. drawing. drawing2D. smoothingMode. highQuality; 48g. compositingQuality = System. drawing. drawing2D. compositingQuality. highQuality; 49g. drawImage (img, 0, 0); 50} 51 52 // Insert the image 53 System in the QR code. drawing. graphics MyGraphic = System. drawing. graphics. fromImage (bmp img); 54 // white background 55 MyGraphic. fillRectangle (Brushes. white, logoImageX, logoImageY, logoImageW, logoImageH); 56 MyGraphic. drawImage (logoImage, logoImageX, logoImageY, logoImageW, logoImageH); // embedded logo57 MemoryStream MS = new MemoryStream (); 58 bmp img. save (MS, System. drawing. imaging. imageFormat. jpeg); 59 byte [] buffur = ms. toArray (); 60 ms. dispose (); 61 return buffur; 62} 63} 64}View Code

The preceding method is used as an example:

1 public ActionResult CreateQrCode () 2 {3 string logo = AppDomain. currentDomain. baseDirectory + "Images \ cloud.jpg"; 4 ViewBag. img = "data: image/Jpeg; base64," + Convert. toBase64String (UsezxingCreateQRcode. models. createQrCode. getQrCodeByZXing ("NO070133333", logo); 5 return View (); 6}View Code

It's not too early. The last one is:

 

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.