【VB.NET】利用 ZXing.Net 生成二维码(支持自定…
2018-06-23 23:48:01来源:未知 阅读 ()
ZXing .NET 的项目主页
https://github.com/micjahn/ZXing.Net
代码基本上抄袭自下面两篇文章 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 Function Shared 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 bmpimg End Function Shared 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 If End Function
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:mvc 实现超时弹窗后跳转
- 利用css 实现 视觉差效果 2020-04-09
- 利用vertical-align属性实现分隔符 2020-04-03
- CSS3如何利用粒子旋转伸缩加载动画 2020-03-13
- 如何利用 githob 上传自己的网站 2020-03-01
- 如何利用border书写三角形,建议考虑正方形 2020-02-21
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash