Unity二维码生成

发表于2017-08-31
评论2 1.1k浏览
有的时候需要进行二维码扫描,但是二维码是怎么在Unity中进行生成呢?下面就给大家介绍下Unity中二维码的生成方法。

这里我就直接上代码了:
/// 
/// write by 52cwalk,if you have some question ,please contract lycwalk@gmail.com  
///   
///   
using UnityEngine;  
using System.Collections;  
using System.Collections.Generic;  
using ZXing;  
using ZXing.QrCode;  
using ZXing.QrCode.Internal;  
using ZXing.Common;  
public class QRCodeEncodeController : MonoBehaviour {  
    private Texture2D m_EncodedTex;  
    public int e_QRCodeWidth;  
    public int e_QRCodeHeight;  
    public delegate void QREncodeFinished(Texture2D tex);    
    public event QREncodeFinished e_QREncodeFinished;    //定義事件  
    BitMatrix byteMatrix;  
    void Start ()  
    {  
        int targetWidth = Mathf.Min(e_QRCodeWidth,e_QRCodeHeight);  
        targetWidth = Mathf.Clamp (targetWidth, 128, 1024);  
        e_QRCodeWidth = e_QRCodeHeight = targetWidth;  
        m_EncodedTex = new Texture2D(e_QRCodeWidth, e_QRCodeHeight);  
    }  
    void Update ()  
    {  
    }  
    /// 
    /// Encode the specified string .  
    ///   
    ///  content string.  
    public void Encode(string valueStr)  
    {  
    //  var writer = new QRCodeWriter();  
        var writer = new MultiFormatWriter();  
        Dictionary<encodehinttype, object</encodehinttype, > hints = new Dictionary<encodehinttype, < span="">object>();    </encodehinttype, <>
        //set the code type  
        hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");    
        byteMatrix = writer.encode( valueStr, BarcodeFormat.QR_CODE, e_QRCodeWidth, e_QRCodeHeight,hints);   
        //writer1.encode("ddd",BarcodeFormat.  
        for (int i =0; i!= e_QRCodeWidth; i ) {  
            for(int j = 0;j!= e_QRCodeHeight;j )  
            {  
                if(byteMatrix[i,j])  
                {  
                    m_EncodedTex.SetPixel(i,j,Color.black);  
                }  
                else  
                {  
                    m_EncodedTex.SetPixel(i,j,Color.white);  
                }  
            }  
        }  
        ///rotation the image   
        Color32[] pixels = m_EncodedTex.GetPixels32();  
        pixels = RotateMatrixByClockwise(pixels, m_EncodedTex.width);  
        m_EncodedTex.SetPixels32(pixels);   
        m_EncodedTex.Apply ();  
        e_QREncodeFinished (m_EncodedTex);   //編譯完成觸發事件,在其他類中添加觸發事件調用的方法  
    }  
    /// 
    /// Rotates the matrix.Clockwise  
    ///   
    /// The matrix.  
    /// Matrix.  
    /// N.  
    static Color32[] RotateMatrixByClockwise(Color32[] matrix, int n) {  
        Color32[] ret = new Color32[n * n];  
        for (int i = 0; i < n;  i) {  
            for (int j = 0; j < n;  j) {  
                ret[i*n   j] = matrix[(n - i - 1) * n   j];  
            }  
        }  
        return ret;  
    }  
    /// 
    /// anticlockwise  
    ///   
    /// The matrix.  
    /// Matrix.  
    /// N.  
    static Color32[] RotateMatrixByAnticlockwise(Color32[] matrix, int n) {  
        Color32[] ret = new Color32[n * n];  
        for (int i = 0; i < n;  i) {  
            for (int j = 0; j < n;  j) {  
                ret[i*n   j] = matrix[(n - j - 1) * n   i];  
            }  
        }  
        return ret;  
    }  
  

如:
/// 
/// write by 52cwalk,if you have some question ,please contract lycwalk@gmail.com  
///   
///   
///   
using UnityEngine;  
using System.Collections;  
using UnityEngine.UI;  
public class QREncodeTest : MonoBehaviour {  
    public QRCodeEncodeController e_qrController;  
    public RawImage qrCodeImage;  
    public InputField m_inputfield;  
    // Use this for initialization  
    void Start () {  
        if (e_qrController != null) {  
            e_qrController.e_QREncodeFinished  = qrEncodeFinished;  
        }  
    }  
    void qrEncodeFinished(Texture2D tex)     //觸發事件調用此方法  
    {  
        if (tex != null && tex != null) {  
            qrCodeImage.texture = tex;  
        } else {  
        }  
    }  
    public void Encode()           
    {  
        if (e_qrController != null) {  
            string valueStr = m_inputfield.text;  
            e_qrController.Encode(valueStr);        //傳入需要編譯成二維碼的string字段  
        }  
    }  
    public void ClearCode()  
    {  
        qrCodeImage.texture = null;  
        m_inputfield.text = "";  
    }  
}  

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论