C# 利用ZXing.Net来生成条形码和二维码
本文是利用ZXing.Net在WinForm中生成条形码,二维码的小例子,仅供学习分享使用,如有不足之处,还请指正。
什么是ZXing.Net?
ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。而ZXing.Net是ZXing的端口之一。
在工程中引用ZXing.Net
在项目中,点击项目名称右键-->管理NuGet程序包,打开NuGet包管理器窗口,进行搜索下载即可,如下图所示:
ZXing.Net关键类结构图
包括Reader【识别图片中的条形码和二维码】) 和Writer【生成条形码和二维码到图片中】两部分,如下图所示:
涉及知识点:
- BarcodeWriter 用于生成图片格式的条码类,通过Write函数进行输出。继承关系如上图所示。
- BarcodeFormat 枚举类型,条码格式
- QrCodeEncodingOptions 二维码设置选项,继承于EncodingOptions,主要设置宽,高,编码方式等信息。
- MultiFormatWriter 复合格式条码写码器,通过encode方法得到BitMatrix。
- BitMatrix 表示按位表示的二维矩阵数组,元素的值用true和false表示二进制中的1和0。
示例效果图
关键代码
如下所示,包含一维条码,二维条码,和带logo的条码
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;
namespace DemoQrCode
{
/// <summary>
/// 描述:条形码和二维码帮助类
/// 时间:2018-02-18
/// </summary>
public class BarcodeHelper
{
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap Generate1(string text,int width,int height)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
QrCodeEncodingOptions options = new QrCodeEncodingOptions()
{
DisableECI = true,//设置内容编码
CharacterSet = "UTF-8", //设置二维码的宽度和高度
Width = width,
Height = height,
Margin = 1//设置二维码的边距,单位不是固定像素
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
/// <summary>
/// 生成一维条形码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap Generate2(string text,int width,int height)
{
BarcodeWriter writer = new BarcodeWriter();
//使用ITF 格式,不能被现在常用的支付宝、微信扫出来
//如果想生成可识别的可以使用 CODE_128 格式
//writer.Format = BarcodeFormat.ITF;
writer.Format = BarcodeFormat.CODE_39;
EncodingOptions options = new EncodingOptions()
{
Width = width,
Height = height,
Margin = 2
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
/// <summary>
/// 生成带Logo的二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public static Bitmap Generate3(string text, int width, int height)
{
//Logo 图片
string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\img\logo.png";
Bitmap logo = new Bitmap(logoPath);
//构造二维码写码器
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//hint.Add(EncodeHintType.MARGIN, 2);//旧版本不起作用,需要手动去除白边
//生成二维码
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width+30, height+30, hint);
bm = deleteWhite(bm);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm);
//获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
int[] rectangle = bm.getEnclosingRectangle();
//计算插入图片的大小和位置
int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width);
int middleH = Math.Min((int)(rectangle[3] / 3), logo.Height);
int middleL = (map.Width - middleW) / 2;
int middleT = (map.Height - middleH) / 2;
Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmpimg))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(map, 0, 0,width,height);
//白底将二维码插入图片
g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
g.DrawImage(logo, middleL, middleT, middleW, middleH);
}
return bmpimg;
}
/// <summary>
/// 删除默认对应的空白
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
private static BitMatrix deleteWhite(BitMatrix matrix)
{
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++)
{
for (int j = 0; j < resHeight; j++)
{
if (matrix[i + rec[0], j + rec[1]])
resMatrix[i, j]=true;
}
}
return resMatrix;
}
}
}
1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Drawing.Imaging; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 using ZXing; 9 using ZXing.Common; 10 using ZXing.QrCode; 11 using ZXing.QrCode.Internal; 12 13 namespace DemoQrCode 14 { 15 /// <summary> 16 /// 描述:条形码和二维码帮助类 17 /// 时间:2018-02-18 18 /// </summary> 19 public class BarcodeHelper 20 { 21 /// <summary> 22 /// 生成二维码 23 /// </summary> 24 /// <param name="text">内容</param> 25 /// <param name="width">宽度</param> 26 /// <param name="height">高度</param> 27 /// <returns></returns> 28 public static Bitmap Generate1(string text,int width,int height) 29 { 30 BarcodeWriter writer = new BarcodeWriter(); 31 writer.Format = BarcodeFormat.QR_CODE; 32 QrCodeEncodingOptions options = new QrCodeEncodingOptions() 33 { 34 DisableECI = true,//设置内容编码 35 CharacterSet = "UTF-8", //设置二维码的宽度和高度 36 Width = width, 37 Height = height, 38 Margin = 1//设置二维码的边距,单位不是固定像素 39 }; 40 41 writer.Options = options; 42 Bitmap map = writer.Write(text); 43 return map; 44 } 45 46 /// <summary> 47 /// 生成一维条形码 48 /// </summary> 49 /// <param name="text">内容</param> 50 /// <param name="width">宽度</param> 51 /// <param name="height">高度</param> 52 /// <returns></returns> 53 public static Bitmap Generate2(string text,int width,int height) 54 { 55 BarcodeWriter writer = new BarcodeWriter(); 56 //使用ITF 格式,不能被现在常用的支付宝、微信扫出来 57 //如果想生成可识别的可以使用 CODE_128 格式 58 //writer.Format = BarcodeFormat.ITF; 59 writer.Format = BarcodeFormat.CODE_39; 60 EncodingOptions options = new EncodingOptions() 61 { 62 Width = width, 63 Height = height, 64 Margin = 2 65 }; 66 writer.Options = options; 67 Bitmap map = writer.Write(text); 68 return map; 69 } 70 71 /// <summary> 72 /// 生成带Logo的二维码 73 /// </summary> 74 /// <param name="text">内容</param> 75 /// <param name="width">宽度</param> 76 /// <param name="height">高度</param> 77 public static Bitmap Generate3(string text, int width, int height) 78 { 79 //Logo 图片 80 string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\img\logo.png"; 81 Bitmap logo = new Bitmap(logoPath); 82 //构造二维码写码器 83 MultiFormatWriter writer = new MultiFormatWriter(); 84 Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>(); 85 hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); 86 hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 87 //hint.Add(EncodeHintType.MARGIN, 2);//旧版本不起作用,需要手动去除白边 88 89 //生成二维码 90 BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width+30, height+30, hint); 91 bm = deleteWhite(bm); 92 BarcodeWriter barcodeWriter = new BarcodeWriter(); 93 Bitmap map = barcodeWriter.Write(bm); 94 95 //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) 96 int[] rectangle = bm.getEnclosingRectangle(); 97 98 //计算插入图片的大小和位置 99 int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width); 100 int middleH = Math.Min((int)(rectangle[3] / 3), logo.Height); 101 int middleL = (map.Width - middleW) / 2; 102 int middleT = (map.Height - middleH) / 2; 103 104 Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb); 105 using (Graphics g = Graphics.FromImage(bmpimg)) 106 { 107 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 108 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 109 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 110 g.DrawImage(map, 0, 0,width,height); 111 //白底将二维码插入图片 112 g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); 113 g.DrawImage(logo, middleL, middleT, middleW, middleH); 114 } 115 return bmpimg; 116 } 117 118 /// <summary> 119 /// 删除默认对应的空白 120 /// </summary> 121 /// <param name="matrix"></param> 122 /// <returns></returns> 123 private static BitMatrix deleteWhite(BitMatrix matrix) 124 { 125 int[] rec = matrix.getEnclosingRectangle(); 126 int resWidth = rec[2] + 1; 127 int resHeight = rec[3] + 1; 128 129 BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); 130 resMatrix.clear(); 131 for (int i = 0; i < resWidth; i++) 132 { 133 for (int j = 0; j < resHeight; j++) 134 { 135 if (matrix[i + rec[0], j + rec[1]]) 136 resMatrix[i, j]=true; 137 } 138 } 139 return resMatrix; 140 } 141 } 142 }
关于生成条形码和二维码的方式有很多,条码的种类也有很多种,每一种都有其对应的应用领域。
源码下载链接