53 lines
1.0 KiB
C#
53 lines
1.0 KiB
C#
using ZXing;
|
|
using ZXing.QrCode;
|
|
using UnityEngine;
|
|
|
|
public class QRGen : MonoBehaviour {
|
|
|
|
private static QRGen _instance;
|
|
|
|
public static QRGen Instance {
|
|
get {
|
|
if (_instance == null) {
|
|
_instance = new QRGen ();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
|
|
public Texture2D generateQR(string text)
|
|
{
|
|
var encoded = new Texture2D(256, 256);
|
|
var color32 = Encode(text, encoded.width, encoded.height);
|
|
encoded.SetPixels32(color32);
|
|
encoded.Apply();
|
|
return encoded;
|
|
}
|
|
private static Color32[] Encode(string textForEncoding, int width, int height)
|
|
{
|
|
var writer = new BarcodeWriter
|
|
{
|
|
Format = BarcodeFormat.QR_CODE,
|
|
Options = new QrCodeEncodingOptions
|
|
{
|
|
Height = height,
|
|
Width = width,
|
|
Margin = 1
|
|
}
|
|
};
|
|
return writer.Write(textForEncoding);
|
|
}
|
|
|
|
private Color32[] Write(string content,int width,int height){
|
|
var writer = new BarcodeWriter {
|
|
Format = BarcodeFormat.QR_CODE,
|
|
Options = new QrCodeEncodingOptions {
|
|
Height = height,
|
|
Width = width
|
|
}
|
|
};
|
|
return writer.Write (content);
|
|
}
|
|
}
|