Unity用ZXing插件生成二维码

现在ZXing插件并导入到Unity 中,在场景中加一个RawImage

1、使用ZXing生成二维码的第一种方法,新建脚本命名为QrCodeDraw并挂在RawImage上:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;

/// <summary>
/// 第一种生成二维码方法
/// </summary>
public class QrCodeDraw : MonoBehaviour
{

    private Texture2D enCodedTexture;
    private string qrCodePath;
    public RawImage rawIMg;
    // Start is called before the first frame update
    void Start()
    {
        enCodedTexture = new Texture2D(512, 512);
        qrCodePath = "https://www.baidu.com/";
        Encode(qrCodePath, 512, 512);
    }

    void Encode(string content, int width, int height)
    {
        MultiFormatWriter writer = new MultiFormatWriter();
        Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
        hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        //转换成texture2d
        int w = bitMatrix.Width;
        int h = bitMatrix.Height;
        Texture2D texture = new Texture2D(w, h);
        for (int x = 0; x < h; x++)
        {
            for (int y = 0; y < w; y++)
            {
                if (bitMatrix[x, y])
                {
                    texture.SetPixel(y, x, Color.black);
                }
                else
                {
                    texture.SetPixel(y, x, Color.white);
                }
            }
        }
        texture.Apply();
        rawIMg.texture = texture;
    }

}

 2、使用ZXing生成二维码的第二种方法,新建脚本命名为QrCodeDraw1并挂在RawImage上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.QrCode;

public class QrCodeDraw1 : MonoBehaviour
{
    private string qrCodePath = "https://cn.bing.com/?mkt=zh-CN";
    private Texture texture;
    public RawImage rawImg;
    // Start is called before the first frame update
    void Start()
    {
        texture= CreatQR_texture(qrCodePath);
        rawImg.texture = texture;
    }

    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 static Texture CreatQR_texture(string message)
    {
        Texture2D encoded;
        encoded = new Texture2D(256, 256);
        if (message.Length > 1)
        {
            //二维码写入图片    
            var color32 = Encode(message, encoded.width, encoded.height);
            encoded.SetPixels32(color32);
            encoded.Apply();
        }
        else
        {
            Debug.Log("生成二维码失败");
        }
        return encoded;
    }
}

猜你喜欢

转载自blog.csdn.net/meibianchuidi123/article/details/125030813