实现在unity中将字符串信息内容转换为二维码图片显示,通过扫一扫功能即可识别二维码解析内容。
导入Unity二维码插件 (下载链接)
引入需要的命名空间
using ZXing;
using ZXing.QrCode;
具体实现方法
public RawImage QR; //存储二维码图片
public void creatQR()
{
//存放二维码
Texture2D encoded = new Texture2D(256, 256);
string key = string.Format("你的字符串信息内容");
//二维码写入图片
var color32 = Encode(key, encoded.width, encoded.height);
encoded.SetPixels32(color32);
encoded.Apply();
//生成的二维码图片附给RawImage
QR.texture = encoded;
}
private 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);
}