-Begin-
今天给大家分享一下,如何基于C#来生成并识别条形码或者二维码。
实现二维码生成的库有很多,我们这里采用的是ZXing.Net。
ZXing是一个开放源码的,用Java实现的多种格式的一维二维条码图像处理库,而ZXing.Net是ZXing在.Net平台下的实现。
我们通过Nuget搜索zxing.net即可搜索安装。
安装好ZXing.Net后,我们创建一个BarCodeHelper,对于进行封装,封装了以下几个方法:
方法一:生成条形码
public static Bitmap GenerateBarCode(string text, int width, int height)
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.CODE_39;
EncodingOptions options = new EncodingOptions()
{
Width = width,
Height = height,
Margin = 2,
PureBarcode = true
};
writer.Options = options;
Bitmap map = writer.Write(text);
return map;
}
方法二:生成二维码
public static Bitmap GenerateQRCode(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;
}
方法三:生成带Logo二维码:
public static Bitmap GenerateQRCodeWithLogo(string text, int width, int height, Bitmap logo)
{
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);
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hint);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm);
int[] rectangle = bm.getEnclosingRectangle();
int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
int middleH = Math.Min((int)(rectangle[3] / 3.5), 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);
}
Graphics myGraphic = Graphics.FromImage(bmpimg);
myGraphic.FillRectangle(Brushes.Transparent, middleL, middleT, middleW, middleH);
myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);
return bmpimg;
}
方法四:识别条码二维码:
public static string ReadCode(Bitmap bitmap)
{
BarcodeReader barcodeReader = new BarcodeReader();
barcodeReader.Options.CharacterSet = "UTF-8";
Result result = barcodeReader.Decode(bitmap);
return result.Text;
}
有了以上方法后,编写一个案例来进行功能测试。
按钮事件代码很简单,就是调用BarCodeHelper中的对应方法即可。
private void btn_BarCode_Click(object sender, EventArgs e)
{
this.pic_BarCode.Image = BarCodeHelper.GenerateBarCode(this.txt_BarCode.Text, this.pic_BarCode.Width, this.pic_BarCode.Height);
}
private void btn_QRCode_Click(object sender, EventArgs e)
{
if (chk_Logo.Checked)
{
this.pic_QRCode.Image = BarCodeHelper.GenerateQRCodeWithLogo(this.txt_QRCode.Text,
this.pic_QRCode.Width, this.pic_QRCode.Height, new Bitmap(this.pic_Logo.Image));
}
else
{
this.pic_QRCode.Image = BarCodeHelper.GenerateQRCode(this.txt_QRCode.Text, this.pic_QRCode.Width, this.pic_QRCode.Height);
}
}
private void btn_ReadQRCode_Click(object sender, EventArgs e)
{
MessageBox.Show(BarCodeHelper.ReadCode(new Bitmap(this.pic_QRCode.Image)));
}
private void btn_ReadBarCode_Click(object sender, EventArgs e)
{
MessageBox.Show(BarCodeHelper.ReadCode(new Bitmap(this.pic_BarCode.Image)));
}
最终实现效果如下所示:
-END-
该文章在 2024/12/25 16:13:25 编辑过