日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity 扫描 二维码

發布時間:2023/12/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 扫描 二维码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。