【Unity】如何扫描二维码?

using UnityEditor;
using UnityEngine;
using ZXing;//要将这个dll放入Plugins文件夹

public class Scan : MonoBehaviour {

    public static string ScanQRCode(Texture2D texture)
    {     
        //将Texture设置为可读(默认是不可读的,且必须手动修改Texture import settings,这里提供了一种代码控制的方法)
        string path = AssetDatabase.GetAssetPath(texture);  
        TextureImporter import = AssetImporter.GetAtPath(path) as TextureImporter;
        import.isReadable = true;
        AssetDatabase.ImportAsset(path);

        //解码
        BarcodeReader barcodeReader = new BarcodeReader();
        Color32[] data = texture.GetPixels32();
        var br = barcodeReader.Decode(data, texture.width, texture.height);
        string txt;
        if (br != null)
        {
            txt = br.Text;//我在这个二维码中放了一个字符串
            print(txt);
        }
        else
        {
            txt = "null";
            print("二维码代表文字为空");
        }
        return txt;
    }     
}

猜你喜欢

转载自blog.csdn.net/qq_36622009/article/details/80837069