下载地址:http://zxingnet.codeplex.com/

zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便。

本文主要说明一下多种类型条码的生成。

适用的场景,标签可视化设计时,自定义条码类型,预览。

遍历zxing支持的全部条码类型

  1. if (rb == rb1wm)
  2. {
  3. foreach (BarcodeFormat format in Enum.GetValues(typeof(BarcodeFormat)))
  4. {
  5. if (format != BarcodeFormat.All_1D)
  6. cbxBarcodeFormat.Items.Add(format.ToString());
  7. }
  8. cbxBarcodeFormat.Items.Remove(BarcodeFormat.QR_CODE.ToString());
  9. cbxBarcodeFormat.Items.Remove(BarcodeFormat.AZTEC.ToString());
  10. cbxBarcodeFormat.Items.Remove(BarcodeFormat.DATA_MATRIX.ToString());
  11. cbxBarcodeFormat.Items.Remove(BarcodeFormat.PDF_417.ToString());
  12. }
  13. if (rb == rb2wm)
  14. {
  15. cbxBarcodeFormat.Items.Add(BarcodeFormat.QR_CODE.ToString());
  16. cbxBarcodeFormat.Items.Add(BarcodeFormat.AZTEC.ToString());
  17. cbxBarcodeFormat.Items.Add(BarcodeFormat.DATA_MATRIX.ToString());
  18. cbxBarcodeFormat.Items.Add(BarcodeFormat.PDF_417.ToString());
  19. }

根据选择的类型生成条码

  1. Bitmap bitmap = new Bitmap(pbxBarcode.Width, pbxBarcode.Height);
  2. Graphics g = Graphics.FromImage(bitmap);
  3. g.Clear(Color.White);
  4. Format = (BarcodeFormat)Enum.Parse(typeof(BarcodeFormat), cbxBarcodeFormat.SelectedItem.ToString());
  5. try
  6. {
  7. var options = new ZXing.Common.EncodingOptions
  8. {
  9. PureBarcode = !chxDisplayBarcode.Checked
  10. };
  11. #region 根据条码类型Write Image
  12. switch (Format)
  13. {
  14. case BarcodeFormat.QR_CODE:
  15. #region QRCode
  16. if (cbxErrorLevel.SelectedItem.ToString().Equals("L"))
  17. ErrorCorrectionLevel = QR_ErrorCorrectionLevel.L;
  18. if (cbxErrorLevel.SelectedItem.ToString().Equals("H"))
  19. ErrorCorrectionLevel = QR_ErrorCorrectionLevel.H;
  20. if (cbxErrorLevel.SelectedItem.ToString().Equals("M"))
  21. ErrorCorrectionLevel = QR_ErrorCorrectionLevel.M;
  22. if (cbxErrorLevel.SelectedItem.ToString().Equals("Q"))
  23. ErrorCorrectionLevel = QR_ErrorCorrectionLevel.Q;
  24. ErrorCorrectionLevel level = null;
  25. switch (ErrorCorrectionLevel)
  26. {
  27. case QR_ErrorCorrectionLevel.H:
  28. level = ZXing.QrCode.Internal.ErrorCorrectionLevel.H;
  29. break;
  30. case QR_ErrorCorrectionLevel.M:
  31. level = ZXing.QrCode.Internal.ErrorCorrectionLevel.M;
  32. break;
  33. case QR_ErrorCorrectionLevel.L:
  34. level = ZXing.QrCode.Internal.ErrorCorrectionLevel.L;
  35. break;
  36. case QR_ErrorCorrectionLevel.Q:
  37. level = ZXing.QrCode.Internal.ErrorCorrectionLevel.Q;
  38. break;
  39. }
  40. QrCodeEncodingOptions qr_options = new QrCodeEncodingOptions
  41. {
  42. Margin = 0,
  43. DisableECI = true,
  44. CharacterSet = "UTF-8",
  45. ErrorCorrection = level,
  46. PureBarcode = !chxDisplayBarcode.Checked,
  47. Width = pbxBarcode.Width,
  48. Height = pbxBarcode.Height
  49. };
  50. var qrWriter = new ZXing.BarcodeWriter();
  51. qrWriter.Format = BarcodeFormat.QR_CODE;
  52. qrWriter.Options = qr_options;
  53. #endregion
  54. bitmap = qrWriter.Write(tbxBarcodeValue.Text.Trim());
  55. BarCodeOptionsChanged?.Invoke(qrWriter.Options, Format, bitmap);
  56. break;
  57. case BarcodeFormat.PDF_417:
  58. #region PDF417
  59. PDF417EncodingOptions pdf_options = new PDF417EncodingOptions
  60. {
  61. Margin = 0,
  62. DisableECI = true,
  63. CharacterSet = "UTF-8",
  64. Width = pbxBarcode.Width,
  65. Height = pbxBarcode.Height,
  66. PureBarcode = !chxDisplayBarcode.Checked
  67. };
  68. var pdf417Writer = new ZXing.BarcodeWriter();
  69. pdf417Writer.Format = BarcodeFormat.PDF_417;
  70. pdf417Writer.Options = pdf_options;
  71. #endregion
  72. bitmap = pdf417Writer.Write(tbxBarcodeValue.Text.Trim());
  73. BarCodeOptionsChanged?.Invoke(pdf417Writer.Options, Format, bitmap);
  74. break;
  75. case BarcodeFormat.DATA_MATRIX:
  76. #region DataMatrix
  77. DatamatrixEncodingOptions dataMatrix_options = new DatamatrixEncodingOptions
  78. {
  79. Margin = 0,
  80. SymbolShape = (ZXing.Datamatrix.Encoder.SymbolShapeHint)(Enum.Parse(typeof(ZXing.Datamatrix.Encoder.SymbolShapeHint), cbxDataMatrixOption.SelectedItem.ToString())),
  81. Width = pbxBarcode.Width,
  82. Height = pbxBarcode.Height,
  83. PureBarcode = !chxDisplayBarcode.Checked,
  84. };
  85. var dataMatrixWriter = new ZXing.BarcodeWriter();
  86. dataMatrixWriter.Format = BarcodeFormat.DATA_MATRIX;
  87. dataMatrixWriter.Options = dataMatrix_options;
  88. #endregion
  89. bitmap = dataMatrixWriter.Write(tbxBarcodeValue.Text.Trim());
  90. BarCodeOptionsChanged?.Invoke(dataMatrixWriter.Options, Format, bitmap);
  91. break;
  92. case BarcodeFormat.AZTEC:
  93. #region Aztec
  94. ZXing.Aztec.AztecEncodingOptions aztecEncodingOptions = new ZXing.Aztec.AztecEncodingOptions
  95. {
  96. Margin = 0,
  97. ErrorCorrection = 2,
  98. PureBarcode = !chxDisplayBarcode.Checked,
  99. Layers = 16
  100. };
  101. var aztecWriter = new ZXing.BarcodeWriter();
  102. aztecWriter.Format = BarcodeFormat.AZTEC;
  103. aztecWriter.Options = aztecEncodingOptions;
  104. #endregion
  105. bitmap = aztecWriter.Write(tbxBarcodeValue.Text.Trim());
  106. BarCodeOptionsChanged?.Invoke(aztecWriter.Options, Format, bitmap);
  107. break;
  108. case BarcodeFormat.CODE_128:
  109. #region Code128
  110. ZXing.OneD.Code128EncodingOptions code128_options = new ZXing.OneD.Code128EncodingOptions
  111. {
  112. Margin = 0,
  113. PureBarcode = !chxDisplayBarcode.Checked,
  114. Width = pbxBarcode.Width,
  115. Height = pbxBarcode.Height,
  116. ForceCodesetB = true
  117. };
  118. var code128_Writer = new ZXing.BarcodeWriter();
  119. code128_Writer.Format = BarcodeFormat.CODE_128;
  120. code128_Writer.Options = code128_options;
  121. #endregion
  122. bitmap = code128_Writer.Write(tbxBarcodeValue.Text.Trim());
  123. BarCodeOptionsChanged?.Invoke(code128_Writer.Options, Format, bitmap);
  124. break;
  125. case BarcodeFormat.CODABAR:
  126. var codeBar_Writer = new ZXing.BarcodeWriter();
  127. codeBar_Writer.Format = BarcodeFormat.CODABAR;
  128. codeBar_Writer.Options = options;
  129. bitmap = codeBar_Writer.Write(tbxBarcodeValue.Text.Trim());
  130. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  131. break;
  132. case BarcodeFormat.EAN_13:
  133. var ean13_Writer = new ZXing.BarcodeWriter();
  134. ean13_Writer.Format = BarcodeFormat.EAN_13;
  135. ean13_Writer.Options = options;
  136. bitmap = ean13_Writer.Write(tbxBarcodeValue.Text.Trim());
  137. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  138. break;
  139. case BarcodeFormat.EAN_8:
  140. var ean8_Writer = new ZXing.BarcodeWriter();
  141. ean8_Writer.Format = BarcodeFormat.EAN_8;
  142. ean8_Writer.Options = options;
  143. bitmap = ean8_Writer.Write(tbxBarcodeValue.Text.Trim());
  144. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  145. break;
  146. case BarcodeFormat.CODE_39:
  147. var code39_Writer = new ZXing.BarcodeWriter();
  148. code39_Writer.Format = BarcodeFormat.CODE_39;
  149. code39_Writer.Options = options;
  150. bitmap = code39_Writer.Write(tbxBarcodeValue.Text.Trim());
  151. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  152. break;
  153. case BarcodeFormat.UPC_A:
  154. var upca_Writer = new ZXing.BarcodeWriter();
  155. upca_Writer.Format = BarcodeFormat.UPC_A;
  156. upca_Writer.Options = options;
  157. bitmap = upca_Writer.Write(tbxBarcodeValue.Text.Trim());
  158. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  159. break;
  160. case BarcodeFormat.UPC_E:
  161. var upce_Writer = new ZXing.BarcodeWriter();
  162. upce_Writer.Format = BarcodeFormat.UPC_E;
  163. upce_Writer.Options = options;
  164. bitmap = upce_Writer.Write(tbxBarcodeValue.Text.Trim());
  165. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  166. break;
  167. case BarcodeFormat.MSI:
  168. var msi_Writer = new ZXing.BarcodeWriter();
  169. msi_Writer.Format = BarcodeFormat.MSI;
  170. msi_Writer.Options = options;
  171. bitmap = msi_Writer.Write(tbxBarcodeValue.Text.Trim());
  172. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  173. break;
  174. case BarcodeFormat.ITF:
  175. var itf_Writer = new ZXing.BarcodeWriter();
  176. itf_Writer.Format = BarcodeFormat.ITF;
  177. itf_Writer.Options = options;
  178. bitmap = itf_Writer.Write(tbxBarcodeValue.Text.Trim());
  179. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  180. break;
  181. case BarcodeFormat.PLESSEY:
  182. var plessey_Writer = new ZXing.BarcodeWriter();
  183. plessey_Writer.Format = BarcodeFormat.PLESSEY;
  184. plessey_Writer.Options = options;
  185. bitmap = plessey_Writer.Write(tbxBarcodeValue.Text.Trim());
  186. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  187. break;
  188. case BarcodeFormat.MAXICODE:
  189. var code_Writer = new ZXing.BarcodeWriter();
  190. code_Writer.Format = BarcodeFormat.MAXICODE;
  191. code_Writer.Options = options;
  192. bitmap = code_Writer.Write(tbxBarcodeValue.Text.Trim());
  193. BarCodeOptionsChanged?.Invoke(options, Format, bitmap);
  194. break;
  195. default:
  196. throw new Exception("条码格式暂不支持!");
  197. }
  198. #endregion
  199. }
  200. catch (Exception ex)
  201. {
  202. MessageBox.Show("编码生成错误:" + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  203. }
  204. finally
  205. {
  206. pbxBarcode.Image = bitmap;
  207. }

  

zxing .net 多种条码格式的生成的更多相关文章

  1. C# - VS2019调用ZXing.NET实现条码、二维码和带有Logo的二维码生成

    前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码生成. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https:/ ...

  2. java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例

    java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍   我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...

  3. ini格式数据生成与解析具体解释

    ini格式数据生成与解析具体解释 1.ini格式数据长啥样? watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/ ...

  4. python 将png图片格式转换生成gif动画

    先看知乎上面的一个连接 用Python写过哪些[脑洞大开]的小工具? https://www.zhihu.com/question/33646570/answer/157806339 这个哥们通过爬气 ...

  5. C# - VS2019 WinFrm程序调用ZXing.NET实现条码、二维码和带有Logo的二维码的识别

    前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码的识别. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https: ...

  6. 2018-8-10-VisualStudio-2017-项目格式-自动生成版本号

    title author date CreateTime categories VisualStudio 2017 项目格式 自动生成版本号 lindexi 2018-08-10 19:16:52 + ...

  7. Android zxing 解析二维码,生成二维码极简demo

    zxing 官方的代码很多,看起来很费劲,此demo只抽取了有用的部分,实现了相机预览解码,解析本地二维码,生成二维码三个功能. 简化后的结构如下: 废话少说直接上代码: BaseDecodeHand ...

  8. 将DataSet(DataTable)转换成JSON格式(生成JS文件存储)

    public static string CreateJsonParameters(DataTable dt) { /**/ /**/ /**/ /* /*********************** ...

  9. JAVA中通过时间格式来生成唯一的文件名

    有时候我们需要截图,在要截图时,有人用到了时间格式,但是时间格式中的:在文件名称中是不被允许的字符,所以就会报错,如何生成唯一的时间文件名: package com.demo; import java ...

随机推荐

  1. Chromium与CEF的多进程模型及相关參数

    CEF基于Chromium,也是多进程模型.关于进程模型.參考这里:https://www.chromium.org/developers/design-documents/process-model ...

  2. mac os x 触摸板点击无效

    macbook默认轻击触摸板无效,这样是为了防止误点击.可是习惯了windows笔记本的我对这一设置非常不习惯. 能够在"system preference"的"Trac ...

  3. 轻松上云,从容实施Office 365项目

    这个是我在MVP 社区活动的一节课程,讲述Office 365部署中一些大的挑战和解决的方法 视频URL 例如以下: http://edu.51cto.com/lesson/id-17440.html ...

  4. 转:java单例设计模式

    本文转自:http://www.cnblogs.com/yinxiaoqiexuxing/p/5605338.html 单例设计模式 Singleton是一种创建型模式,指某个类采用Singleton ...

  5. 将IDEA maven项目中src源代码下的xml等资源文件编译进classes文件夹

    如果使用的是Eclipse,Eclipse的src目录下的xml等资源文件在编译的时候会自动打包进输出到classes文件夹.Hibernate和Spring有时会将配置文件放置在src目录下,编译后 ...

  6. python_web----------数据可视化从0到1的过程

    一.数据可视化项目配置 1. django + Echarts 2. 服务器(linux:Ubuntu 17.04 (GNU/Linux 4.10.0-40-generic x86_64)) 3. I ...

  7. 让intellij idea 14 支持ES6语法

    用eclipse做前端开发,用到了webpack,结果各种依赖导致软件卡的一比,简直不能动!虽然在同事的帮忙下,修改了一下配置,但仍然卡的没脾气.改用intellij idea 14解决了卡的问题,但 ...

  8. Fragment多重嵌套实现电影,影院展示页

    转载请标明出处: http://www.cnblogs.com/dingxiansen/p/8135888.html 本文出自:丁先森-博客园 公司以前的app是用H5封的,由于一个模块效果用H5实现 ...

  9. ASP.NET Core 一步步搭建个人网站(3)_菜单管理

    上一章,我们实现了用户的注册和登录,登录之后展示的是我们的主页,页面的左侧是多级的导航菜单,定位并展示用户需要访问的不同页面.目前导航菜单是写死的,考虑以后菜单管理的便捷性,我们这节实现下可视化配置菜 ...

  10. iOS常见的几种加密方法(base64.MD5.Token传值.系统指纹验证。。加密)

    普通加密方法是讲密码进行加密后保存到用户偏好设置中 钥匙串是以明文形式保存,但是不知道存放的具体位置 一. base64加密 base64 编码是现代密码学的基础 基本原理: 原本是 8个bit 一组 ...