C# QRCode、DataMatrix和其他条形码的生成和解码软件

今天制造了一个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;


就这样吧,欢迎交流。

转载于:https://www.cnblogs.com/chenyucong/p/6222391.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值