一、前言
大家好!我是付工。
今天给大家分享一下,如何基于C#来生成并识别条形码或者二维码。
二、ZXing.Net
实现二维码生成的库有很多,我们这里采用的是http://ZXing.Net。
ZXing是一个开放源码的,用Java实现的多种格式的一维二维条码图像处理库,而http://ZXing.Net是ZXing在.Net平台下的实现。
我们通过Nuget搜索http://zxing.net即可搜索安装。
三、BarCodeHelper
安装好http://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)));
}
最终实现效果如下所示: