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;
}
}
【Unity】如何扫描二维码?
最新推荐文章于 2024-09-20 21:22:50 发布