Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active September 16, 2021 01:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baobao/73be808d6eb257bba541699a47da6a04 to your computer and use it in GitHub Desktop.
Save baobao/73be808d6eb257bba541699a47da6a04 to your computer and use it in GitHub Desktop.
QR Code Creator EditorWindow
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
/// <summary>
/// QRコード作成EditorWindow
/// </summary>
public class CreateQRCodeWindow : EditorWindow
{
public enum QRImageSize
{
SIZE_128 = 7,
SIZE_256,
SIZE_512,
SIZE_1024,
SIZE_2048,
SIZE_4096
}
[MenuItem ("Window/QR")]
static void Init ()
{
var w = EditorWindow.GetWindow<CreateQRCodeWindow> ();
w.Show ();
}
QRImageSize _size = QRImageSize.SIZE_256;
string _content = "";
void OnGUI ()
{
string savePath = Application.dataPath + "/qr.png";
_content = GUILayout.TextArea (_content, GUILayout.Height (30f));
_size = (QRImageSize)EditorGUILayout.EnumPopup (_size);
EditorGUI.BeginDisabledGroup (string.IsNullOrEmpty (_content));
if (GUILayout.Button ("Save")) {
int size = (int)Mathf.Pow (2f, (int)_size);
var tex = QRCodeHelper.CreateQRCode (_content, size, size);
using (var fs = new FileStream (savePath, FileMode.OpenOrCreate)) {
var b = tex.EncodeToPNG ();
fs.Write (b, 0, b.Length);
}
AssetDatabase.Refresh ();
}
EditorGUI.EndDisabledGroup ();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZXing;
using ZXing.QrCode;
/// <summary>
/// QRコードの作成と読み込みヘルパークラス
/// </summary>
public class QRCodeHelper
{
static public string Read (Texture2D tex)
{
BarcodeReader reader = new BarcodeReader ();
int w = tex.width;
int h = tex.height;
var pixel32s = tex.GetPixels32 ();
var r = reader.Decode (pixel32s, w, h);
return r.Text;
}
static public string Read (WebCamTexture tex)
{
BarcodeReader reader = new BarcodeReader ();
int w = tex.width;
int h = tex.height;
var pixel32s = tex.GetPixels32 ();
var r = reader.Decode (pixel32s, w, h);
if (r != null)
return r.Text;
else
return "error";
}
static public Texture2D CreateQRCode (string str, int w, int h)
{
var tex = new Texture2D (w, h, TextureFormat.ARGB32, false);
var content = Write (str, w, h);
tex.SetPixels32 (content);
tex.Apply ();
return tex;
}
static Color32[] Write (string content, int w, int h)
{
Debug.Log (content + " / " + w + " / " + h);
var writer = new BarcodeWriter {
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions {
Width = w, Height = h
}
};
return writer.Write (content);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SampleQRReader : MonoBehaviour
{
string _result = null;
WebCamTexture _webCam;
bool _isInit;
IEnumerator Start ()
{
yield return Application.RequestUserAuthorization (UserAuthorization.WebCam);
if (Application.HasUserAuthorization (UserAuthorization.WebCam) == false) {
yield break;
}
WebCamDevice[] devices = WebCamTexture.devices;
if (devices == null || devices.Length == 0)
yield break;
_webCam = new WebCamTexture (devices [0].name, Screen.width, Screen.height, 12);
_webCam.Play ();
}
void Update ()
{
if (_webCam != null) {
_result = QRCodeHelper.Read (_webCam);
}
CheckDebugMode ();
}
void OnGUI ()
{
isDebug = GUILayout.Toggle (isDebug, "DEBUGモード", GUILayout.Height (50f));
GUILayout.Label (_result, GUILayout.Width (300f), GUILayout.Height (200f));
}
#region Debug
public bool isDebug;
bool _isDebugMode;
void CheckDebugMode ()
{
if (_isDebugMode != isDebug) {
_isDebugMode = isDebug;
if (_isDebugMode)
ShowDebugView ();
else
HideDebugView ();
}
}
/// <summary>
/// 画面右上にWebCamを表示させる
/// </summary>
void ShowDebugView ()
{
var debugCanvasObj = new GameObject ("DebugQR");
var debugRawObj = new GameObject ("Raw");
debugRawObj.transform.SetParent (debugCanvasObj.transform, false);
var rawImage = debugRawObj.AddComponent<RawImage> ();
var c = debugCanvasObj.AddComponent<Canvas> ();
c.renderMode = RenderMode.ScreenSpaceOverlay;
rawImage.texture = _webCam;
float w = 200f;
float h = w * Screen.width / Screen.height;
rawImage.rectTransform.sizeDelta = new Vector2 (w, h);
rawImage.rectTransform.anchorMax = new Vector2 (1f, 1f);
rawImage.rectTransform.anchorMin = new Vector2 (1f, 1f);
rawImage.rectTransform.anchoredPosition = new Vector2 (-w * 0.5f, -h * 0.5f);
}
void HideDebugView ()
{
Destroy (GameObject.Find ("DebugQR"));
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment