C# - VS2019調用ZXing.NET實現條碼、二維碼和帶有Logo的二維碼生成


前言

C# WinFrm程序調用ZXing.NET實現條碼、二維碼和帶有Logo的二維碼生成。

ZXing.NET導入

GitHub開源庫

ZXing.NET開源庫githib下載地址:https://github.com/zxing/zxing

NuGet包管理

選擇安裝ZXing.NET v0.16.1版本。

前台部署搭建

如下圖,創建WinFrm桌面應用程序后,添加如下必要的控件。

封裝ZXingLibs類

核心代碼如下:

注意條形碼暫時只支持數字(Requested contents should only contain digits, but got 'i');

只支持偶數個(The lenght of the input should be even);

碼值最大長度為80(Requested contents should be less than 80 digits long, but got 102)。

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Drawing.Imaging;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 using ZXing;
  9 using ZXing.Common;
 10 using ZXing.QrCode;
 11 using ZXing.QrCode.Internal;
 12 
 13 namespace GetBarCodeQRCode_ZXing
 14 {
 15     /// <summary>
 16     /// 重新封裝條碼、二維碼生成方法和帶有圖片的二維碼生成方法
 17     /// </summary>
 18     public class ZXingLibs
 19     {
 20         /// <summary>
 21         /// 生成二維碼
 22         /// </summary>
 23         /// <param name="text">內容</param>
 24         /// <param name="width">寬度</param>
 25         /// <param name="height">高度</param>
 26         /// <returns></returns>
 27         public static Bitmap GetQRCode(string text, int width, int height)
 28         {
 29             BarcodeWriter writer = new BarcodeWriter();
 30             writer.Format = BarcodeFormat.QR_CODE;
 31             QrCodeEncodingOptions options = new QrCodeEncodingOptions()
 32             {
 33                 DisableECI = true,//設置內容編碼
 34                 CharacterSet = "UTF-8",  //設置二維碼的寬度和高度
 35                 Width = width,
 36                 Height = height,
 37                 Margin = 1//設置二維碼的邊距,單位不是固定像素
 38             };
 39 
 40             writer.Options = options;
 41             Bitmap map = writer.Write(text);
 42             return map;
 43         }
 44 
 45         /// <summary>
 46         /// 生成一維條形碼
 47         /// 只支持數字 Requested contents should only contain digits, but got 'i'
 48         /// 只支持偶數個 The lenght of the input should be even
 49         /// 最大長度80  Requested contents should be less than 80 digits long, but got 102
 50         /// </summary>
 51         /// <param name="text">內容</param>
 52         /// <param name="width">寬度</param>
 53         /// <param name="height">高度</param>
 54         /// <returns></returns>
 55         public static Bitmap GetBarCode(string text, int width, int height)
 56         {
 57             BarcodeWriter writer = new BarcodeWriter();
 58             //使用ITF 格式,不能被現在常用的支付寶、微信掃出來
 59             //如果想生成可識別的可以使用 CODE_128 格式
 60             //writer.Format = BarcodeFormat.ITF;
 61             writer.Format = BarcodeFormat.CODE_39;
 62             EncodingOptions options = new EncodingOptions()
 63             {
 64                 Width = width,
 65                 Height = height,
 66                 Margin = 2
 67             };
 68             writer.Options = options;
 69             Bitmap map = writer.Write(text);
 70             return map;
 71         }
 72 
 73         /// <summary>
 74         /// 生成帶Logo的二維碼
 75         /// </summary>
 76         /// <param name="text">內容</param>
 77         /// <param name="width">寬度</param>
 78         /// <param name="height">高度</param>
 79         public static Bitmap GetQRCodeWithLogo(string text, int width, int height)
 80         {
 81             //Logo 圖片
 82             string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\img\logo.bmp";
 83             Bitmap logo = new Bitmap(logoPath);
 84             //構造二維碼寫碼器
 85             MultiFormatWriter writer = new MultiFormatWriter();
 86             Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
 87             hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
 88             hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
 89             //hint.Add(EncodeHintType.MARGIN, 2);//舊版本不起作用,需要手動去除白邊
 90 
 91             //生成二維碼 
 92             BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width + 80, height+80, hint);
 93             bm = deleteWhite(bm);
 94             BarcodeWriter barcodeWriter = new BarcodeWriter();
 95             Bitmap map = barcodeWriter.Write(bm);
 96 
 97             //獲取二維碼實際尺寸(去掉二維碼兩邊空白后的實際尺寸)
 98             int[] rectangle = bm.getEnclosingRectangle();
 99 
100             //計算插入圖片的大小和位置
101             int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
102             int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
103             int middleL = (map.Width - middleW) / 2;
104             int middleT = (map.Height - middleH) / 2;
105 
106             Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
107             using (Graphics g = Graphics.FromImage(bmpimg))
108             {
109                 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
110                 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
111                 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
112                 g.DrawImage(map, 0, 0, width, height);
113                 //白底將二維碼插入圖片
114                 g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
115                 g.DrawImage(logo, middleL, middleT, middleW, middleH);
116             }
117             return bmpimg;
118         }
119 
120         /// <summary>
121         /// 刪除默認對應的空白
122         /// </summary>
123         /// <param name="matrix"></param>
124         /// <returns></returns>
125         private static BitMatrix deleteWhite(BitMatrix matrix)
126         {
127             int[] rec = matrix.getEnclosingRectangle();
128             int resWidth = rec[2] + 1;
129             int resHeight = rec[3] + 1;
130 
131             BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
132             resMatrix.clear();
133             for (int i = 0; i < resWidth; i++)
134             {
135                 for (int j = 0; j < resHeight; j++)
136                 {
137                     if (matrix[i + rec[0], j + rec[1]])
138                         resMatrix[i, j] = true;
139                 }
140             }
141             return resMatrix;
142         }
143     }
144 }
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 
12 namespace GetBarCodeQRCode_ZXing
13 {
14     public partial class FrmMain : Form
15     {
16         public FrmMain()
17         {
18             InitializeComponent();
19         }
20         // 實例化ZXingLibs類
21         public ZXingLibs zxTools = new ZXingLibs();
22 
23         /// <summary>
24         /// 清空輸入框內容
25         /// </summary>
26         /// <param name="sender"></param>
27         /// <param name="e"></param>
28         private void btnClear_Click(object sender, EventArgs e)
29         {
30             this.tbValues.Text = "";
31         }
32 
33         /// <summary>
34         /// 生成條碼
35         /// </summary>
36         /// <param name="sender"></param>
37         /// <param name="e"></param>
38         private void btnBarCode_Click(object sender, EventArgs e)
39         {
40             if (this.tbValues.Text.ToString().Trim() != "")
41             {
42                 this.pbImage.Image = ZXingLibs.GetBarCode(this.tbValues.Text.ToString(), this.pbImage.Width, this.pbImage.Height);
43             }
44         }
45 
46         /// <summary>
47         /// 生成二維碼
48         /// </summary>
49         /// <param name="sender"></param>
50         /// <param name="e"></param>
51         private void btnQRCode_Click(object sender, EventArgs e)
52         {
53             if (this.tbValues.Text.ToString().Trim() != "")
54             {
55                 this.pbImage.Image = ZXingLibs.GetQRCode(this.tbValues.Text.ToString(), this.pbImage.Width, this.pbImage.Height);
56             }
57         }
58 
59         /// <summary>
60         /// 生成帶有Logo的二維碼
61         /// </summary>
62         /// <param name="sender"></param>
63         /// <param name="e"></param>
64         private void btnGetQRCodeWithLogo_Click(object sender, EventArgs e)
65         {
66             this.pbImage.Image = ZXingLibs.GetQRCodeWithLogo(this.tbValues.Text.ToString(), this.pbImage.Width, this.pbImage.Height);
67         }
68     }
69 }
View Code

實現效果

參考資料 GitHub 

  作者:Jeremy.Wu
  出處:https://www.cnblogs.com/jeremywucnblog/
  本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM