zxing .net 多种条码格式的生成
下载地址:http://zxingnet.codeplex.com/
zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便。
本文主要说明一下多种类型条码的生成。
适用的场景,标签可视化设计时,自定义条码类型,预览。
遍历zxing支持的全部条码类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | if (rb == rb1wm) { foreach (BarcodeFormat format in Enum.GetValues( typeof (BarcodeFormat))) { if (format != BarcodeFormat.All_1D) cbxBarcodeFormat.Items.Add(format.ToString()); } cbxBarcodeFormat.Items.Remove(BarcodeFormat.QR_CODE.ToString()); cbxBarcodeFormat.Items.Remove(BarcodeFormat.AZTEC.ToString()); cbxBarcodeFormat.Items.Remove(BarcodeFormat.DATA_MATRIX.ToString()); cbxBarcodeFormat.Items.Remove(BarcodeFormat.PDF_417.ToString()); } if (rb == rb2wm) { cbxBarcodeFormat.Items.Add(BarcodeFormat.QR_CODE.ToString()); cbxBarcodeFormat.Items.Add(BarcodeFormat.AZTEC.ToString()); cbxBarcodeFormat.Items.Add(BarcodeFormat.DATA_MATRIX.ToString()); cbxBarcodeFormat.Items.Add(BarcodeFormat.PDF_417.ToString()); } |
根据选择的类型生成条码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | Bitmap bitmap = new Bitmap(pbxBarcode.Width, pbxBarcode.Height); Graphics g = Graphics.FromImage(bitmap); g.Clear(Color.White); Format = (BarcodeFormat)Enum.Parse( typeof (BarcodeFormat), cbxBarcodeFormat.SelectedItem.ToString()); try { var options = new ZXing.Common.EncodingOptions { PureBarcode = !chxDisplayBarcode.Checked }; #region 根据条码类型Write Image switch (Format) { case BarcodeFormat.QR_CODE: #region QRCode if (cbxErrorLevel.SelectedItem.ToString().Equals( "L" )) ErrorCorrectionLevel = QR_ErrorCorrectionLevel.L; if (cbxErrorLevel.SelectedItem.ToString().Equals( "H" )) ErrorCorrectionLevel = QR_ErrorCorrectionLevel.H; if (cbxErrorLevel.SelectedItem.ToString().Equals( "M" )) ErrorCorrectionLevel = QR_ErrorCorrectionLevel.M; if (cbxErrorLevel.SelectedItem.ToString().Equals( "Q" )) ErrorCorrectionLevel = QR_ErrorCorrectionLevel.Q; ErrorCorrectionLevel level = null ; switch (ErrorCorrectionLevel) { case QR_ErrorCorrectionLevel.H: level = ZXing.QrCode.Internal.ErrorCorrectionLevel.H; break ; case QR_ErrorCorrectionLevel.M: level = ZXing.QrCode.Internal.ErrorCorrectionLevel.M; break ; case QR_ErrorCorrectionLevel.L: level = ZXing.QrCode.Internal.ErrorCorrectionLevel.L; break ; case QR_ErrorCorrectionLevel.Q: level = ZXing.QrCode.Internal.ErrorCorrectionLevel.Q; break ; } QrCodeEncodingOptions qr_options = new QrCodeEncodingOptions { Margin = 0, DisableECI = true , CharacterSet = "UTF-8" , ErrorCorrection = level, PureBarcode = !chxDisplayBarcode.Checked, Width = pbxBarcode.Width, Height = pbxBarcode.Height }; var qrWriter = new ZXing.BarcodeWriter(); qrWriter.Format = BarcodeFormat.QR_CODE; qrWriter.Options = qr_options; #endregion bitmap = qrWriter.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(qrWriter.Options, Format, bitmap); break ; case BarcodeFormat.PDF_417: #region PDF417 PDF417EncodingOptions pdf_options = new PDF417EncodingOptions { Margin = 0, DisableECI = true , CharacterSet = "UTF-8" , Width = pbxBarcode.Width, Height = pbxBarcode.Height, PureBarcode = !chxDisplayBarcode.Checked }; var pdf417Writer = new ZXing.BarcodeWriter(); pdf417Writer.Format = BarcodeFormat.PDF_417; pdf417Writer.Options = pdf_options; #endregion bitmap = pdf417Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(pdf417Writer.Options, Format, bitmap); break ; case BarcodeFormat.DATA_MATRIX: #region DataMatrix DatamatrixEncodingOptions dataMatrix_options = new DatamatrixEncodingOptions { Margin = 0, SymbolShape = (ZXing.Datamatrix.Encoder.SymbolShapeHint)(Enum.Parse( typeof (ZXing.Datamatrix.Encoder.SymbolShapeHint), cbxDataMatrixOption.SelectedItem.ToString())), Width = pbxBarcode.Width, Height = pbxBarcode.Height, PureBarcode = !chxDisplayBarcode.Checked, }; var dataMatrixWriter = new ZXing.BarcodeWriter(); dataMatrixWriter.Format = BarcodeFormat.DATA_MATRIX; dataMatrixWriter.Options = dataMatrix_options; #endregion bitmap = dataMatrixWriter.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(dataMatrixWriter.Options, Format, bitmap); break ; case BarcodeFormat.AZTEC: #region Aztec ZXing.Aztec.AztecEncodingOptions aztecEncodingOptions = new ZXing.Aztec.AztecEncodingOptions { Margin = 0, ErrorCorrection = 2, PureBarcode = !chxDisplayBarcode.Checked, Layers = 16 }; var aztecWriter = new ZXing.BarcodeWriter(); aztecWriter.Format = BarcodeFormat.AZTEC; aztecWriter.Options = aztecEncodingOptions; #endregion bitmap = aztecWriter.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(aztecWriter.Options, Format, bitmap); break ; case BarcodeFormat.CODE_128: #region Code128 ZXing.OneD.Code128EncodingOptions code128_options = new ZXing.OneD.Code128EncodingOptions { Margin = 0, PureBarcode = !chxDisplayBarcode.Checked, Width = pbxBarcode.Width, Height = pbxBarcode.Height, ForceCodesetB = true }; var code128_Writer = new ZXing.BarcodeWriter(); code128_Writer.Format = BarcodeFormat.CODE_128; code128_Writer.Options = code128_options; #endregion bitmap = code128_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(code128_Writer.Options, Format, bitmap); break ; case BarcodeFormat.CODABAR: var codeBar_Writer = new ZXing.BarcodeWriter(); codeBar_Writer.Format = BarcodeFormat.CODABAR; codeBar_Writer.Options = options; bitmap = codeBar_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; case BarcodeFormat.EAN_13: var ean13_Writer = new ZXing.BarcodeWriter(); ean13_Writer.Format = BarcodeFormat.EAN_13; ean13_Writer.Options = options; bitmap = ean13_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; case BarcodeFormat.EAN_8: var ean8_Writer = new ZXing.BarcodeWriter(); ean8_Writer.Format = BarcodeFormat.EAN_8; ean8_Writer.Options = options; bitmap = ean8_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; case BarcodeFormat.CODE_39: var code39_Writer = new ZXing.BarcodeWriter(); code39_Writer.Format = BarcodeFormat.CODE_39; code39_Writer.Options = options; bitmap = code39_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; case BarcodeFormat.UPC_A: var upca_Writer = new ZXing.BarcodeWriter(); upca_Writer.Format = BarcodeFormat.UPC_A; upca_Writer.Options = options; bitmap = upca_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; case BarcodeFormat.UPC_E: var upce_Writer = new ZXing.BarcodeWriter(); upce_Writer.Format = BarcodeFormat.UPC_E; upce_Writer.Options = options; bitmap = upce_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; case BarcodeFormat.MSI: var msi_Writer = new ZXing.BarcodeWriter(); msi_Writer.Format = BarcodeFormat.MSI; msi_Writer.Options = options; bitmap = msi_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; case BarcodeFormat.ITF: var itf_Writer = new ZXing.BarcodeWriter(); itf_Writer.Format = BarcodeFormat.ITF; itf_Writer.Options = options; bitmap = itf_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; case BarcodeFormat.PLESSEY: var plessey_Writer = new ZXing.BarcodeWriter(); plessey_Writer.Format = BarcodeFormat.PLESSEY; plessey_Writer.Options = options; bitmap = plessey_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; case BarcodeFormat.MAXICODE: var code_Writer = new ZXing.BarcodeWriter(); code_Writer.Format = BarcodeFormat.MAXICODE; code_Writer.Options = options; bitmap = code_Writer.Write(tbxBarcodeValue.Text.Trim()); BarCodeOptionsChanged?.Invoke(options, Format, bitmap); break ; default : throw new Exception( "条码格式暂不支持!" ); } #endregion } catch (Exception ex) { MessageBox.Show( "编码生成错误:" + ex.Message, "系统提示" , MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { pbxBarcode.Image = bitmap; } |
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/p/datacool_2017_zxing.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2015-10-11 也许每个农村出来的码农都有个田园梦