【问题标题】:How i can generate QR code with logo on Unity3D?如何在 Unity3D 上生成带有徽标的 QR 码?
【发布时间】:2020-01-30 09:23:01
【问题描述】:

我想生成带有这样标志的二维码:

环境:Unity3D C#

我尝试通过互联网上的示例编写代码。

private Texture2D GenerateBarcode(string data, BarcodeFormat format, int width, int height)
{
        var encodeOptions = new EncodingOptions
        {
            Height = height,
            Width = width,
            Margin = 0,
            PureBarcode = false
        };
        encodeOptions.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

        BarcodeWriter writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = encodeOptions
        };

        Color32[] pixels = writer.Write(text);
        Texture2D tex = new Texture2D(width, height);
        tex.SetPixels32(pixels);
        tex.Apply();
        return tex;
}

Q1:如何将 QR 码的填充颜色从黑色更改为绿色?

Q2:如何将二维码两侧的“黑色方块”更改为另一个类似的图形?

请帮忙。我不知道我需要做什么。

A1:derHugo

的回答

    for (var i = 0; i < pixels.Length; i++){
          var pixel = pixels[i];
                if (pixel.r == 0 && pixel.g == 0 && pixel.b == 0)
                {
                    pixel = Color.green;
                }
                else {
                    pixel = Color.white;
                }
                pixels[i] = pixel;
    }

【问题讨论】:

    标签: unity3d qr-code zxing generate


    【解决方案1】:

    问题一:简单地遍历像素并将黑色的像素染成绿色,而不是像

    Color32[] pixels = writer.Write(text);
    
    for(var i=0; i<pixels.Length; i++)
    {
        var pixel = pixels[i];
        if(pixel.r == 0 && pixel.g == 0 && pixel.b == 0) continue;
    
        pixel = Color.black;
        pixels[i] = pixel;
    }
    
    Texture2D tex = new Texture2D(width, height);
    tex.SetPixels32(pixels);
    tex.Apply();
    

    对于问题 2:有很多类似的问题,例如Add custom image or text to QR code generated by ZXing.Netthis post 至少对于中间的 Logo。边缘的原理基本相同:用自定义图像覆盖像素。

    【讨论】:

    • 感谢您的回答。我对二维码有所了解。在 QR 码生成中具有“QR 码纠错级别”,如果我们只看到 70%,它有助于读取 QR 码。意思是,我可以将任何图片 30% 放在 QR 码上。
    猜你喜欢
    • 2015-05-02
    • 1970-01-01
    • 2018-01-13
    • 2016-05-08
    • 1970-01-01
    • 2016-05-23
    • 2012-10-26
    • 2023-01-05
    • 2019-05-25
    相关资源
    最近更新 更多