unity二維碼生成(新)

                                           二維碼的生成

     關於二維碼的生成細節和原理,可以參考: http://blog.csdn.net/zy799894671/article/details/19983041#
可以參考我的上一篇博文來學習二維碼的製作;
這裏我就直接上代碼了:
/// <summary>
/// write by 52cwalk,if you have some question ,please contract lycwalk@gmail.com
/// </summary>
/// 


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 ()
	{

	}

	/// <summary>
	/// Encode the specified string .
	/// </summary>
	/// <param name="valueStr"> content string.</param>
	public void Encode(string valueStr)
	{
	//	var writer = new QRCodeWriter();
		var writer = new MultiFormatWriter();
		Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();  
		//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);   //編譯完成觸發事件,在其他類中添加觸發事件調用的方法
	}

	/// <summary>
	/// Rotates the matrix.Clockwise
	/// </summary>
	/// <returns>The matrix.</returns>
	/// <param name="matrix">Matrix.</param>
	/// <param name="n">N.</param>
	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;
	}

	/// <summary>
	/// anticlockwise
	/// </summary>
	/// <returns>The matrix.</returns>
	/// <param name="matrix">Matrix.</param>
	/// <param name="n">N.</param>
	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;
	}


}
如:
/// <summary>
/// write by 52cwalk,if you have some question ,please contract lycwalk@gmail.com
/// </summary>
/// 
/// 

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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值