You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.7 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using ZXing;
using ZXing.QrCode;
public class QRCode : MonoBehaviour {
public string QRCodeString;
[SerializeField]
private Texture2D QRResult;
const int QRSize = 256;
private Vector2 width_height = new Vector2(QRSize, QRSize);
[Serializable]
public class QRResultEvent : UnityEvent<Texture> { };
[SerializeField]
public QRResultEvent qrcodeEvent = new QRResultEvent();
List<Texture> results = new List<Texture>();
private void Update()
{
lock (results)
{
foreach (var r in results)
{
if (qrcodeEvent != null)
{
qrcodeEvent.Invoke(r);
}
}
results.Clear();
}
}
public void Encode()
{
Color32[] color32 = useEncode(QRCodeString, (int)width_height.x, (int)width_height.y);
if(QRResult == null)
QRResult = new Texture2D((int)width_height.x, (int)width_height.y);
QRResult.SetPixels32(color32);
QRResult.Apply();
if(qrcodeEvent != null)
qrcodeEvent.Invoke(QRResult);
}
private Color32[] useEncode(string textForEncoding, int width, int height)
{
Debug.Log("Encode QR code: " + textForEncoding);
BarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width
}
};
return writer.Write(textForEncoding);
}
}