今天制造了一個C#的軟件,具體是用於生成二維碼和條形碼的,包括常用的QRCode、DataMatrix、Code128、EAN-8等等。
使用的第三方類庫是Zxing.net和DataMatrix.net;另外,程序中也用了委托。
(這兩個類庫可以在VS2015的NuGet管理器中下載)
但說明一下,其中發現PDF_417是有問題,具體問題是生成的條形碼,再解析為字符串時,前面多了“A ”,也不知道是什么原因,反正我很少用到PDF_417,就算了,但其他的編碼已經測試過是沒問題的。
效果圖:
使用非常簡單,用法是:從ListView中選擇編碼類型,然后在Content中輸入內容,點擊“Encode”則在PictureBox中生成編碼,下方的save可以保存圖片,open則是打開本地的編碼圖片,點擊“Decode”后即可自動解碼。
下面直接上代碼(第一段代碼是Helper,后面的代碼如果不感興趣可以忽略):
(1)CodeHelper
1 using DataMatrix.net; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using ZXing; 5 using ZXing.Aztec; 6 using ZXing.Common; 7 using ZXing.OneD; 8 using ZXing.PDF417; 9 using ZXing.QrCode; 10
11 namespace CodeHelper 12 { 13 public class CodeHelper 14 { 15 #region 編碼
16 public static Bitmap Encode_QR(string content, int width = 100, int margin = 1) 17 { 18 QrCodeEncodingOptions opt = new QrCodeEncodingOptions(); 19 opt.DisableECI = true; 20 opt.CharacterSet = "UTF-8"; 21 opt.Width = width; 22 opt.Height = width; 23 opt.Margin = margin; 24
25 BarcodeWriter wr = new BarcodeWriter(); 26 wr.Options = opt; 27 wr.Format = BarcodeFormat.QR_CODE; 28
29 Bitmap bm = wr.Write(content); 30 return bm; 31 } 32
33 public static Bitmap Encode_DM(string content, int moduleSize = 5, int margin = 5) 34 { 35 DmtxImageEncoderOptions opt = new DmtxImageEncoderOptions(); 36 opt.ModuleSize = moduleSize; 37 opt.MarginSize = margin; 38
39 DmtxImageEncoder encoder = new DmtxImageEncoder(); 40
41 Bitmap bm = encoder.EncodeImage(content, opt); 42 return bm; 43 } 44
45 public static Bitmap Encode_PDF_417(string content, int width = 100, int margin = 5) 46 { 47 PDF417EncodingOptions opt = new PDF417EncodingOptions(); 48 opt.Width = width; 49 opt.Margin = margin; 50 opt.CharacterSet = "UTF-8"; 51
52 BarcodeWriter wr = new BarcodeWriter(); 53 wr.Options = opt; 54 wr.Format = BarcodeFormat.PDF_417; 55
56 Bitmap bm = wr.Write(content); 57 return bm; 58 } 59
60 public static Bitmap Encode_AZTEC(string content, int width = 100, int margin = 1) 61 { 62 AztecEncodingOptions opt = new AztecEncodingOptions(); 63 opt.Width = width; 64 opt.Height = width; 65 opt.Margin = margin; 66
67 BarcodeWriter wr = new BarcodeWriter(); 68 wr.Options = opt; 69 wr.Format = BarcodeFormat.AZTEC; 70
71 Bitmap bm = wr.Write(content); 72 return bm; 73 } 74
75 public static Bitmap Encode_Code_128(string content, int heigt = 40, int margin = 5) 76 { 77 Code128EncodingOptions opt = new Code128EncodingOptions(); 78 opt.Height = heigt; 79 opt.Margin = margin; 80
81 BarcodeWriter wr = new BarcodeWriter(); 82 wr.Options = opt; 83 wr.Format = BarcodeFormat.CODE_128; 84
85 Bitmap bm = wr.Write(content); 86 return bm; 87 } 88
89 public static Bitmap Encode_Code_39(string content, int height = 30, int margin = 1) 90 { 91 EncodingOptions encodeOption = new EncodingOptions(); 92 encodeOption.Height = height; 93 encodeOption.Margin = margin; 94
95 BarcodeWriter wr = new BarcodeWriter(); 96 wr.Options = encodeOption; 97 wr.Format = BarcodeFormat.CODE_39; 98
99 Bitmap bm = wr.Write(content); 100 return bm; 101 } 102
103 public static Bitmap Encode_EAN_8(string content, int height = 50, int margin = 1) 104 { 105 EncodingOptions encodeOption = new EncodingOptions(); 106 encodeOption.Height = height; 107 encodeOption.Margin = margin; 108
109 BarcodeWriter wr = new BarcodeWriter(); 110 wr.Options = encodeOption; 111 wr.Format = BarcodeFormat.EAN_8; 112
113 Bitmap bm = wr.Write(content); 114 return bm; 115 } 116
117 public static Bitmap Encode_EAN_13(string content, int height = 50, int margin = 1) 118 { 119 EncodingOptions encodeOption = new EncodingOptions(); 120 encodeOption.Height = height; 121 encodeOption.Margin = margin; 122
123 BarcodeWriter wr = new BarcodeWriter(); 124 wr.Options = encodeOption; 125 wr.Format = BarcodeFormat.EAN_13; 126
127 Bitmap bm = wr.Write(content); 128 return bm; 129 } 130 #endregion
131
132 #region 編碼重載
133 public static Bitmap Encode_QR(string content) 134 { 135 return Encode_QR(content, 100, 1); 136 } 137
138 public static Bitmap Encode_DM(string content) 139 { 140 return Encode_DM(content, 5, 5); 141 } 142
143 public static Bitmap Encode_PDF_417(string content) 144 { 145 return Encode_PDF_417(content, 100, 5); 146 } 147
148 public static Bitmap Encode_AZTEC(string content) 149 { 150 return Encode_AZTEC(content, 100, 1); 151 } 152
153 public static Bitmap Encode_Code_128(string content) 154 { 155 return Encode_Code_128(content, 40, 5); 156 } 157
158 public static Bitmap Encode_Code_39(string content) 159 { 160 return Encode_Code_39(content, 30, 1); 161 } 162
163 public static Bitmap Encode_EAN_8(string content) 164 { 165 return Encode_EAN_8(content, 50, 1); 166 } 167
168 public static Bitmap Encode_EAN_13(string content) 169 { 170 return Encode_EAN_13(content, 50, 1); 171 } 172 #endregion
173
174 /// <summary>
175 /// 全部編碼類型解碼 176 /// </summary>
177 /// <param name="bm"></param>
178 /// <returns></returns>
179 public static string Decode(Bitmap bm) 180 { 181 DecodingOptions opt = new DecodingOptions(); 182 opt.PossibleFormats = new List<BarcodeFormat>() 183 { 184 BarcodeFormat.QR_CODE, 185 BarcodeFormat.DATA_MATRIX, 186 BarcodeFormat.PDF_417, 187 BarcodeFormat.AZTEC, 188 BarcodeFormat.CODE_128, 189 BarcodeFormat.CODE_39, 190 BarcodeFormat.EAN_8, 191 BarcodeFormat.EAN_13 192 }; 193 opt.CharacterSet = "UTF-8"; 194
195 BarcodeReader reader = new BarcodeReader(); 196 reader.Options = opt; 197 Result rs = reader.Decode(bm); 198 if (rs != null) 199 { 200 return rs.Text; 201 } 202
203 //DM
204 DmtxImageDecoder decoder = new DmtxImageDecoder(); 205 List<string> list = decoder.DecodeImage(bm); 206 if (list.Count > 0) 207 { 208 return list[0]; 209 } 210
211 return ""; 212 } 213 } 214 }
(2)Codes類:
1 using System.Collections.Generic; 2 using System.Drawing; 3 using ZXing; 4
5 namespace CodeHelper 6 { 7 public class Code 8 { 9 public string Name { get; set; } 10
11 public BarcodeFormat Type { get; set; } 12
13 public delegateGetBm GetBm { get; set; } 14 } 15
16 public delegate Bitmap delegateGetBm(string content); 17
18 public class Codes 19 { 20 public Dictionary<int, Code> list { get; set; } 21
22 public Codes() 23 { 24 list = new Dictionary<int, Code>(); 25 list.Add(0, new Code { Name = "QR_CODE", Type = BarcodeFormat.QR_CODE, GetBm = CodeHelper.Encode_QR }); 26 list.Add(1, new Code { Name = "DATA_MATRIX", Type = BarcodeFormat.DATA_MATRIX, GetBm = CodeHelper.Encode_DM }); 27 list.Add(2, new Code { Name = "PDF_417", Type = BarcodeFormat.PDF_417, GetBm = CodeHelper.Encode_PDF_417 }); 28 list.Add(3, new Code { Name = "AZTEC", Type = BarcodeFormat.AZTEC, GetBm = CodeHelper.Encode_AZTEC }); 29 list.Add(4, new Code { Name = "CODE_128", Type = BarcodeFormat.CODE_128, GetBm = CodeHelper.Encode_Code_128 }); 30 list.Add(5, new Code { Name = "CODE_39", Type = BarcodeFormat.CODE_39, GetBm = CodeHelper.Encode_Code_39 }); 31 list.Add(6, new Code { Name = "EAN_8", Type = BarcodeFormat.EAN_8, GetBm = CodeHelper.Encode_EAN_8 }); 32 list.Add(7, new Code { Name = "EAN_13", Type = BarcodeFormat.EAN_13, GetBm = CodeHelper.Encode_EAN_13 }); 33 } 34 } 35 }
(3)主界面的后台代碼:
1 using System; 2 using System.Drawing; 3 using System.Windows.Forms; 4
5 namespace CodeHelper 6 { 7 public partial class FrmMain : Form 8 { 9 public Codes Codes = new Codes(); 10
11 public FrmMain() 12 { 13 InitializeComponent(); 14 } 15
16 private void FrmMain_Load(object sender, EventArgs e) 17 { 18 BindListViewItem(); 19 lvType.Items[0].Selected = true; 20 } 21
22 private void btnEncode_Click(object sender, EventArgs e) 23 { 24 string content = txtContent.Text; 25
26 int index = lvType.SelectedItems[0].Index; 27 try
28 { 29 pbCode.Image = Codes.list[index].GetBm(content); 30 } 31 catch (Exception ex) 32 { 33 txtContent.Text = ""; 34 pbCode.Image = null; 35 MessageBox.Show(ex.Message); 36 } 37 } 38
39 private void btnSave_Click(object sender, EventArgs e) 40 { 41 if (pbCode.Image == null) 42 { 43 MessageBox.Show("there is no code."); 44 return; 45 } 46
47 bool isSave = true; 48 SaveFileDialog sfd = new SaveFileDialog(); 49 sfd.Title = "圖片保存"; 50 sfd.Filter = @"jpeg|*.jpg|bmp|*.bmp|png|*.png"; 51 sfd.FileName = txtContent.Text; 52
53 if (sfd.ShowDialog() == DialogResult.OK) 54 { 55 string fileName = sfd.FileName.ToString(); 56 if (fileName != "" && fileName != null) 57 { 58 string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString(); 59 System.Drawing.Imaging.ImageFormat imgformat = null; 60 if (fileExtName != "") 61 { 62 switch (fileExtName) 63 { 64 case "jpg": 65 imgformat = System.Drawing.Imaging.ImageFormat.Jpeg; 66 break; 67 case "bmp": 68 imgformat = System.Drawing.Imaging.ImageFormat.Bmp; 69 break; 70 case "png": 71 imgformat = System.Drawing.Imaging.ImageFormat.Gif; 72 break; 73 default: 74 MessageBox.Show("只能保存為: jpg,bmp,png 格式"); 75 isSave = false; 76 break; 77 } 78 } 79 if (imgformat == null) 80 { 81 imgformat = System.Drawing.Imaging.ImageFormat.Jpeg; 82 } 83 if (isSave) 84 { 85 try
86 { 87 pbCode.Image.Save(fileName, imgformat); 88 } 89 catch
90 { 91 MessageBox.Show("保存失敗,還沒有圖片或已經清空圖片!"); 92 } 93 } 94 } 95 } 96 } 97
98 private void BindListViewItem() 99 { 100 lvType.View = View.LargeIcon; 101 lvType.LargeImageList = imgList; 102 lvType.BeginUpdate(); 103
104 for (int i = 0; i < Codes.list.Count; i++) 105 { 106 ListViewItem lvi = new ListViewItem(); 107 lvi.ImageIndex = i; 108 lvi.Text = Codes.list[i].Name; 109 lvType.Items.Add(lvi); 110 } 111 lvType.EndUpdate(); 112 } 113
114 private void btnDecode_Click(object sender, EventArgs e) 115 { 116 txtContent.Text = ""; 117
118 Bitmap bm = (Bitmap)pbCode.Image; 119 try
120 { 121 txtContent.Text = CodeHelper.Decode(bm); 122 } 123 catch (Exception ex) 124 { 125 MessageBox.Show(ex.Message); 126 } 127 } 128
129 private void btnOpen_Click(object sender, EventArgs e) 130 { 131 txtContent.Text = ""; 132 pbCode.Image = null; 133
134 OpenFileDialog ofd = new OpenFileDialog(); 135 ofd.Title = "打開圖片"; 136 ofd.Filter = @"所有文件|*.*|jpeg|*.jpg|bmp|*.bmp|png|*.png"; 137
138 if (ofd.ShowDialog() == DialogResult.OK) 139 pbCode.Image = Image.FromFile(ofd.FileName); 140 } 141
142 private void lvType_SelectedIndexChanged(object sender, EventArgs e) 143 { 144 pbCode.Image = null; 145 } 146 } 147 }
界面的控件:
Label lblType;
ListView lvType;
Button btnEncode;
Button btnDecode;
PictureBox pbCode;
TextBox txtContent;
Label lblContent;
Button btnSave;
Button btnOpen;
ImageList imgList;
就這樣吧,歡迎交流。