前言
C# WinFrm程序调用ZXing.NET实现条码、二维码和带有Logo的二维码生成。
ZXing.NET导入
GitHub开源库
ZXing.NET开源库githib下载地址:https://github.com/zxing/zxing
NuGet包管理
选择安装ZXing.NET v0.16.1版本。
前台部署搭建
如下图,创建WinFrm桌面应用程序后,添加如下必要的控件。
封装ZXingLibs类
核心代码如下:
注意条形码暂时只支持数字(Requested contents should only contain digits, but got 'i');
只支持偶数个(The lenght of the input should be even);
码值最大长度为80(Requested contents should be less than 80 digits long, but got 102)。
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 GetBarCodeQRCode_ZXing
{
/// <summary>
/// 重新封装条码、二维码生成方法和带有图片的二维码生成方法
/// </summary>
public class ZXingLibs
{
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap GetQRCode(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 = //设置二维码的边距,单位不是固定像素
}; writer.Options = options;
Bitmap map = writer.Write(text);
return map;
} /// <summary>
/// 生成一维条形码
/// 只支持数字 Requested contents should only contain digits, but got 'i'
/// 只支持偶数个 The lenght of the input should be even
/// 最大长度80 Requested contents should be less than 80 digits long, but got 102
/// </summary>
/// <param name="text">内容</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <returns></returns>
public static Bitmap GetBarCode(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 =
};
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 GetQRCodeWithLogo(string text, int width, int height)
{
//Logo 图片
string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\img\logo.bmp";
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 + , height+, hint);
bm = deleteWhite(bm);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置
int middleW = Math.Min((int)(rectangle[] / 3.5), logo.Width);
int middleH = Math.Min((int)(rectangle[] / 3.5), logo.Height);
int middleL = (map.Width - middleW) / ;
int middleT = (map.Height - middleH) / ; 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, , , 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[] + ;
int resHeight = rec[] + ; BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = ; i < resWidth; i++)
{
for (int j = ; j < resHeight; j++)
{
if (matrix[i + rec[], j + rec[]])
resMatrix[i, j] = true;
}
}
return resMatrix;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace GetBarCodeQRCode_ZXing
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
// 实例化ZXingLibs类
public ZXingLibs zxTools = new ZXingLibs(); /// <summary>
/// 清空输入框内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClear_Click(object sender, EventArgs e)
{
this.tbValues.Text = "";
} /// <summary>
/// 生成条码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBarCode_Click(object sender, EventArgs e)
{
if (this.tbValues.Text.ToString().Trim() != "")
{
this.pbImage.Image = ZXingLibs.GetBarCode(this.tbValues.Text.ToString(), this.pbImage.Width, this.pbImage.Height);
}
} /// <summary>
/// 生成二维码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQRCode_Click(object sender, EventArgs e)
{
if (this.tbValues.Text.ToString().Trim() != "")
{
this.pbImage.Image = ZXingLibs.GetQRCode(this.tbValues.Text.ToString(), this.pbImage.Width, this.pbImage.Height);
}
} /// <summary>
/// 生成带有Logo的二维码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGetQRCodeWithLogo_Click(object sender, EventArgs e)
{
this.pbImage.Image = ZXingLibs.GetQRCodeWithLogo(this.tbValues.Text.ToString(), this.pbImage.Width, this.pbImage.Height);
}
}
}
实现效果
参考资料 GitHub
作者:Jeremy.Wu
出处:https://www.cnblogs.com/jeremywucnblog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。