码迷,mamicode.com
首页 > Web开发 > 详细

【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)

时间:2017-02-12 22:21:30      阅读:1259      评论:0      收藏:0      [点我收藏+]

标签:lex   read   reader   bit   大小   smo   宽度   img   math   

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 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

 

【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)

标签:lex   read   reader   bit   大小   smo   宽度   img   math   

原文地址:http://www.cnblogs.com/clso/p/6391941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!