none
Need help with QR Code library for .NET. RRS feed

  • Question


  • I have string codes and need to get QR Codes from them.

    It's important to have a logo in the middle of the QR Code, like this example :


    I must use it in a VB 2015 Windows Forms Application.

    Could you suggest a library or code ?

    Thank You.




    • Edited by MarcoGG Tuesday, September 10, 2019 7:50 AM
    Tuesday, September 10, 2019 7:41 AM

Answers

  • Hi,

    You can use Nuget package management, Project - > Manage NuGet Packages - > Browse - > zxing. Then download ZXing.Net as shown below.

    Note: ZXing is an open source Java class library for parsing 1D/2D barcodes in various formats. The goal is to decode QR codes, Data Matrix and UPC 1D barcodes. It provides clients under a variety of platforms, including J2ME, J2SE and Android. There is also a corresponding. Net version.

    You can try my code as follows.

    Imports ZXing
    Imports ZXing.QrCode.Internal
    Imports System.Drawing.Imaging
    Imports ZXing.Common
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Generate("https://www.google.com/")
            Generate("It's important to have a logo in the middle of the QR Code, like this example")
        End Sub
        Private Shared Sub Generate(ByVal text As String)
            Dim logo As Bitmap = New Bitmap("D:\panda.png")
            Dim writer As MultiFormatWriter = New MultiFormatWriter()
            Dim hint As Dictionary(Of EncodeHintType, Object) = New Dictionary(Of EncodeHintType, Object)()
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8")
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H)
            Dim bm As BitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, hint)
            Dim barcodeWriter As BarcodeWriter = New BarcodeWriter()
            Dim map As Bitmap = barcodeWriter.Write(bm)
            Dim rectangle As Integer() = bm.getEnclosingRectangle()
            Dim middleW As Integer = Math.Min(CInt((rectangle(2) / 3.5)), logo.Width)
            Dim middleH As Integer = Math.Min(CInt((rectangle(3) / 3.5)), logo.Height)
            Dim middleL As Integer = (map.Width - middleW) / 2
            Dim middleT As Integer = (map.Height - middleH) / 2
            Dim bmpimg As Bitmap = New Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb)
    
            Using g As Graphics = 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)
            End Using
    
            Dim myGraphic As Graphics = Graphics.FromImage(bmpimg)
            myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH)
            myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH)
            bmpimg.Save("D:\QrCode.png", ImageFormat.Png)
            MsgBox("Successfully generate!")
        End Sub
    End Class
    

    Also,you can put links in two-dimensional codes, such as "https://www.google.com/".

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MarcoGG Thursday, September 12, 2019 2:00 PM
    Tuesday, September 10, 2019 9:06 AM

All replies

  • Hi,

    You can use Nuget package management, Project - > Manage NuGet Packages - > Browse - > zxing. Then download ZXing.Net as shown below.

    Note: ZXing is an open source Java class library for parsing 1D/2D barcodes in various formats. The goal is to decode QR codes, Data Matrix and UPC 1D barcodes. It provides clients under a variety of platforms, including J2ME, J2SE and Android. There is also a corresponding. Net version.

    You can try my code as follows.

    Imports ZXing
    Imports ZXing.QrCode.Internal
    Imports System.Drawing.Imaging
    Imports ZXing.Common
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Generate("https://www.google.com/")
            Generate("It's important to have a logo in the middle of the QR Code, like this example")
        End Sub
        Private Shared Sub Generate(ByVal text As String)
            Dim logo As Bitmap = New Bitmap("D:\panda.png")
            Dim writer As MultiFormatWriter = New MultiFormatWriter()
            Dim hint As Dictionary(Of EncodeHintType, Object) = New Dictionary(Of EncodeHintType, Object)()
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8")
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H)
            Dim bm As BitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, hint)
            Dim barcodeWriter As BarcodeWriter = New BarcodeWriter()
            Dim map As Bitmap = barcodeWriter.Write(bm)
            Dim rectangle As Integer() = bm.getEnclosingRectangle()
            Dim middleW As Integer = Math.Min(CInt((rectangle(2) / 3.5)), logo.Width)
            Dim middleH As Integer = Math.Min(CInt((rectangle(3) / 3.5)), logo.Height)
            Dim middleL As Integer = (map.Width - middleW) / 2
            Dim middleT As Integer = (map.Height - middleH) / 2
            Dim bmpimg As Bitmap = New Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb)
    
            Using g As Graphics = 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)
            End Using
    
            Dim myGraphic As Graphics = Graphics.FromImage(bmpimg)
            myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH)
            myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH)
            bmpimg.Save("D:\QrCode.png", ImageFormat.Png)
            MsgBox("Successfully generate!")
        End Sub
    End Class
    

    Also,you can put links in two-dimensional codes, such as "https://www.google.com/".

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by MarcoGG Thursday, September 12, 2019 2:00 PM
    Tuesday, September 10, 2019 9:06 AM
  • Hi,

    first of all, thank you for answer and useful code example.

    In your example I see two steps : first creation of the code image, and then the logo is drawn on the existing QR barcode bitmap.

    My question was a little different : is there a library that can embed the logo inside the QR, so I can be sure that QR will always be readable ?

    I'm afraid my logo won't work on every code I print, and the QR won't be readable.

    Or this is the only way ?

    Thanks again.

    Tuesday, September 10, 2019 2:13 PM
  • Hi,

    Please don't worry about it.

    QR code has fault-tolerant function, and the fault-tolerant rate is very high.

    The missing part or the occluded part can also be scanned correctly. This is due to the "fault tolerance" design of QR code when it was invented. The generator will repeat some information (that is, redundancy) to improve its fault tolerance.

    QR codes can be generated with four degrees of fault tolerance (correctable codes), namely L, M, Q, H, corresponding to 7%, 15%, 25%, 30% of the fault tolerance. That is to say, if you choose H-file fault tolerance when generating two-dimensional codes, even if 30% of the patterns are occluded, they can be scanned correctly. This is why LOGO can be added to many two-dimensional codes.

    Note that you should not obscure the "回" pattern on the three corners of the QR code pattern, which is position detection patterns to help decoding software locate. There are also several small "回" in larger QR codes, which are position aligners, similar to the use of detectors. With these homographs, you can scan the two-dimensional code information accurately from any angle. QR code has the following four error correction levels:
    ErrorCorrectionLevel.L: About <=7% of errors can be corrected
    ErrorCorrectionLevel.M: About <=15% of errors can be corrected
    ErrorCorrectionLevel.Q: About <=25% of errors can be corrected
    ErrorCorrectionLevel.H: About <=30% of errors can be corrected

    hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H)

    Please note that your logo is not too big. When inserting it, adjust the position, usually in the middle of the QR code.If so, the QR codes will always be readable..

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 12, 2019 5:05 AM
  • Hi, you can probably try this barcode api. It provides a property to embed a logo in a QR code image. You should not worry about wethere the generated QR code can be read or not.

    using Spire.Barcode;
    using System.Drawing;
    
    namespace GenerateQRCode
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                BarcodeSettings barCodeSetting = new BarcodeSettings();
                barCodeSetting.Type = BarCodeType.QRCode;
                barCodeSetting.BackColor = Color.WhiteSmoke;
                barCodeSetting.QRCodeDataMode = QRCodeDataMode.Byte;
                barCodeSetting.QRCodeECL = QRCodeECL.M;
                barCodeSetting.ShowText = false;
                barCodeSetting.BorderWidth = 50;
                barCodeSetting.BarHeight = 20;
                barCodeSetting.X = 5;
                string data = "ABC 123456789";
                barCodeSetting.Data = data;//qr code data
                barCodeSetting.Data2D = data;//display text
                barCodeSetting.QRCodeLogoImage = Image.FromFile("logo.jpg");//embed logo in qr code
                BarCodeGenerator generator = new BarCodeGenerator(barCodeSetting);
                Image barcode = generator.GenerateImage(); ;
                barcode.Save("QR.png",System.Drawing.Imaging.ImageFormat.Png);
            }
        }
    }


    Thursday, September 12, 2019 8:52 AM
  • Hi,

    Please don't worry about it.

    QR code has fault-tolerant function, and the fault-tolerant rate is very high.

    The missing part or the occluded part can also be scanned correctly. This is due to the "fault tolerance" design of QR code when it was invented. The generator will repeat some information (that is, redundancy) to improve its fault tolerance.

    QR codes can be generated with four degrees of fault tolerance (correctable codes), namely L, M, Q, H, corresponding to 7%, 15%, 25%, 30% of the fault tolerance. That is to say, if you choose H-file fault tolerance when generating two-dimensional codes, even if 30% of the patterns are occluded, they can be scanned correctly. This is why LOGO can be added to many two-dimensional codes.

    Note that you should not obscure the "回" pattern on the three corners of the QR code pattern, which is position detection patterns to help decoding software locate. There are also several small "回" in larger QR codes, which are position aligners, similar to the use of detectors. With these homographs, you can scan the two-dimensional code information accurately from any angle. QR code has the following four error correction levels:
    ErrorCorrectionLevel.L: About <=7% of errors can be corrected
    ErrorCorrectionLevel.M: About <=15% of errors can be corrected
    ErrorCorrectionLevel.Q: About <=25% of errors can be corrected
    ErrorCorrectionLevel.H: About <=30% of errors can be corrected

    hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H)

    Please note that your logo is not too big. When inserting it, adjust the position, usually in the middle of the QR code.If so, the QR codes will always be readable..

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thank You. In fact now I've done many tests on it and it's always readable.

    I will return here for further questions if needed.

    by now I mark your answer as solution. Good work.


    Thursday, September 12, 2019 1:58 PM
  • Hi, you can probably try this barcode api. It provides a property to embed a logo in a QR code image. You should not worry about wethere the generated QR code can be read or not.

    using Spire.Barcode;
    using System.Drawing;
    
    namespace GenerateQRCode
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                BarcodeSettings barCodeSetting = new BarcodeSettings();
                barCodeSetting.Type = BarCodeType.QRCode;
                barCodeSetting.BackColor = Color.WhiteSmoke;
                barCodeSetting.QRCodeDataMode = QRCodeDataMode.Byte;
                barCodeSetting.QRCodeECL = QRCodeECL.M;
                barCodeSetting.ShowText = false;
                barCodeSetting.BorderWidth = 50;
                barCodeSetting.BarHeight = 20;
                barCodeSetting.X = 5;
                string data = "ABC 123456789";
                barCodeSetting.Data = data;//qr code data
                barCodeSetting.Data2D = data;//display text
                barCodeSetting.QRCodeLogoImage = Image.FromFile("logo.jpg");//embed logo in qr code
                BarCodeGenerator generator = new BarCodeGenerator(barCodeSetting);
                Image barcode = generator.GenerateImage(); ;
                barcode.Save("QR.png",System.Drawing.Imaging.ImageFormat.Png);
            }
        }
    }


    Thank You !

    I'll try this as soon as possible.

    Thursday, September 12, 2019 1:59 PM