Unity3d创建二维码(利用zxing2.2)

首先 下载ZXing.Net.0.12.0.0.zip,下载地址为http://zxingnet.codeplex.com/

然后找到其中的unity文件夹,将文件夹放到Unity的工程内。

脚本:

using UnityEngine;
using System.Collections;
using ZXing;
using ZXing.QrCode;
public class BarcodeCam: MonoBehaviour 
{
void Start () 
    {
        encoded = new Texture2D(256, 256);
        Lastresult = "https://www.baidu.com";
}

    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);
    }

void Update () 
    {
        var textForEncoding = Lastresult;
        if (textForEncoding != null)
        {
            var color32 = Encode(textForEncoding, encoded.width, encoded.height);
            encoded.SetPixels32(color32);
            encoded.Apply();
        } 
}
    void OnGUI()
    {
        GUI.DrawTexture(new Rect(100, 100,256,256), encoded);
    }
    public Texture2D encoded;
    public string Lastresult;
}

将脚本挂在一个空物体上、运行、即可生成二维码

posted @   多见多闻  阅读(204)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示