• 关于Unity调用摄像头扫描二维码与生成二维码的实现方法


    1.常用的生成二维码网址 https://cli.im/
    2.上官网下载二维码插件 http://zxingnet.codeplex.com/
    3.将下载的插件中zxing.unity.dll文件放入Unity工程中Plugins文件夹下
    4.将下面脚本挂载在场景的游戏物体身上,创建两个RawImage用于显示摄像头画面与生成的二维码,根据需要修改参数(如果报错,有可能是你的电脑没有摄像头)
      1 /*************************************************
      2  * 项目名称:Unity实现启用摄像头扫描与生成二维码
      3  * 脚本创建人:魔卡
      4  * 脚本创建时间:2017.12.20
      5  * 脚本功能:二维码识别生成控制类
      6  * ***********************************************/
      7 using System.Collections;
      8 using System.Collections.Generic;
      9 using UnityEngine;
     10 using UnityEngine.UI;
     11 using ZXing;
     12 using ZXing.QrCode;
     13 
     14 //二维码识别生成控制类
     15 public class QRCode : MonoBehaviour
     16 {
     17     #region 扫描二维码
     18     //定义一个用于存储调用电脑或手机摄像头画面的RawImage
     19     public RawImage m_cameraTexture;
     20 
     21     //摄像头实时显示的画面
     22     private WebCamTexture m_webCameraTexture;
     23 
     24     //申请一个读取二维码的变量
     25     private BarcodeReader m_barcodeRender=new BarcodeReader();
     26 
     27     //多久检索一次二维码
     28     private float m_delayTime = 3f;
     29     #endregion
     30 
     31     #region 生成二维码
     32     //用于显示生成的二维码RawImage
     33     public RawImage m_QRCode;
     34 
     35     //申请一个写二维码的变量
     36     private BarcodeWriter m_barcodeWriter;
     37     #endregion
     38 
     39 
     40     #region 扫描二维码
     41     void Start ()
     42     {
     43         //调用摄像头并将画面显示在屏幕RawImage上
     44         WebCamDevice[] tDevices = WebCamTexture.devices;    //获取所有摄像头
     45         string tDeviceName = tDevices[0].name;  //获取第一个摄像头,用第一个摄像头的画面生成图片信息
     46         m_webCameraTexture = new WebCamTexture(tDeviceName, 400, 300);//名字,宽,高
     47         m_cameraTexture.texture = m_webCameraTexture;   //赋值图片信息
     48         m_webCameraTexture.Play();  //开始实时显示
     49 
     50         InvokeRepeating("CheckQRCode", 0, m_delayTime);
     51     }
     52     /// <summary>
     53     /// 检索二维码方法
     54     /// </summary>
     55     void CheckQRCode()
     56     {
     57         //存储摄像头画面信息贴图转换的颜色数组
     58         Color32[] m_colorData= m_webCameraTexture.GetPixels32();
     59     
     60         //将画面中的二维码信息检索出来
     61         var tResult= m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height);
     62 
     63         if (tResult != null)
     64         {
     65             Debug.Log(tResult.Text);
     66         }
     67     }
     68     #endregion
     69 
     70     #region 传递字符串生成二维码
     71     void Update()
     72     {
     73         if (Input.GetKeyDown(KeyCode.Escape))
     74         {
     75             //在这种写法中  宽高必须256  否则报错
     76             ShowQRCode("魔卡先生", 256, 256);
     77         }
     78     }
     79     /// <summary>
     80     /// 显示绘制的二维码
     81     /// </summary>
     82     /// <param name="s_formatStr">扫码信息</param>
     83     /// <param name="s_width">码宽</param>
     84     /// <param name="s_height">码高</param>
     85     void ShowQRCode(string s_str,int s_width,int s_height)
     86     {
     87         //定义Texture2D并且填充
     88         Texture2D tTexture = new Texture2D(s_width, s_height);
     89 
     90         //绘制相对应的贴图纹理
     91         tTexture.SetPixels32(GeneQRCode(s_str, s_width, s_height));
     92 
     93         tTexture.Apply();
     94 
     95         //赋值贴图
     96         m_QRCode.texture = tTexture;
     97     }
     98     /// <summary>
     99     /// 返回对应颜色数组
    100     /// </summary>
    101     /// <param name="s_formatStr">扫码信息</param>
    102     /// <param name="s_width">码宽</param>
    103     /// <param name="s_height">码高</param>
    104     Color32 [] GeneQRCode(string s_formatStr,int s_width,int s_height)
    105     {
    106         //设置中文编码格式,否则中文不支持
    107         QrCodeEncodingOptions tOptions = new QrCodeEncodingOptions();
    108         tOptions.CharacterSet = "UTF-8";
    109         //设置宽高
    110         tOptions.Width = s_width;
    111         tOptions.Height = s_height;
    112         //设置二维码距离边缘的空白距离
    113         tOptions.Margin = 3;
    114 
    115         //重置申请写二维码变量类       (参数为:码格式(二维码、条形码...)    编码格式(支持的编码格式)    )
    116         m_barcodeWriter = new BarcodeWriter{Format =BarcodeFormat.QR_CODE ,Options =tOptions };
    117 
    118         //将咱们需要隐藏在码后面的信息赋值上
    119         return m_barcodeWriter.Write(s_formatStr);
    120     }
    121     #endregion
    122 
    123 }

    5.生成的效果图如下:

        

  • 相关阅读:
    手写数字识别-卷积神经网络cnn(06-2)
    putty中文显示乱码解决方法
    linux可执行程序调试(c++)
    修改优化器进一步提升准确率(04-3)
    react 数组删除某一项更新setState无效的问题,react js怎么删除数组某一项,splice删除了某一项页面数据却不变
    高德地图实现一个比例圆环形聚合点缩放
    使用react context的作用React.createContext
    怎么在websotrm配置快捷启动vue-cli项目?
    vue项目找不到.eslintrc.js文件解决---帮助小白解决 'xxx' is defined but never used
    react hook性能优化使用memo、useCallback、useMemo
  • 原文地址:https://www.cnblogs.com/mrmocha/p/8076154.html
Copyright © 2020-2023  润新知