Unity 扫描 二维码
生活随笔
收集整理的這篇文章主要介紹了
Unity 扫描 二维码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?Unity掃描二維碼有2中有以下兩種實現方式:
???1.使用原生開發,然后Unity里調用
???2.使用Unity開發,利用zxing.net解碼
?比較2種方式,1的開發難度較高,需要相關android和ios開發的知識才能實現界面定制,所以方法2會比較適用,界面定制簡單,也不用復雜去開發原生插件。下面來說下第二種開發怎么做。
?首先需要一個下載一個zxing.net庫,大家可以去官網下載,地址:點擊打開鏈接。
? 原理就是使用WebCamTexutre調用攝像頭,將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默認開發分辨率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); //自適應掃描框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;}}}} } ?? ?總結
以上是生活随笔為你收集整理的Unity 扫描 二维码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hive操作随笔
- 下一篇: python桌面应用_用Python写个