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

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

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

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

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

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());
}

根据选择的类型生成条码

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;
}

  

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. UVA 12009 - Avaricious Maryanna(数论)

    UVA 12009 - Avaricious Maryanna 题目链接 题意:给定一个n.求出n个数位组成的数字x,x^2的前面|x|位为x 思路:自己先暴力打了前几组数据,发现除了1中有0和1以外 ...

  2. COM-IE-(2)

    # -*- coding:UTF-8 -*- import sys from time import sleep import win32com.client from win32com.client ...

  3. Creational模式之Builder模式

    1.意图 将一个复杂对象的构建与它表示分离,使得相同的构建过程能够创建不同的表示. 查看很多其它请点击 2.别名 无 3.动机 一个RTF(Rich Text Format)文档交换格式的阅读器应能将 ...

  4. 【quickhybrid】H5和原生的职责划分

    前言 在JSBridge实现后,前端网页与原生的交互已经通了,接下来就要开始规划API,明确需要提供哪一些功能来供前端调用. 但是在这之前,还有一点重要工作需要做: 明确H5与Native的职责划分, ...

  5. Error updating database. Cause: java.sql.BatchUpdateException: Field 'id' doesn't have a default value

    异常信息 ### Error updating database. Cause: java.sql.BatchUpdateException: Field 'id' doesn't have a de ...

  6. 小白的Python之路 day4 装饰器前奏

    装饰器前奏: 一.定义: 1.装饰器本质是函数,语法都是用def去定义的 (函数的目的:他需要完成特定的功能) 2.装饰器的功能:就是装饰其他函数(就是为其他函数添加附加功能) 二.原则: 1. 不能 ...

  7. 【java】打印一个对象即打印出该对象toString()返回值

    public class TestToString { public static void main(String[] args){ Node node1=new Node("东邪&quo ...

  8. iOS APP内购

    看到网上文章一大把,看了这个觉得挺不错的,谢谢 iOS大全 公众平台; 原文:http://mp.weixin.qq.com/s?__biz=MzAxMzE2Mjc2Ng==&mid=2652 ...

  9. 小白的Python之路 day4 装饰器高潮

    首先装饰器实现的条件: 高阶函数+嵌套函数 =>装饰器 1.首先,我们先定义一个高级函数,去装饰test1函数,得不到我们想要的操作方式 import time #定义高阶函数 def deco ...

  10. 如何在MQ中实现支持任意延迟的消息?

    什么是定时消息和延迟消息? 定时消息:Producer 将消息发送到 MQ 服务端,但并不期望这条消息立马投递,而是推迟到在当前时间点之后的某一个时间投递到 Consumer 进行消费,该消息即定时消 ...