欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity 扫描 二维码

程序员文章站 2022-07-14 08:00:18
...

 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;
                }
            }
        }
    }
}
   
相关标签: Unit