• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

unity:弄一个二维码扫描

武飞扬头像
被代码折磨的狗子
帮助1

一、效果图

学新通

二、dll下载 

要生成二维码需要使用 zxing.unity.dll

链接:https://pan.百度.com/s/166klhdbIi3mAesdp4JEn9A?pwd=syq1 
提取码:syq1

在unity中创建Plugins文件夹,将dll放入此文件夹中

学新通

我这里使用的unity版本是2019.4.32

三.创建二维码

网上有好多关于生成创建二维码效果的案例大家不懂也可以查查其他的,基础代码都差不多,我这里整理了一下,脚本多加了些注释。

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
    using UnityEngine.UI;
  5.  
    using ZXing;
  6.  
    using ZXing.QrCode;
  7.  
     
  8.  
    /// <summary>
  9.  
    /// 创建二维码
  10.  
    /// </summary>
  11.  
    public class CreatQR : MonoBehaviour {
  12.  
     
  13.  
    //存放二维码的纹理图片
  14.  
    Texture2D encoded;
  15.  
     
  16.  
    [Header("需要生产二维码的字符")]
  17.  
    public string QrCodeStr = "https://www.百度.com/" ;
  18.  
    [Header("在屏幕上显示二维码 ")]
  19.  
    public RawImage rawImg;
  20.  
     
  21.  
    void Start()
  22.  
    {
  23.  
    /*初始化纹理图片
  24.  
    * 注意:宽高度大小必须是256,
  25.  
    * 否则出现索引超出数组边界错误
  26.  
    */
  27.  
    encoded = new Texture2D(256, 256);
  28.  
    CreatQr(); //创建生成二维码
  29.  
    }
  30.  
     
  31.  
    #region 生成二维码
  32.  
     
  33.  
    /// <summary>
  34.  
    /// 创建二维码
  35.  
    /// </summary>
  36.  
    public void CreatQr()
  37.  
    {
  38.  
    if (QrCodeStr != string.Empty)
  39.  
    {
  40.  
    //二维码写入图片
  41.  
    var color32 = Encode(QrCodeStr, encoded.width, encoded.height);
  42.  
    encoded.SetPixels32(color32); //更改纹理的像素颜色
  43.  
    encoded.Apply();
  44.  
    //生成的二维码图片附给RawImage
  45.  
    rawImg.texture = encoded;
  46.  
    }
  47.  
    else
  48.  
    Debug.Log("没有生成信息");
  49.  
    }
  50.  
     
  51.  
    /// <summary>
  52.  
    /// 生成二维码
  53.  
    /// </summary>
  54.  
    /// <param name="textForEncoding">需要生产二维码的字符串</param>
  55.  
    /// <param name="width"></param>
  56.  
    /// <param name="height"></param>
  57.  
    /// <returns></returns>
  58.  
    private static Color32[] Encode(string formatStr, int width, int height)
  59.  
    {
  60.  
     
  61.  
    //绘制二维码前进行一些设置
  62.  
    QrCodeEncodingOptions options = new QrCodeEncodingOptions();
  63.  
     
  64.  
    //设置字符串转换格式,确保字符串信息保持正确
  65.  
    options.CharacterSet = "UTF-8";
  66.  
     
  67.  
    //设置绘制区域的宽度和高度的像素值
  68.  
    options.Width = width;
  69.  
    options.Height = height;
  70.  
     
  71.  
    //设置二维码边缘留白宽度(值越大留白宽度大,二维码就减小)
  72.  
    options.Margin = 1;
  73.  
     
  74.  
    /*实例化字符串绘制二维码工具
  75.  
    * BarcodeFormat:条形码格式
  76.  
    * Options: 编码格式(支持的编码格式)
  77.  
    */
  78.  
    var barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };
  79.  
    //进行二维码绘制并进行返回图片的颜色数组信息
  80.  
    return barcodeWriter.Write(formatStr);
  81.  
     
  82.  
    }
  83.  
    #endregion
  84.  
    }
学新通

场景布局

创建一个RawImage,给这个物体添加Button组件和新建的CreatQR脚本 

学新通

 运行后效果:

学新通

使用浏览器或其他可扫描的软件扫描此二维码可以跳转到百度页面

四.unity扫描二维码 

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
    using UnityEngine.UI;
  5.  
    using ZXing;
  6.  
     
  7.  
    /// <summary>
  8.  
    /// 扫描图片
  9.  
    /// </summary>
  10.  
    public class ScanQRCode : MonoBehaviour
  11.  
    {
  12.  
    bool isOpen = true; //true当前开启扫描状态 false 当前是关闭扫描状态
  13.  
     
  14.  
    Animator ani; //扫描动画
  15.  
     
  16.  
    private WebCamTexture m_webCameraTexture;//摄像头实时显示的画面
  17.  
    private BarcodeReader m_barcodeRender; //申请一个读取二维码的变量
  18.  
     
  19.  
    [Header("显示摄像头画面的RawImage")]
  20.  
    public RawImage m_cameraTexture;
  21.  
     
  22.  
    [Header("扫描间隔")]
  23.  
    public float m_delayTime = 3f;
  24.  
     
  25.  
    [Header("开启扫描按钮")]
  26.  
    public Button openScanBtn;
  27.  
     
  28.  
    void Start()
  29.  
    {
  30.  
    //调用摄像头并将画面显示在屏幕RawImage上
  31.  
    WebCamDevice[] tDevices = WebCamTexture.devices; //获取所有摄像头
  32.  
    string tDeviceName = tDevices[0].name; //获取第一个摄像头,用第一个摄像头的画面生成图片信息
  33.  
    m_webCameraTexture = new WebCamTexture(tDeviceName, 400, 300);//名字,宽,高
  34.  
    m_cameraTexture.texture = m_webCameraTexture; //赋值图片信息
  35.  
    m_webCameraTexture.Play(); //开始实时显示
  36.  
     
  37.  
    m_barcodeRender = new BarcodeReader();
  38.  
    ani = GetComponent<Animator>();
  39.  
     
  40.  
    OpenScanQRCode(); //默认不扫描
  41.  
    //按钮监听
  42.  
    openScanBtn.onClick.AddListener(OpenScanQRCode);
  43.  
    }
  44.  
     
  45.  
    #region 扫描二维码
  46.  
     
  47.  
    //开启关闭扫描二维码
  48.  
    void OpenScanQRCode()
  49.  
    {
  50.  
    if (isOpen)
  51.  
    {
  52.  
    //开启状态,需要关闭扫描
  53.  
    ani.Play("CloseScan", 0, 0);
  54.  
    //CancelInvoke("CheckQRCode");
  55.  
    }
  56.  
    else
  57.  
    {
  58.  
    //关闭状态,需要开启扫描
  59.  
     
  60.  
    //开始扫描
  61.  
    ani.Play("OpenScan", 0, 0);
  62.  
     
  63.  
    //以秒为单位调用方法
  64.  
    //InvokeRepeating("CheckQRCode", 0, m_delayTime);
  65.  
    }
  66.  
    isOpen = !isOpen;
  67.  
    }
  68.  
     
  69.  
    #endregion
  70.  
     
  71.  
    #region 检索二维码方法
  72.  
    /// <summary>
  73.  
    /// 检索二维码方法
  74.  
    /// </summary>
  75.  
    public void CheckQRCode()
  76.  
    {
  77.  
    //存储摄像头画面信息贴图转换的颜色数组
  78.  
    Color32[] m_colorData = m_webCameraTexture.GetPixels32();
  79.  
     
  80.  
    //将画面中的二维码信息检索出来
  81.  
    var tResult = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);
  82.  
     
  83.  
    if (tResult != null)
  84.  
    {
  85.  
    Application.OpenURL(tResult.Text);
  86.  
    Debug.Log(tResult.Text);
  87.  
    }
  88.  
    }
  89.  
    #endregion
  90.  
     
  91.  
    }
学新通

场景布局:

学新通

 这里要录制动画,可能比较麻烦,下面有程序包,大家可以下载来看看

五、程序包下载

链接:https://pan.百度.com/s/1EXpW5CUISI6Khk3dQfaeLw?pwd=syq1 
提取码:syq1

unity版本是2019.4.32

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkikeb
系列文章
更多 icon
同类精品
更多 icon
继续加载