Young87

SmartCat's Blog

So happy to code my life!

游戏开发交流QQ群号60398951

当前位置:首页 >跨站数据测试

Unity 3D : 從 QR-Code 提取字串

前言 :

這個範例使用 ZXing.NET 來做 QR-Code 掃瞄並判斷裡面的字串,最棒的是 ZXing .NET 官方是直接支持 Unity 的 !

你可以在這個網站產生 QR-Code : https://www.ifreesite.com/qrcode/

※ 使用 UTF-8 與 L 校正

第一步 :

下載 ZXing.NET : https://github.com/micjahn/ZXing.Net

第二步 :

複製 zxing.unity.dll 到專案中,dll 檔如下圖路徑 :

这里写图片描述

第三步 :

將下面程式碼加入到專案中

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

public class QRCodeTest : MonoBehaviour
{
    public RawImage cameraTexture; // 即時預覽相機畫面
    public Text txtQRcode; // 掃完 QR-CODE 後,顯示裡面的字串
    private WebCamTexture webCameraTexture;
    private BarcodeReader barcodeReader;

    IEnumerator Start()
    {
        barcodeReader = new BarcodeReader();
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            string devicename = devices[0].name;
            webCameraTexture = new WebCamTexture(devicename, 800, 600);
            cameraTexture.texture = webCameraTexture;
            webCameraTexture.Play();

            txtQRcode.text = "相機解析度 : " + webCameraTexture.width + "x" + webCameraTexture.height;

            InvokeRepeating("DecodeQR", 0, 0.5f); // 0.5 秒掃描一次
        }
    }

    private void DecodeQR()
    {
        var br = barcodeReader.Decode(webCameraTexture.GetPixels32(), webCameraTexture.width, webCameraTexture.height);
        if (br != null)
        {
            txtQRcode.text = br.Text;
            webCameraTexture.Stop();
        }
    }
}

第四步 :

在 Unity 中建立 RawImage 與 Text ,並套用至腳本中

第五步 :

執行吧 !

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: 做小程序的心得体会

下一篇: 为什么BAT干不掉海康威视?——关于人工智能的思考

精华推荐