美文网首页
Vuforia AR动态加载DataSet(三)

Vuforia AR动态加载DataSet(三)

作者: Lucas_5 | 来源:发表于2019-03-30 21:31 被阅读0次

Vuforia官网
XAMPP官网
SharpZipLib
截止目前,Vuforia SDK版本为8.1.7,已内置与Unity引擎内。
   经常会遇到项目需要动态的增删改识别图,使用云识别上成本又比较高,在识别图体量还不是特别多的情况,利用动态的加载Dataset可以很好的实现识别图的热更新。

1.下载数据包,部署于本地服务器

  本次示例通过压缩包形式,在下载数据包是选择下图选项,得到压缩格式的数据包,直接将压缩包部署本地或服务器,省去了下载unitypackge形式还得导入引擎内取出数据包的过程。


  这里应用了集成服务器环境XAMPP,一键式安装,开箱即用,局域网直接访问,比lamp server好用不要太多。安装完毕后开启Apache服务,并等待启动成功,随后可自行访问本机的内网IP进行测试,正常情况下你会看到这个XAMPP的默认页面。


  进入XAMPP的安装目录进入htdocs文件夹内的dashboard,将数据包放入。并在浏览器内输入路径 内网ip/dashboard/数据包名.zip 如http://192.168.0.108/dashboard/Test.zip 确认数据包URL访问正常。这里需要注意的是Android平台9.0以上的系统,也就是Andorid P会报出Cleartext HTTP traffic to xxx not permitted。会出现无法请求HTTP的资源,HTTPS请求则不会受影响。这里比较简单的解决办法是在Player Settings内设置Target API level版本在API Level28以下,比如Level27 Android 8.1。

2.引用第三方解压库SharpZipLib

  在Github内选择releases内下载最新版本,通过压缩文件打开找到其Dll文件,选择并导入合适.NET版本的到Unity内,使用时直接引入命名空间ICSharpCode.SharpZipLib.Zip,并封装对应的解压到指定目录的方法。




3.下载数据包,进行解压并加载

  填入数据包的URL地址进行下载,下载完成后解压到沙盒路径内,通过激活数据包来完成识别图的动态加载。代码如下


using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;

public class FileDecompression {
    /// <summary>
    /// 解压功能(下载后直接解压压缩文件到指定目录) 
    /// </summary>
    /// <param name="path">解压目标路径</param>
    /// <param name="ZipByte">原始数据</param>
    /// <param name="password">解压密码</param>
    /// <returns></returns>
    public static string SaveZip(string path,byte[] ZipByte,string password = null) {
        string result = null;
        FileStream fs = null;
        ZipInputStream zipStream = null;
        ZipEntry ent = null;
        string fileName;

        if(!Directory.Exists(path)) {
            Directory.CreateDirectory(path);
        }
        try {
            //直接使用 将byte转换为Stream,省去先保存到本地在解压的过程
            Stream stream = new MemoryStream(ZipByte);
            zipStream = new ZipInputStream(stream);

            if(!string.IsNullOrEmpty(password)) {
                zipStream.Password = password;
            }
            while((ent = zipStream.GetNextEntry()) != null) {
                if(!string.IsNullOrEmpty(ent.Name)) {
                    fileName = Path.Combine(path,ent.Name);

                    #region      Android
                    fileName = fileName.Replace('\\','/');

                    if(fileName.EndsWith("/")) {
                        Directory.CreateDirectory(fileName);
                        continue;
                    }
                    #endregion
                    fs = File.Create(fileName);

                    int size = 2048;
                    byte[] data = new byte[size];
                    while(true) {
                        size = zipStream.Read(data,0,data.Length);
                        if(size > 0) {
                            //fs.Write(data, 0, data.Length);
                            fs.Write(data,0,size);//解决读取不完整情况
                        }
                        else
                            break;
                    }
                }
            }
        }
        catch(Exception e) {
            result = e.Message;
        }
        finally {
            if(fs != null) {
                fs.Close();
                fs.Dispose();
            }
            if(zipStream != null) {
                zipStream.Close();
                zipStream.Dispose();
            }
            if(ent != null) {
                ent = null;
            }
            GC.Collect();
            GC.Collect(1);
        }
        return result;
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class DownloadUtlitile {
    public static IEnumerator DownloadDataset(string url,Action<byte[]> succeedAction,Action<string> failedAction) {
        UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
        yield return unityWebRequest.SendWebRequest();
        if(unityWebRequest.isDone && string.IsNullOrEmpty(unityWebRequest.error)) {
            succeedAction.Invoke(unityWebRequest.downloadHandler.data);
        }
        else {
            failedAction.Invoke(unityWebRequest.error);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Vuforia;

public class LoadDataset:MonoBehaviour {
    public string url;
    public Material material;
    // Start is called before the first frame update
    void Start() {
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(()=> {
                LoadDataSetForUrl(url);
        });
    }

    private void LoadDataSetForUrl(string url) {
        int startIndex = url.LastIndexOf("/") + 1;
        int conut = url.LastIndexOf(".") - startIndex;
        string zipName = url.Substring(startIndex,conut);
        string filePath = string.Format("{0}/{1}/{2}",Application.persistentDataPath,"dataset",zipName);
        string xmlPath = string.Format("{0}/{1}.xml",filePath,zipName);
        print(filePath);
        if(Directory.Exists(filePath)) {
            LoadDataSet(xmlPath);
        }
        else {
            StartCoroutine(DownloadUtlitile.DownloadDataset(url,a => {
                print("下载成功");
                if(string.IsNullOrEmpty(FileDecompression.SaveZip(filePath,a))) {
                    print("解压成功");
                    LoadDataSet(xmlPath);
                }
                else {
                    print("解压失败");
                }
            },a => {
                print("下载异常:" + a);
            }));
        }
    }


    private void LoadDataSet(string path) {
        ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
        DataSet dataSet = tracker.CreateDataSet();
        if(dataSet.Load(path,VuforiaUnity.StorageType.STORAGE_ABSOLUTE)) {
            tracker.Stop();
            if(!tracker.ActivateDataSet(dataSet)) {
                Debug.Log("<color=yellow>Failed to Activate DataSet: " + path + "</color>");
            }
            if(!tracker.Start()) {
                Debug.Log("<color=yellow>Tracker Failed to Start.</color>");
            }
            IEnumerable<TrackableBehaviour> trackableBehaviours = TrackerManager.Instance.GetStateManager().GetTrackableBehaviours();
            foreach(TrackableBehaviour trackableBehaviour in trackableBehaviours) {
                trackableBehaviour.gameObject.name = trackableBehaviour.TrackableName;
                trackableBehaviour.gameObject.AddComponent<DefaultTrackableEventHandler>();
                trackableBehaviour.gameObject.AddComponent<TurnOffBehaviour>();
                GameObject gameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
                gameObject.GetComponent<MeshRenderer>().material = material;
                gameObject.transform.parent = trackableBehaviour.gameObject.transform;
                gameObject.transform.localPosition = Vector3.zero;
                gameObject.transform.localRotation = Quaternion.identity;
                gameObject.transform.localScale = Vector3.one * 0.5f;
            }
        }
        else {
            Debug.LogError("<color=yellow>Failed to load dataset: '" + path + "'</color>");
        }
    }
}


相关文章

网友评论

      本文标题:Vuforia AR动态加载DataSet(三)

      本文链接:https://www.haomeiwen.com/subject/dhzrbqtx.html