You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中使用ZXing.Net生成二维码的常见问题解决方法

Unity ZXing 二维码生成优化方案

三个问题可以通过调整编码参数+像素后处理一次性解决,不需要额外引入第三方库,具体实现如下:

1. 去除外围白色边距

ZXing生成二维码默认会预留4个单位的安静区(即外围白边),直接在QrCodeEncodingOptions配置中添加Margin = 0参数即可关闭默认边距。

印刷场景或二维码需要叠加在复杂背景上时,建议保留至少1单位的边距,避免扫码设备识别失败;纯Unity内UI展示可直接设为0。

2. 透明背景实现

ZXing默认输出的像素数组中,背景为纯白色Color32(255,255,255,255),遍历像素数组将白色像素的Alpha通道设为0即可实现透明背景,注意创建Texture2D时需要指定带Alpha通道的格式。

3. 自定义二维码模块颜色

默认二维码模块为纯黑色Color32(0,0,0,255),遍历像素数组时将所有黑色像素替换为目标自定义颜色即可,支持任意RGB色值。

完整可直接运行的修改后代码

using ZXing;
using ZXing.QrCode;
using UnityEngine;
using UnityEngine.UI;

public class QRCodeGenerator : MonoBehaviour
{
    // 预先绑定场景中的RawImage组件
    public RawImage targetRawImage;
    // 可在Inspector面板直接配置二维码模块颜色
    public Color qrModuleColor = Color.black;
    // 二维码生成尺寸,可根据需求调整
    private const int QR_TEXTURE_SIZE = 256;

    private static Color32[] EncodeQRCode(string content, int width, int height, Color moduleColor)
    {
        var barcodeWriter = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new QrCodeEncodingOptions
            {
                Height = height,
                Width = width,
                // 关闭默认外围白边
                Margin = 0,
                // 指定UTF-8编码避免中文乱码
                CharacterSet = "UTF-8"
            }
        };

        Color32[] pixelArray = barcodeWriter.Write(content);
        // 遍历像素做颜色替换与透明处理
        for (int i = 0; i < pixelArray.Length; i++)
        {
            // 识别原黑色二维码模块,替换为自定义颜色
            if (pixelArray[i].r == 0 && pixelArray[i].g == 0 && pixelArray[i].b == 0)
            {
                pixelArray[i] = new Color32(
                    (byte)(moduleColor.r * 255),
                    (byte)(moduleColor.g * 255),
                    (byte)(moduleColor.b * 255),
                    (byte)(moduleColor.a * 255)
                );
            }
            // 识别原白色背景,设置为全透明
            else
            {
                pixelArray[i] = new Color32(255, 255, 255, 0);
            }
        }
        return pixelArray;
    }

    public Texture2D GenerateQRTexture(string content)
    {
        // 必须指定RGBA32格式,否则Alpha通道不生效,透明背景会显示为白色
        Texture2D qrTexture = new Texture2D(QR_TEXTURE_SIZE, QR_TEXTURE_SIZE, TextureFormat.RGBA32, false);
        Color32[] processedPixels = EncodeQRCode(content, qrTexture.width, qrTexture.height, qrModuleColor);
        qrTexture.SetPixels32(processedPixels);
        qrTexture.Apply();
        return qrTexture;
    }

    private void Start()
    {
        // 调用示例
        targetRawImage.texture = GenerateQRTexture("https://test.link/123");
    }
}

注意事项

  • 如果需要保留少量边距,将Margin参数调整为1或2即可,边距区域会自动处理为透明
  • 自定义颜色支持带透明度的色值,可实现半透明二维码效果
  • 动态创建的Texture2D在不再使用时需要主动调用Destroy()释放,避免内存泄漏
  • 如果需要给二维码加logo、圆角等效果,可在Apply()之前对像素数组做二次处理

内容的提问来源于stack exchange,提问作者voids Mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 21:06:34