Search code examples
c#unity-game-engineqr-codezxing.net

How to fix QR code generation using ZXing.Net lib


It is necessary to generate a QR code from the text. I'm using the ZXing.unity.dll library

using ZXing;
using ZXing.QrCode;

Generating QR code

private static Color32[] Encode(string textForEncoding, int width, int height) {
    var writer = new BarcodeWriter {
        Format = BarcodeFormat.QR_CODE,

        Options = new QrCodeEncodingOptions{
            Height = height,
            Width = width
        }
    };
return writer.Write(textForEncoding);
}
public Texture2D generateQR(string text) {
    var encoded = new Texture2D (256, 256);
    var color32 = Encode(text, encoded.width, encoded.height);
    encoded.SetPixels32(color32);
    encoded.Apply();
    return encoded;
}

Applying the generated QR to the RawImage filled in the field

public RawImage RI;
...
RI.texture = generateQR("https://test.link/123");

The output is a picture with large white edges.

QR code preview pic

  • Q1 - How to remove white edges;
  • Q2 - How to make a transparent background;
  • Q3 - How to change black color to any other

Solution

  • Q3: Sample for changing foreground and background color:

    private static Color32[] Encode(string textForEncoding, int width, int height)
    {
        var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
    
            Options = new QrCodeEncodingOptions
            {
                Height = height,
                Width = width
            },
            Renderer = new Color32Renderer
            {
                Background = new Color32(255, 0, 0, 0),
                Foreground = new Color32(0, 255, 0, 0)
            }
        };
        return writer.Write(textForEncoding);
    }
    

    Q2: I' not absolutely sure, but the following sample should create a transparent background:

    private static Color32[] Encode(string textForEncoding, int width, int height)
    {
        var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
    
            Options = new QrCodeEncodingOptions
            {
                Height = height,
                Width = width
            },
            Renderer = new Color32Renderer
            {
                Background = new Color32(0, 0, 0, 255)
            }
        };
        return writer.Write(textForEncoding);
    }
    

    Q1: create the image as small as possible without a border and resize it manually

    private static Color32[] Encode(string textForEncoding, int width, int height)
    {
        var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
    
            Options = new QrCodeEncodingOptions
            {
                Height = width,
                Width = height,
                Margin = 0
            }
        };
        return writer.Write(textForEncoding);
    }
    public Texture2D generateQR(string text)
    {
        // create the image as small as possible without a white border by setting width an height to 1
        var color32 = Encode(text, 1, 1);
        var widthAndHeight = color32.Length / 2;
        var encoded = new Texture2D(widthAndHeight, widthAndHeight);
        encoded.SetPixels32(color32);
        encoded.Apply();
        //
        // Attention: insert code for resizing to the desired size here
        // I didn't try it myself. Not sure if it works.
        //
        encoded.Resize(256, 256);
        return encoded;
    }