Unity调用手机摄像机识别二维码
本文实现Unity调用手机摄像,拍摄,然后识别二维码,显示二维码的内容。
需要导入一个zxing.unity.dll文件,现在这个脚本的识别数据是放在Updata里边扫描的 数据量特别大会卡 要是用的话就自己做一下一秒执行一次。我这里没有弄
下载地址:zxing.unity.dll
代码:
using System.Threading; using UnityEngine; using ZXing; public class WebCameraScript : MonoBehaviour { public string LastResult; public string Lastresult; public Color32[] data; private bool isQuit; public GUITexture myCameraTexture; private WebCamTexture webCameraTexture; private void Start() { // bool success = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO); // Checks how many and which cameras are available on the device for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++) { // We want the back camera if (!WebCamTexture.devices[cameraIndex].isFrontFacing) { //webCameraTexture = new WebCamTexture(cameraIndex, Screen.width, Screen.height); webCameraTexture = new WebCamTexture(cameraIndex, 200, 200); // Here we flip the GuiTexture by applying a localScale transformation // works only in Landscape mode myCameraTexture.transform.localScale = new Vector3(1, 1, 1); } } // Here we tell that the texture of coming from the camera should be applied // to our GUITexture. As we have flipped it before the camera preview will have the // correct orientation myCameraTexture.texture = webCameraTexture; // Starts the camera webCameraTexture.Play(); //enabled=WebCamTexture.s } public void ShowCamera() { myCameraTexture.guiTexture.enabled = true; webCameraTexture.Play(); } public void HideCamera() { myCameraTexture.guiTexture.enabled = false; webCameraTexture.Stop(); } private void OnGUI() { GUI.Label(new Rect(60, 30*1, Screen.width, 20), "LastResult:" + LastResult); if (GUI.Button(new Rect(0, 0, 100, 100), "ON/OFF")) { if (webCameraTexture.isPlaying) HideCamera(); else ShowCamera(); } } private void Update() { //data = new Color32[webCameraTexture.width * webCameraTexture.height]; data = webCameraTexture.GetPixels32(); DecodeQR(webCameraTexture.width, webCameraTexture.height); } private void DecodeQR(int W, int H) { if (isQuit) return; // create a reader with a custom luminance source var barcodeReader = new BarcodeReader {AutoRotate = true, TryHarder = true}; // while (true) { try { // decode the current frame Result result = barcodeReader.Decode(data, W, H); if (result != null) { LastResult = result.Text; // shouldEncodeNow = true; print("i read out::" + result.Text); } // Sleep a little bit and set the signal to get the next frame Thread.Sleep(200); data = null; } catch { } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
栏 目:C#教程
下一篇:C#位运算以及实例计算详解
本文标题:Unity调用手机摄像机识别二维码
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/4726.html
您可能感兴趣的文章
- 01-10C#调用dos窗口获取相关信息的方法
- 01-10同步调用和异步调用WebService
- 01-10C#调用WebService实例开发
- 01-10C++调用C#的DLL程序实现方法
- 01-10Unity3d获取系统时间
- 01-10Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作
- 01-10C#怎么实现手机短信发送功能
- 01-10C# 调用 JavaWebservice服务遇到的问题汇总
- 01-10C#开发Android百度地图手机应用程序(多地图展示)
- 01-10基于C#实现手机号码归属地接口调用
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-11ajax实现页面的局部加载
- 01-10delphi制作wav文件的方法
- 04-02jquery与jsp,用jquery
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10C#中split用法实例总结
- 01-10SublimeText编译C开发环境设置
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05DEDE织梦data目录下的sessions文件夹有什