unity之制作二维码扫描

一、效果图

二、dll下载 

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

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

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

我这里使用的unity版本是2019.4.32

三.创建二维码

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using ZXing.QrCode;

/// <summary>
/// 创建二维码
/// </summary>
public class CreatQR : MonoBehaviour {

    //存放二维码的纹理图片
    Texture2D encoded;

    [Header("需要生产二维码的字符")]
    public string QrCodeStr = "https://www.baidu.com/" ;
    [Header("在屏幕上显示二维码 ")]
    public RawImage rawImg;

    void Start()
    {
        /*初始化纹理图片
         * 注意:宽高度大小必须是256,
         * 否则出现索引超出数组边界错误
         */
        encoded = new Texture2D(256, 256); 
        CreatQr(); //创建生成二维码
    }

    #region 生成二维码

    /// <summary>  
    /// 创建二维码
    /// </summary>  
    public void CreatQr()
    {
        if (QrCodeStr != string.Empty)
        {
            //二维码写入图片    
            var color32 = Encode(QrCodeStr, encoded.width, encoded.height);
            encoded.SetPixels32(color32); //更改纹理的像素颜色
            encoded.Apply();
            //生成的二维码图片附给RawImage    
            rawImg.texture = encoded;
        }
        else
            Debug.Log("没有生成信息");
    }

    /// <summary>
    /// 生成二维码 
    /// </summary>
    /// <param name="textForEncoding">需要生产二维码的字符串</param>
    /// <param name="width">宽</param>
    /// <param name="height">高</param>
    /// <returns></returns>       
    private static Color32[] Encode(string formatStr, int width, int height)
    {

        //绘制二维码前进行一些设置
        QrCodeEncodingOptions options = new QrCodeEncodingOptions();

        //设置字符串转换格式,确保字符串信息保持正确
        options.CharacterSet = "UTF-8";

        //设置绘制区域的宽度和高度的像素值
        options.Width = width;
        options.Height = height;

        //设置二维码边缘留白宽度(值越大留白宽度大,二维码就减小)
        options.Margin = 1;

        /*实例化字符串绘制二维码工具
         * BarcodeFormat:条形码格式
         * Options: 编码格式(支持的编码格式)
         */
        var barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };
        //进行二维码绘制并进行返回图片的颜色数组信息
        return barcodeWriter.Write(formatStr);

    }
    #endregion
}

场景布局

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

 运行后效果:

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

四.unity扫描二维码 

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

/// <summary>
/// 扫描图片
/// </summary>
public class ScanQRCode : MonoBehaviour
{
    bool isOpen = true; //true当前开启扫描状态 false 当前是关闭扫描状态

    Animator ani; //扫描动画

    private WebCamTexture m_webCameraTexture;//摄像头实时显示的画面
    private BarcodeReader m_barcodeRender; //申请一个读取二维码的变量

    [Header("显示摄像头画面的RawImage")]
    public RawImage m_cameraTexture;

    [Header("扫描间隔")]
    public float m_delayTime = 3f;

    [Header("开启扫描按钮")]
    public Button openScanBtn;

    void Start()
    {
        //调用摄像头并将画面显示在屏幕RawImage上
        WebCamDevice[] tDevices = WebCamTexture.devices;    //获取所有摄像头
        string tDeviceName = tDevices[0].name;  //获取第一个摄像头,用第一个摄像头的画面生成图片信息
        m_webCameraTexture = new WebCamTexture(tDeviceName, 400, 300);//名字,宽,高
        m_cameraTexture.texture = m_webCameraTexture;   //赋值图片信息
        m_webCameraTexture.Play();  //开始实时显示

        m_barcodeRender = new BarcodeReader();
        ani = GetComponent<Animator>();

        OpenScanQRCode(); //默认不扫描
        //按钮监听
        openScanBtn.onClick.AddListener(OpenScanQRCode);
    }

    #region 扫描二维码

    //开启关闭扫描二维码
    void OpenScanQRCode()
    {
        if (isOpen)
        {
            //开启状态,需要关闭扫描
            ani.Play("CloseScan", 0, 0);
            //CancelInvoke("CheckQRCode");
        }
        else
        {
            //关闭状态,需要开启扫描

            //开始扫描
            ani.Play("OpenScan", 0, 0);

            //以秒为单位调用方法 
            //InvokeRepeating("CheckQRCode", 0, m_delayTime);
        }
        isOpen = !isOpen;
    }

    #endregion

    #region 检索二维码方法
    /// <summary>
    /// 检索二维码方法
    /// </summary>
    public void CheckQRCode()
    {
        //存储摄像头画面信息贴图转换的颜色数组
        Color32[] m_colorData = m_webCameraTexture.GetPixels32();

        //将画面中的二维码信息检索出来
        var tResult = m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);

        if (tResult != null)
        {
            Application.OpenURL(tResult.Text);
            Debug.Log(tResult.Text);
        }
    }
    #endregion

}

场景布局:

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

五、程序包下载

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

unity版本是2019.4.32

猜你喜欢

转载自blog.csdn.net/qq_42345116/article/details/125909302