首页 > 代码库 > 【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)
【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)
ZXing .NET 的项目主页
https://zxingnet.codeplex.com/
代码基本上抄袭自下面两篇文章 XD
http://www.cnblogs.com/tianma3798/p/5426869.html
http://www.cnblogs.com/tianma3798/p/5426880.html
仅作参数优化,更加实用和简便一点
Shared Function MakeQR(ByVal qrtext As String, Optional ByVal width As Integer = 800, Optional ByVal height As Integer = 800, Optional ByVal margin As Integer = 1) As Bitmap Dim writer As New ZXing.BarcodeWriter writer.Format = ZXing.BarcodeFormat.QR_CODE Dim opt As New ZXing.QrCode.QrCodeEncodingOptions opt.DisableECI = True ‘设置为True才可以调整编码 opt.CharacterSet = "UTF-8" ‘文本编码,建议设置为UTF-8 opt.Width = width ‘宽度 opt.Height = height ‘高度 opt.Margin = margin ‘边距,貌似不是像素格式,因此不宜设置过大 writer.Options = opt Return writer.Write(qrtext)End FunctionShared Function MakeQR(ByVal qrtext As String, ByVal logo As Bitmap, Optional ByVal width As Integer = 800, Optional ByVal height As Integer = 800, Optional ByVal margin As Integer = 1) As Bitmap If logo Is Nothing Then Return MakeQR(qrtext, width, height, margin) End If Dim writer As New ZXing.MultiFormatWriter Dim hint As New Dictionary(Of ZXing.EncodeHintType, Object)() hint.Add(ZXing.EncodeHintType.CHARACTER_SET, "UTF-8") hint.Add(ZXing.EncodeHintType.MARGIN, margin) hint.Add(ZXing.EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H) ‘ 生成二维码 Dim bm As ZXing.Common.BitMatrix = writer.encode(qrtext, ZXing.BarcodeFormat.QR_CODE, width, height, hint) Dim barcodeWriter = New ZXing.BarcodeWriter() Dim bmp As Bitmap = barcodeWriter.Write(bm) ‘获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) Dim rectangle As Integer() = bm.getEnclosingRectangle() ‘计算插入图片的大小和位置 Dim middleW As Integer = Math.Min((rectangle(2) / 3.5), logo.Width) Dim middleH As Integer = Math.Min((rectangle(3) / 3.5), logo.Height) Dim middleL As Integer = (bmp.Width - middleW) / 2 Dim middleT As Integer = (bmp.Height - middleH) / 2 ‘将img转换成bmp格式,否则后面无法创建Graphics对象 Dim bmpimg As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format32bppArgb) Using g As Graphics = Graphics.FromImage(bmpimg) g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality g.DrawImage(bmp, 0, 0) End Using ‘将二维码插入图片 Using myGraphic As Graphics = Graphics.FromImage(bmpimg) ‘白底 myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH) myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH) End Using bmp.Dispose() Return bmpimgEnd FunctionShared Function ReadQR(ByVal bmp As Bitmap) As String Dim reader As New ZXing.BarcodeReader reader.Options.CharacterSet = "UTF-8" Dim ret As ZXing.Result = reader.Decode(bmp) If ret Is Nothing Then Return Nothing Else Return ret.Text End IfEnd Function
【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。