gpt4 book ai didi

c# - 无法将文件/图像设置为对象跟踪器的 RunTimeImageSource

转载 作者:行者123 更新时间:2023-12-03 20:50:30 27 4
gpt4 key购买 nike

我正在尝试在运行时动态创建图像目标。为此,我从 vuforia AR 相机捕获屏幕并将其保存在 Application.persistentDataPath 中。然后我按照 How to Create and Load Targets in Unity 中提供的脚本创建数据集可跟踪行为.
我用于捕获屏幕的代码:

            var datapath = Application.persistentDataPath;

datapath = Path.Combine(datapath, "image.jpg");

int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] bytes = tex.EncodeToJPG();

File.WriteAllBytes(datapath, bytes);
创建可跟踪行为的代码:

var objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
var runtimeImageSource = objectTracker.RuntimeImageSource;
//Problem here
bool result = runtimeImageSource.SetFile(VuforiaUnity.StorageType.STORAGE_ABSOLUTE, datapath, 0.15f, "Temp");
// create a new dataset and use the source to create a new trackable
var dataset = objectTracker.CreateDataSet();

var trackableBehaviour = dataset.CreateTrackable(runtimeImageSource, "Temp");
// add the DefaultTrackableEventHandler to the newly created game object
trackableBehaviour.gameObject.AddComponent<DefaultTrackableEventHandler>();
这适用于我的 Windows 笔记本电脑,但不适用于我的 android 手机。经过一番调试,我发现 bool result是假的,因此, var trackableBehaviour正在成为 null .
编辑:问题不在于写权限。我可以看到图像被保存在persistentDataPath 中。问题在于将运行时图像源设置为保存的图像。
编辑 2:我尝试直接使用纹理而不是像这样的文件 bool result = runtimeImageSource.SetImage(tex, 1f, "Temp"); .结果还是假的。因此,我认为问题不在于路径解析,而在于其他问题。

最佳答案

  • 首先,尝试使用 VuforiaUnity.StorageType.STORAGE_APP 而不是绝对的。
  • 第二:尝试 bool result = DataSet.Exists (String path, VuforiaUnity.StorageType storageType) 并调试哪个路径将在手机上返回 true
    enter image description here
  • Application.persistentDataPath 返回的路径没有反斜杠,这意味着尝试添加“/image.jpg”而不是 image.jpg,这也可能是问题所在。
  • 关于c# - 无法将文件/图像设置为对象跟踪器的 RunTimeImageSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63147244/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com