Unity 扫描 二维码
生活随笔
收集整理的這篇文章主要介紹了
Unity 扫描 二维码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?Unity掃描二維碼有2中有以下兩種實(shí)現(xiàn)方式:
???1.使用原生開發(fā),然后Unity里調(diào)用
???2.使用Unity開發(fā),利用zxing.net解碼
?比較2種方式,1的開發(fā)難度較高,需要相關(guān)android和ios開發(fā)的知識(shí)才能實(shí)現(xiàn)界面定制,所以方法2會(huì)比較適用,界面定制簡單,也不用復(fù)雜去開發(fā)原生插件。下面來說下第二種開發(fā)怎么做。
?首先需要一個(gè)下載一個(gè)zxing.net庫,大家可以去官網(wǎng)下載,地址:點(diǎn)擊打開鏈接。
? 原理就是使用WebCamTexutre調(diào)用攝像頭,將WebCamTexutre賦到一張UI rawimage上面,每一幀讀取,給zxing解碼?
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using ZXing;namespace miwu {public class QRScaner : MonoBehaviour{public delegate void OnDecodSuccess(string data);public OnDecodSuccess OnDecodSuccessHandler;public int BlockWidth = 350;public Vector2 UIResolution = new Vector3(1334f, 750f); //UI默認(rèn)開發(fā)分辨率private Vector2 rectTop;private WebCamTexture webCamTexture;private bool Decoding = false;BarcodeReader mBarcodeReader = new BarcodeReader();private Texture2D decodeTex;private void Start(){BlockWidth =(int)( BlockWidth / UIResolution.y * Screen.height); //自適應(yīng)掃描框rectTop = new Vector2((Screen.width - BlockWidth) / 2, (Screen.height - BlockWidth) / 2);webCamTexture = new WebCamTexture(Screen.width, Screen.height, 60);this.GetComponent<RawImage>().texture = webCamTexture;StartScanQRCode();}/// <summary>/// 開始掃描/// </summary>public void StartScanQRCode(){Decoding = true;webCamTexture.Play();StartCoroutine("DecodingQRCode");}/// <summary>/// 停止掃描/// </summary>public void StopScanQRCode(){Decoding = false;StopCoroutine("DecodingQRCode");webCamTexture.Stop();}/// <summary>/// 重新開始解碼/// </summary>public void ReDecode(){Decoding = true;StartCoroutine("DecodingQRCode");}/// <summary>/// 是否解碼中/// </summary>/// <returns></returns>public bool isDecoding(){return Decoding;}IEnumerator DecodingQRCode(){while (Decoding){yield return new WaitForEndOfFrame();decodeTex = new Texture2D(BlockWidth, BlockWidth, TextureFormat.ARGB32, true);decodeTex.ReadPixels(new Rect(rectTop.x, rectTop.y, BlockWidth, BlockWidth), 0, 0, false);//byte[] bytes = decodeTex.EncodeToPNG();//System.IO.File.WriteAllBytes("test.png", bytes);//Decoding = false;//yield break;var data = mBarcodeReader.Decode(decodeTex.GetPixels32(), decodeTex.width, decodeTex.height);if (data != null){OnDecodSuccessHandler(data.Text);Decoding = false;yield break;}}}} } ?? ?總結(jié)
以上是生活随笔為你收集整理的Unity 扫描 二维码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hive操作随笔
- 下一篇: manjaro pacman 使用方法总