Unity之生成扫描二维码

news/2024/6/18 21:09:01 标签: 前端, c#, unity3d

Unity之生成扫描二维码

  • Unity之生成扫描二维码
    • 前言
      • 开篇
      • Unity版本及使用插件
    • 正题
      • 前期准备
        • 首先生成二维码
        • 然后需要扫描二维码
        • 该使用了
      • 挂载脚本绑定按钮和输入框
      • 运行内容
        • 生成二维码
        • 扫描二维码
    • 结尾
      • 唠家常
    • 今日有推荐

Unity之生成扫描二维码

前言

开篇

  • 又到了一周一分享啦,今儿小黑为了给小可爱一个好玩的,想来想去不知道弄什么。
  • 后来一想,就制作一个生成和扫描二维码的软件吧。

Unity版本及使用插件

Unity 2020.4.4f1
zxing.unity.dll 👉 下载地址 👈

正题

前期准备

  • 1、创建项目。
  • 2、创建Plugins文件夹并且放入 zxing.unity.dll
  • 3、编写脚本

首先生成二维码

using UnityEngine;
using UnityEngine.UI;
using ZXing;

/// <summary>
/// 二维码的生成类
/// </summary>
public class QRCodeDraw : MonoBehaviour
{
    public static QRCodeDraw _Instance;
    private void Awake() => _Instance = this;
    
    /// <summary> 绘制好的二维码 </summary>
    public Image image_QRCode;
    
    //二维码绘制类
    BarcodeWriter barcodeWriter;    
    /// <summary>
    /// 将制定字符串信息转换成二维码图片信息
    /// </summary>
    /// <param name="formatStr">要生产二维码的字符串信息</param>
    /// <param name="width">二维码的宽度</param>
    /// <param name="height">二维码的高度</param>
    /// <returns>返回二维码图片的颜色数组信息</returns>
    Color32[] GeneQRCode(string formatStr, int width, int height)
    {
        //绘制二维码前进行一些设置
        ZXing.QrCode.QrCodeEncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions();
        //设置字符串转换格式,确保字符串信息保持正确
        options.CharacterSet = "UTF-8";
        //设置绘制区域的宽度和高度的像素值
        options.Width = width;
        options.Height = height;
        //设置二维码边缘留白宽度(值越大留白宽度大,二维码就减小)
        options.Margin = 1;               
        //实例化字符串绘制二维码工具
        barcodeWriter = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = options };
        //进行二维码绘制并进行返回图片的颜色数组信息
        return barcodeWriter.Write(formatStr);
    }  
    
    /// <summary>
    /// 根据二维码图片信息绘制指定字符串信息的二维码到指定区域
    /// </summary>
    /// <param name="str">要生产二维码的字符串信息</param>
    /// <param name="width">二维码的宽度</param>
    /// <param name="height">二维码的高度</param>
    /// <returns>返回绘制好的图片</returns>
    Texture2D ShowQRCode(string str, int width, int height)
    {
        //实例化一个图片类
        Texture2D t = new Texture2D(width, height);
        //获取二维码图片颜色数组信息
        Color32[] col32 = GeneQRCode(str, width, height);
        //为图片设置绘制像素颜色信息
        t.SetPixels32(col32);
        //设置信息更新应用下
        t.Apply();
        //将整理好的图片信息显示到指定区域中
        return t;
    }    
    
    /// <summary>
    /// 开始绘制指定信息的二维码
    /// </summary>
    /// <param name="formatStr">要被绘制的信息</param>
    public void DrawQRCode(string formatStr)
    {
        //Debug.Log(formatStr);
        //注意:这个宽高度大小256不要变。不然生成的信息不正确
        //256有可能是这个ZXingNet插件指定大小的绘制像素点数值
        Texture2D t = ShowQRCode(formatStr, 256, 256);
        //转为image
        Sprite image = Sprite.Create(t, new Rect(00, 0, t.width, t.height),new Vector2(0.5f,0.5f));
        //显示到UI界面的图片上
        ///texture_QRCode = t;
        image_QRCode.sprite = image;
        UnityTools.Debuger.Log("想要生成二维码的内容为:" + formatStr);
    }
}

然后需要扫描二维码

using UnityEngine;
using UnityEngine.UI;
using ZXing;
/// <summary>
/// 二维码扫描识别类
/// </summary>
public class QRCodeScanning : MonoBehaviour
{
    public static QRCodeScanning _Instance;
    private void Awake() => _Instance = this;
    
    [System.NonSerialized]
    public bool bool_Result;
    
    [System.NonSerialized]
    /// <summary> 扫描信息 </summary>
    public string str_ScanInfo;
    
    /// <summary> 摄像机映射显示区域 </summary>
    ///public Texture cameraTexture;
    public Image cameraTexture;
    
    private WebCamTexture webCamTexture;//摄像机映射纹理
    
    //二维码识别类
    BarcodeReader barcodeReader;//库文件的对象(二维码信息保存的地方)
    
    /// <summary>
    /// 开启摄像机和准备工作
    /// </summary>
    void DeviceInit()
    {
        //1、获取所有摄像机硬件
        WebCamDevice[] devices = WebCamTexture.devices;
        
        //2、获取第一个摄像机硬件的名称
        string deviceName = devices[0].name;//手机后置摄像机
        
        //3、创建实例化一个摄像机显示区域
        webCamTexture = new WebCamTexture(deviceName: deviceName);
        
        //4、显示的图片信息
        //cameraTexture = webCamTexture;
        //Debuger.Log(webCamTexture.width + "_____"+ webCamTexture.height);
        //Texture2D texture2D = Texture2Texture2D(webCamTexture);
        //Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
        //cameraTexture.sprite = sprite;
        
        //5、打开摄像机运行识别
        if (webCamTexture.isPlaying) return;
            webCamTexture.Play();
            
        //6、实例化识别二维码信息存储对象
        barcodeReader = new BarcodeReader();
    }
    
    Color32[] data;//二维码图片信息以像素点颜色信息数组存放
    
    /// <summary>
    /// 识别摄像机图片中的二维码信息
    /// 打印二维码识别到的信息
    /// </summary>
    void ScanQRCode()
    {
        if (!webCamTexture.isPlaying) return;
        
        //7、获取摄像机画面的像素颜色数组信息
        data = webCamTexture.GetPixels32();
        
        //8、获取图片中的二维码信息
        Result result = barcodeReader.Decode(data, webCamTexture.width, webCamTexture.height);
        
        //如果获取到二维码信息了,打印出来
        if (result != null)
        {
            //Debuger.Log(result.Text);//===》==》===》 这是从二维码识别出来的信息
            str_ScanInfo = result.Text;//显示扫描信息
            
            UnityTools.Debuger.Log("扫描结果为:" + str_ScanInfo);
            _Instance.bool_Result = true;
            
            //扫描成功之后的处理
            IsScanning = false;
            webCamTexture.Stop();
            
            cameraTexture.sprite = null;
        }
    }
    
    bool IsScanning = false;
    float interval = 3;//扫描识别时间间隔    
    
    /// <summary>
    /// 扫描
    /// </summary>
    public void Scanning()
    {
        if (webCamTexture != null && webCamTexture.isPlaying) return;
        //Debuger.Log("开始扫描");
        DeviceInit();
        IsScanning = true;
    }
    public void Stop()
    {
        if (!webCamTexture.isPlaying) return;
        //Debuger.Log("停止扫描");
        webCamTexture.Stop();
        IsScanning = false;
    }
    
    Texture2D texture2D;
    
    private void Update()
    {
        if (IsScann/ing)
        {
            texture2D = Texture2Texture2D(webCamTexture);
            
            Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
            
            cameraTexture.sprite = sprite;
            
            //每隔一段时间进行一次识别二维码信息
            interval += Time.deltaTime;
            if (interval >= 3)
            {
                interval = 0;
                ScanQRCode();//开始扫描
            }
        }
    }
  
    //以下参考博客https://blog.csdn.net/a673544319/article/details/82751883
    Texture2D Texture2Texture2D(Texture texture)
    {
        Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);        
        
        RenderTexture currentRT = RenderTexture.active;        
        
        RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
        
        Graphics.Blit(texture, renderTexture);
        
        RenderTexture.active = renderTexture;
        
        texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
        
        texture2D.Apply();
        
        RenderTexture.active = currentRT;
        
        RenderTexture.ReleaseTemporary(renderTexture);
        
        return texture2D;
    }
}

该使用了

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

public class QRCodeController : MonoBehaviour
{
    public InputField inputField;
        
    public void InstanceQRCode()
    {
        QRCodeDraw._Instance.DrawQRCode(inputField.text);
    }
    
    public void ScaningQRCode()
    {
        QRCodeScanning._Instance.Scanning();
    }
}

挂载脚本绑定按钮和输入框

neihao

运行内容

生成二维码

生成二维码

扫描二维码

扫描二维码

结尾

唠家常

  • 小黑的今日分享结束啦,小伙伴们你们get到了么,你们有没有更好的办法呢,可以评论区留言分享,也可以加小黑的QQ:841298494,大家一起进步。
  • 小黑现在好想让小可爱来北京找我呀,哈哈哈哈哈。

今日有推荐

推荐内容:Texture 转为 Texture2D,博客链接:
Unity中Texture转Texture2D👈👇博客链接 https://blog.csdn.net/a673544319/article/details/82751883

  • 客官,看完get之后记得点赞哟!
  • 小伙伴你还想要别的知识?好的呀,分享给你们😄
  • 小黑的杂货铺,想要什么都有,客官来杯茶喝啊

程序的道路上学习永不停止,探索随时进行。
Let’s go. Just do it. We can.


http://www.niftyadmin.cn/n/1582051.html

相关文章

Unity_UIWidgets - 组件Scaffold

UIWidgets - 组件Scaffold 各位兄弟姐妹&#xff0c;想通过Unity来开发UIWidgets的么&#xff0c;想通过UIWi的gets、、来开发手机APP么?&#xff1f;想么想么&#xff0c;哈哈哈哈哈哈哈哈。好了&#xff0c;小黑不唠叨了&#x1f602;&#xff0c;今儿给大家介绍一个开发AP…

Unity_UIWidgets - 组件AppBar

Unity_UIWidgets - 组件AppBarAppBar构造构造png观看使用代码使用效果AppBar使用结束结语图标IconQQ今日无推荐Unity_UIWidgets - 组件AppBar各位伙伴&#xff0c;周日愉快啊~~~小黑睡醒了&#xff0c;给大家来分享知识啦上周给大家一个组件Scafflod&#xff0c;那我们之后就来…

Unity_UIWidgets - 组件Container

Unity_UIWidgets - 组件ContainerContainer构造效果结语QQ今日无推荐Unity_UIWidgets - 组件Container 上周给大家讲完了Scaffold的AppBar&#xff0c;那么一个框架的头&#xff0c;是不是该有身体了呀&#xff0c;所以给大家讲一下小黑常作为身体的组件Container吧。 Contai…

Unity_UIWidgets - 组件Drawer

Unity_UIWidgets - 组件Drawer组件DrawerDrawer构造代码效果Drawer使用完毕结语图标IconQQ今日无推荐组件Drawer 各位伙伴&#xff0c;周日愉快啊~~~小黑睡醒了&#xff0c;给大家来分享知识啦上周给大家一个组件Scafflod&#xff0c;那我们之后就来挨个介绍他其中的组件吧。那…

Unity之Android端权限申请

Unity之Android端权限申请Unity之Android端权限申请前言开篇废话Unity版本正题前期准备挂载脚本打包发布安装App查看结果结尾唠家常今日无推荐Unity之Android端权限申请 前言 开篇废话 和小伙伴用了两天时间&#xff0c;进行了需求分离&#xff0c;另立项目&#xff0c;然后…

Unity_UIWidgets - 文字图标Icon

Unity_UIWidgets - 文字图标Icon文字图标Icon开篇不吐不快的废话正题主题Icon获取Icon第一步第二步第三步使用Icon1、导入至Resources文件夹下2、在脚本中进行载入3、使用4、结果结尾QQ今日无推荐文字图标Icon 开篇 不吐不快的废话 记得在3月底的时候小黑前往湖北出差&#…

Unity_UIWidgets - 按钮组件IconButton

Unity_UIWidgets - 按钮组件IconButton按钮组件IconButtonIconButton结尾QQ今日无推荐按钮组件IconButton 既然上周给大家介绍了ICON&#xff0c;那么今天给大家介绍 一下相关联的 IconButton吧 IconButton 为了给大家看清&#xff0c;一会儿小黑录屏 但是大家可以看到 上…

Unity之详解Texture

[Texture]详解什么是Texture 前言 开篇废话 嗨咯大家好呀&#xff0c;时隔多日&#xff0c;小黑又和大家见面咯&#xff0c;最近懒癌犯了&#xff0c;而且技术上边倍受打击&#xff0c;所以回了家只想着睡觉休息&#xff0c;所以希望大家见谅见谅啦。 Unity版本 Unity 202…