LoginSignup
12
17

More than 5 years have passed since last update.

UnityでQRコード読み書き(iOS動作)

Posted at

概要

Unityのプロジェクトで、QRコード使いたいなとなったので、QRコードの生成と読み取りをしてみた

環境

名称 バージョン メモ
Unity 5.4.2f2

参考にしたもの

http://www.slideshare.net/hirotoimoto1/spajamresult
QRコードの読み、生成のコードを参考にしました。

導入手順

 1:QRコードライブラリの導入

https://zxingnet.codeplex.com/
からライブラリをダウンロード。
ライセンスは、Apache License 2.0 (Apache)

2:QRコードライブラリをUnityプロジェクトに追加

ダウンロードしたファイルから

  • zxing.unity.dll
  • zxing.unity.pdb
  • zxing.unity.xml

をプロジェクトに追加

3:管理クラス作成

下記のようなクラスを作成

PQRCodeManager.cs
using ZXing;
using ZXing.QrCode;
using UnityEngine;

public class PQRCodeManager {


        /// <summary>
        /// QRコード読み取り)(返り文字列が-1の場合は、読み込めていない)
        /// </summary>
        /// <param name="cameraTexture">Camera texture.</param>
        public string read(WebCamTexture cameraTexture){
            BarcodeReader reader = new BarcodeReader ();
            Color32[] color = cameraTexture.GetPixels32 ();
            int width = cameraTexture.width;
            int height = cameraTexture.height;
            Result result = reader.Decode (color, width, height);
            if (result != null){
                return result.Text;
            }
            return "-1";//エラー時
        }


        /// <summary>
        /// QRコード作成
        /// </summary>
        /// <param name="inputString">QRコード生成元の文字列</param>
        /// <param name="textture">QRの画像がここに入る</param>
        public void create(string inputString,Texture2D textture){
            var qrCodeColors = Write (inputString, textture.width, textture.height);
            textture.SetPixels32 (qrCodeColors);
            textture.Apply ();
        }


        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);
        }
    }

4:読み出し元クラスの作成

Sample.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Sample : MonoBehaviour {

    public int Width = 960;
    public int Height = 540;
    public int FPS = 30;


    //CanvasのUIに紐付けてください。///////////
    public RawImage qrCodeImage;//生成したQRコード
    public Text resultText;//読み取り結果

    private WebCamTexture webcamTexture;
    private  PQRCodeManager qrManager;



    // Use this for initialization
    void Start () {

        //カメラ準備
        WebCamDevice[] devices = WebCamTexture.devices;
        webcamTexture = new WebCamTexture(devices[0].name, Width, Height, FPS );
        GetComponent<Renderer> ().material.mainTexture = webcamTexture;
        webcamTexture.Play();



        //QRコード管理クラス
        this.qrManager = new PQRCodeManager ();

    }

    // Update is called once per frame
    void Update () {
        //カメラから読み取り
        resultText.text = this.qrManager.read (webcamTexture);
    }


    /// <summary>
    /// QRコード生成
    /// </summary>
    public void createQRCode(){
        Debug.Log ("createQRCode");
        string inputText = "http://test.ppen.info";


        Texture2D texture = new Texture2D (256, 256);
        qrCodeImage.texture = texture;
        this.qrManager.create (inputText,(Texture2D)qrCodeImage.texture);
    }
}

以上。

12
17
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
17