Google ZXing是目前一个常用的基于Java实现的多种格式的1D/2D条码图像处理库,出于其开源的特性其现在已有多平台版本。比如今天要用到的ZXing.Net就是针对微软.Net平台的版本。
使用Google ZXing生成和识别二维码使用Zxing制作二维码需要提供最少包括①是否使用ECI编码段——QrCodeEncodingOptions.DisableECI;②内容编码格式(一般为UTF-8)——QrCodeEncodingOptions.CharacterSet;③二维码的宽高(以像素计)——QrCodeEncodingOptions. Width和QrCodeEncodingOptions. Height;④二维码的边距(以像素计)——QrCodeEncodingOptions. Margin;⑤需承载的内容——String字符串。
共计5种参数。
下面的代码为基于C#.Net Winform窗体使用Zxing.Net类库生成的标准二维码。
//以一个函数调用,text为需要生成的内容 public void Normal(string text) { //调用saveFileDialog选择导出二维码保存位置 string filename = saveFileDialog.FileName; //初始化 BarcodeWriter writer = new BarcodeWriter(); writer.Format = BarcodeFormat.QR_CODE; QrCodeEncodingOptions options = new QrCodeEncodingOptions(); //禁用ECI options.DisableECI = true; //内容编码格式 options.CharacterSet = cbx_code.Text; //二维码的宽高 options.Width = int.Parse(tbx_width.Text); options.Height = int.Parse(tbx_height.Text); //二维码边距 options.Margin = int.Parse(tbx_margin.Text); writer.Options = options; //导出图片 Bitmap p = writer.Write(text); p.Save(filename, System.Drawing.Imaging.ImageFormat.Png); p.Dispose(); //在程序中加载导出的二维码 pictureBox.ImageLocation = filename; }
public void GenerateWithLogo(string text) { //调用openFileDialog选择要插入的Logo openFileDialog.ShowDialog(); Bitmap logo = new Bitmap(openFileDialog.FileName); //调用saveFileDialog选择保存位置 saveFileDialog.ShowDialog(); //初始化 MultiFormatWriter writer = new MultiFormatWriter(); Dictionaryhint = new Dictionary (); hint.Add(EncodeHintType.CHARACTER_SET, cbx_code.Text); hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //生成二维码 BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, int.Parse(tbx_width.Text), int.Parse(tbx_height.Text), hint); BarcodeWriter barcodeWriter = new BarcodeWriter(); Bitmap map = barcodeWriter.Write(bm); //计算尺寸 int[] rectangle = bm.getEnclosingRectangle(); //计算插入Logo的大小位置 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; //将img转换成bmp格式,否则后面无法创建Graphics对象 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); } //将二维码插入图片 Graphics myGraphic = Graphics.FromImage(bmpimg); //白底 myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH); myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH); //保存成图片 bmpimg.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Png); pictureBox.ImageLocation = saveFileDialog.FileName; }
相比于生成,识别二维码则要简单很多。
public void LoadQRC(string filename) { BarcodeReader reader = new BarcodeReader(); //设置读取的格式(一般为UTF-8) reader.Options.CharacterSet = cbx_loadcode.Text; Bitmap p = new Bitmap(filename); Result result = reader.Decode(p); rtbx.Text = result.ToString(); }