(个人)AR电子书系统创新实训第二周(1)
来源:互联网 发布:linux暴力破解root密码 编辑:程序博客网 时间:2024/05/22 08:29
从头实现一个识别二维码的Unity项目
通过上次大致了解了ZXing.Net的基本使用方法后,此次我决定使用它和unity制作一个简单的测试项目,以检验其功能是否满足要求。具体步骤如下:
1.创建Unity项目,将zxing.unity.dll文件放置在工程目录下,Unity会自行进行编译。
2.创建一个C#脚本,命名随意。
在脚本中写入如下代码:
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using System.Threading;using ZXing;using ZXing.QrCode;public class ScanCameraScript : MonoBehaviour { public Text testText; private WebCamTexture camTexture; private Thread qrThread; private int W, H; private Rect ScreenRect; private string url; private Color32[] c; private bool isQuit; private void OnGUI() { float[] tmpsize = { Screen.width * 2 / 3, Screen.height * 2 / 3 }; float camsize = Mathf.Min(tmpsize); ScreenRect = new Rect((Screen.width - camsize)/2, (Screen.height - camsize) / 2, camsize, camsize); GUI.DrawTexture(ScreenRect, camTexture, ScaleMode.ScaleToFit); testText.text = url; } // Use this for initialization void Start () { camTexture = new WebCamTexture(); camTexture.requestedHeight = Screen.height; // 480; camTexture.requestedWidth = Screen.width; //640; if (camTexture != null) { camTexture.Play(); W = camTexture.width; H = camTexture.height; } qrThread = new Thread(DecodeQR); qrThread.Start(); } // Update is called once per frame void Update () { if (c == null) { c = camTexture.GetPixels32(); } } void OnDestroy() { qrThread.Abort(); camTexture.Stop(); } void OnApplicationQuit() { isQuit = true; } void DecodeQR() { // create a reader with a custom luminance source var barcodeReader = new BarcodeReader { AutoRotate = false, TryHarder = false }; while (true) { if (isQuit) break; try { // decode the current frame var result = barcodeReader.Decode(c, W, H); if (result != null) { print(result.Text); url = result.Text; } // Sleep a little bit and set the signal to get the next frame Thread.Sleep(200); c = null; } catch { } } }}
这里稍微解释一下代码的内容:
这个代码的主要功能便是负责实时地将当前摄像机镜头的内容显示到屏幕上,同时,每隔一段时间便会对当前画面进行分析解码以得到二维码的网址。其中的camTexture对象在调用了Play()方法后会不断的获取摄像头的内容,并将其保存到颜色数组c中。同时,代码中调用了新线程,在里面执行DecodeQR()函数,也就是解码函数,该进程会在一个循环中对c进行解码并得到网址,赋值给url,然后休眠0.2秒。此外还有一个OnGUI()函数,该函数负责每帧将camTexure中的图像输出到屏幕,同时把url的值传给Text组件以显示出来。
3.将这个C#脚本附加到场景中的主摄像机上。
4.在场景中创建一个Canvas组件和Text组件,将Text组件赋值给C#脚本中的testText。
5.运行结果。
(注:二维码图片来自网络)
顺利运行。
阅读全文
0 0
- (个人)AR电子书系统创新实训第二周(1)
- (个人)AR电子书系统创新实训第二周(1)
- (个人)AR电子书系统创新实训第二周(1)
- (个人)AR电子书系统创新实训第二周(1)
- (个人)AR电子书系统创新实训第二周(1)
- (个人)AR电子书系统创新实训第二周(2)
- (个人)AR电子书系统创新实训第二周(2)
- (个人)AR电子书系统创新实训第二周(2)
- (个人)AR电子书系统创新实训第二周(2)
- (个人)AR电子书系统创新实训第二周(2)
- (个人)AR电子书系统创新实训第六周
- (个人)AR电子书系统创新实训中期总结
- (个人)AR电子书系统创新实训期中汇报
- (个人)AR电子书系统创新实训第三周(1)
- (个人)AR电子书系统创新实训第三周(1)
- (个人)AR电子书系统创新实训第三周(1)
- (个人)AR电子书创新系统实训第三周(1)
- (个人)AR电子书系统创新实训第三周(1)
- Math数组Date
- codevs 1078 最小生成树
- 5.17
- 细致分析cookie、session、sessionid 与jsessionid
- 欢迎使用CSDN-markdown编辑器
- (个人)AR电子书系统创新实训第二周(1)
- 多节点安装部署kubernetes
- 华为实习生笔试题
- Golang程序配置方案小结
- Struts1框架四之文件上传
- 进度条的开发
- java基础技术知识点总结——构造器的调用顺序
- python 注释规范
- [设计模式]访问者模式(Visitor)