(个人)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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刚出生的婴儿拉肚子怎么办 新生儿5天拉稀水怎么办 10个月孩子拉肚子怎么办 不满月的宝宝拉肚子怎么办 一周岁宝宝发烧腹泻呕吐怎么办 6个月宝宝37度怎么办 1岁宝宝发烧37.2怎么办 新生儿发烧37度3怎么办 两个月宝宝抵抗力差怎么办 6月宝宝着凉拉稀怎么办 六个月的宝宝拉肚子怎么办 衣服颜色太深了怎么办 一多半宝宝爱喝水不爱吃饭怎么办 十个月宝宝不爱吃饭怎么办 十个月宝宝突然不爱吃饭怎么办 二十个月宝宝不爱吃饭怎么办 十个月的宝宝不爱吃饭怎么办 6年级学生数学差怎么办 打印机打不出来就是一张白纸怎么办 wps表格下拉数字不递增怎么办 wps表格圈怎么打出来怎么办 手表固定圈掉了怎么办 起来觉得头晕头胀怎么办? 孩子不好好写作业怎么办 孩子考试考差了怎么办 孩子计算题马虎大意怎么办 二年级孩子不认字怎么办 发现计算上的错误怎么办 孩子不好好做作业怎么办 手破了红肿了怎么办呢 老师反应孩子在校粗心胆小怎么办 四年级的学生计算粗心怎么办 老打孩子骂孩子怎么办 站久了脚肿了怎么办 孩子初中了书写越来越潦草怎么办 给孩子自由孩子无法无天怎么办 孩子挑食幼儿园老师该怎么办 老师说孩子挑食家长怎么办 工作中老是粗心不细心怎么办 小孩数学总是特别粗心该怎么办 孩子起范疙瘩的怎么办