1.Vuforai AR 插件(Unity2017以后自带) 3.使用方法 1). 1.https://blog.csdn.net/caozhaodan/article/details/63254633 2.https://blog.csdn.net/xufeifandj/article/details/51744379 第一次写博客,技术有限、写的不好请大家勿喷…准备工具
2. QR CodeBarcode Scanner and Generator4.9 二维码生成加识别插件功能开发
突然接到公司需求,需实现图片和二维码识别,网上查了资料发现有很多问题,所以写了这篇文章。 1.导入两给插件。
2.编写功能代码
using UnityEngine; using ZXing; using ZXing.Client.Result; using Vuforia; using System; using System.Threading; using Tool; public class VuforaiARCameraScanQRCode : MonoBehaviour { private VuforaiARCameraScanQRCode _Instance = null; private Image.PIXEL_FORMAT mPixelFormat = Image.PIXEL_FORMAT.GRAYSCALE; private VuforiaBehaviour mVuforiaBehaviour; private bool mIsDecoding = false; private bool mIsInit = false; BarcodeReader mBarcodeReader = new BarcodeReader(); //识别二维码回调函数 public static event Action<string> IdentifyQRCodeCallbackFunction = delegate { }; private bool mIsQRCode = false; /// <summary> /// 得到实例 /// </summary> /// <returns></returns> public static VuforaiARCameraScanQRCode GetInstance() { if (_Instance == null) { _Instance = new GameObject("_VuforaiARCameraScanQRCode").AddComponent<VuforaiARCameraScanQRCode>(); } return _Instance; } void Start() { if (mVuforiaBehaviour == null) mVuforiaBehaviour = UnityHelper.GetSceneComponetScript<VuforiaBehaviour>(); if (mVuforiaBehaviour!=null) { VuforiaARController.Instance.RegisterTrackablesUpdatedCallback(OnTrackablesUpdated); } } public bool GetIsQRCode { get { return mIsQRCode; } set { mIsQRCode = value; } } public void OnTrackablesUpdated() { if (mVuforiaBehaviour.enabled && !mIsInit) { //Wait 1/4 seconds for the device to initialize (otherwise it seems to crash sometimes) mIsInit = true; Loom.QueueOnMainThread(() => { mIsInit = Vuforia.CameraDevice.Instance.SetFrameFormat(mPixelFormat, true); }, 0.25f); } } void Update() { if (mVuforiaBehaviour == null && mIsQRCode) return; if (mVuforiaBehaviour.enabled && Vuforia.CameraDevice.Instance != null && !mIsDecoding) { Vuforia.Image image = Vuforia.CameraDevice.Instance.GetCameraImage(mPixelFormat); if (image != null) { mIsDecoding = true; Loom.RunAsync(() => { try { var data = mBarcodeReader.Decode(ImageToColor32(image), image.BufferWidth, image.BufferHeight); if (data != null) { Loom.QueueOnMainThread(() => { if (data.BarcodeFormat == BarcodeFormat.QR_CODE) { //识别二维码 Debug.LogError(data.Text); if (IdentifyQRCodeCallbackFunction != null) { IdentifyQRCodeCallbackFunction.Invoke(data.Text); } } }); } } finally { mIsDecoding = false; } }); } } } Color32[] ImageToColor32(Vuforia.Image a) { if (!a.IsValid()) return null; Color32[] r = new Color32[a.BufferWidth * a.BufferHeight]; for (int i = 0; i < r.Length; i++) { r[i].r = r[i].g = r[i].b = a.Pixels[i]; } return r; } }
1)new 一个新场景,添加Vuforai 中的AR Camera ,在需要功能的脚本中初始化创建脚本及绑定识别二维码回调函数public event Action<string> IdentifyQRCodeCallbackFunction = delegate { };
2)Test 调用功能脚本。
3)然后运行场景(然后ARCamera 可以识别识别图了)
注意事项
Vuforia.CameraDevice.Instance.SetFrameFormat(mPixelFormat, true);
设置帧格式的时候不支持Image.PIXEL_FORMAT.RGB888格式,不然会报“Failed to set frame format”错误。
2).在使用mBarcodeReader.Decode这个函数的时候不能直接使用Vuforia.Image.Pixels,要将Image 转成Color32 格式。
3).代码UnityHelper.GetSceneComponetScript<VuforiaBehaviour>();
为自己编写代码,会报错误,大家请使用自己的获取代码。参考大神博客
结尾
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算