[VB. NET] uses ZXing. Net to generate two-dimensional code (custom LOGO supported), vb.netzxing.net

Source: Internet
Author: User

[VB. NET] uses ZXing. Net to generate two-dimensional code (custom LOGO supported), vb.netzxing.net

ZXing. NET project Homepage
Https://zxingnet.codeplex.com/

The code is basically copied from the following two articles XD
Http://www.cnblogs.com/tianma3798/p/5426869.html
Http://www.cnblogs.com/tianma3798/p/5426880.html


Parameter Optimization only, more practical and easier

 

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' is set to True to adjust the encoding opt. characterSet = "UTF-8" 'text encoding, recommended to UTF-8 opt. width = width' width opt. height = height 'height opt. margin = margin 'margin, which does not seem to be in pixel format, so it is not recommended to set a large 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) 'generate the QR code 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) 'Get the actual size of the QR code (remove the actual size after the blank space on both sides of the QR code) Dim rectangle As Integer () = bm. getEnclosingRectangle () 'calculates the size and position of the inserted image. 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' converts img to bmp format. Otherwise, the Graphics object Dim bmp img As New Bitmap (bmp. width, bmp. height, Imaging. pixelFormat. format32bppArgb) Using g As Graphics = Graphics. fromImage (bmp img) g. interpolationMode = Drawing2D. interpolationMode. highQualityBicubic g. smoothingMode = Drawing2D. smoothingMode. highQuality g. compositingQuality = Drawing2D. compositingQuality. highQuality g. drawImage (bmp, 0, 0) End Using Insert the QR code into the image Using myGraphic As Graphics = Graphics. fromImage (bmp img) 'White background myGraphic. fillRectangle (Brushes. white, middleL, middleT, middleW, middleH) myGraphic. drawImage (logo, middleL, middleT, middleW, middleH) End Using bmp. dispose () Return bmp imgend 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.