unity中的二维码识别,ZXing插件识别二维码并跳转网页

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using ZXing;

public class QRCodeReader : MonoBehaviour
{
    public RawImage cameraImage;
    private WebCamTexture camTexture;
    private BarcodeReader barcodeRead;
    private Thread threadReadQR;
    Color32[] colorArr;
    public int width;
    public int height;
    private bool IsFinishRead = false;
    private string ResText;
    public Text Text;
    private void Start()
    {
        cameraImage.GetComponent<RectTransform>().sizeDelta = new Vector2(Screen.height, Screen.width);
        barcodeRead= new BarcodeReader();
        StartCoroutine(OpenPhoneCamera());
    }
   
    private void Update()
    {
        if (colorArr == null && camTexture != null)
        {
            colorArr = camTexture.GetPixels32(); ;
        }

        if (IsFinishRead)
        {         
            Text.text = ResText;
             OpenURL(Text.text);
             IsFinishRead = false;
         
        }
    }
    /// <summary>
    /// 打开手机摄像头
    /// </summary>
    /// <returns></returns>
    public IEnumerator OpenPhoneCamera()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);//请求打开手机摄像头权限,手机端会弹出一个选择弹窗
        WebCamDevice[] devices = WebCamTexture.devices;
        camTexture = new WebCamTexture(devices[0].name,Screen.width,Screen.height);
        if (!camTexture.isPlaying|| camTexture==null)//解决第一次打开相机出线白屏问题
        {
            camTexture = new WebCamTexture(devices[0].name, Screen.width, Screen.height);
        }
        camTexture.Play();
        cameraImage.texture = camTexture;
        width = camTexture.width;
        height = camTexture.height;
        if (camTexture!=null&& camTexture.isPlaying)
        {       
             threadReadQR = new Thread(DeCodeQR);//开启一个线程防止每次识别时候卡顿
             threadReadQR.Start();          
        }
    }
   
    /// <summary>
    /// 二维码识别
    /// </summary>
    public void DeCodeQR()
    {
        while (true)
        {
            if (colorArr!=null)
            {
                Result result = barcodeRead.Decode(colorArr, width, height);
                if (result != null)
                {
                    IsFinishRead = true;
                    ResText = result.Text;                 
                }
                Thread.Sleep(10);
                colorArr = null;
            }    
        }
     
    }

    public void OnDestroy()
    {
        threadReadQR.Abort();
        Debug.Log("结束线程");
    }
    /// <summary>
    /// 打开网页
    /// </summary>
    /// <param name="url"></param>
    public void OpenURL(string url)
    {
#if UNITY_EDITOR
        Application.OpenURL(url);
#endif

#if !UNITY_EDITOR
        Application.OpenURL("https://"+url);
#endif

    }

}


相关代码插件链接:二维码生成以及读取

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1208409371

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值