コード例 #1
0
        private IEnumerator LoadImageBuffer(Buffer buffer, ImageFileSourceData source)
        {
            using (buffer)
            {
                Optional <Image> imageOptional = null;
                bool             taskFinished  = false;
                EasyARController.Instance.Worker.Run(() =>
                {
                    imageOptional = ImageHelper.decode(buffer);
                    taskFinished  = true;
                });

                while (!taskFinished)
                {
                    yield return(0);
                }
                if (imageOptional.OnNone)
                {
                    throw new Exception("invalid buffer");
                }

                using (var image = imageOptional.Value)
                    using (var param = new ImageTargetParameters())
                    {
                        param.setImage(image);
                        param.setName(source.Name);
                        param.setScale(source.Scale);
                        param.setUid(Guid.NewGuid().ToString());
                        param.setMeta(string.Empty);
                        var targetOptional = ImageTarget.createFromParameters(param);
                        if (targetOptional.OnSome)
                        {
                            Target = targetOptional.Value;
                            if (Target != null && TargetAvailable != null)
                            {
                                TargetAvailable();
                            }
                        }
                        else
                        {
                            throw new Exception("invalid parameter");
                        }
                    }
            }
            UpdateTargetInTracker();
        }
コード例 #2
0
        private void CreateTargetFileByByteArray(byte[] data, string name)
        {
            using (Buffer buffer = Buffer.wrapByteArray(data))
            {
                var imageOptional = ImageHelper.decode(buffer);
                if (imageOptional.OnNone)
                {
                    throw new Exception("invalid image data");
                }
                using (var image = imageOptional.Value)
                    using (var param = new ImageTargetParameters())
                    {
                        var uid = Guid.NewGuid().ToString();
                        param.setImage(image);
                        param.setName(name);
                        param.setScale(targetScale);
                        param.setUid(uid);
                        param.setMeta(string.Empty);
                        var targetOptional = ImageTarget.createFromParameters(param);
                        if (targetOptional.OnNone)
                        {
                            throw new Exception("invalid parameter");
                        }

                        if (!Directory.Exists(outputPathDir))
                        {
                            Directory.CreateDirectory(outputPathDir);
                        }
                        var path = outputPathDir + "/" + (string.IsNullOrEmpty(name) ? uid : name) + ".etd";
                        if (targetOptional.Value.save(path))
                        {
                            Debug.Log("Created etd: " + path);
                        }
                        else
                        {
                            Debug.LogWarning("Fail to create etd: " + path);
                        }
                    }
            }
        }
コード例 #3
0
        private void CreateMultipleTargetsFromOneImage(Image image, int count, string name, float scale)
        {
            for (int i = 0; i < count; i++)
            {
                using (var param = new ImageTargetParameters())
                {
                    param.setImage(image);
                    param.setName(name);
                    param.setScale(scale);
                    param.setUid(Guid.NewGuid().ToString());
                    param.setMeta(string.Empty);
                    var targetOptional = ImageTarget.createFromParameters(param);
                    if (targetOptional.OnSome)
                    {
                        var        target     = targetOptional.Value;
                        GameObject Target     = new GameObject(name + " <" + i + ">");
                        var        controller = Target.AddComponent <ImageTargetController>();
                        AddTargetControllerEvents(controller);

                        controller.SourceType   = ImageTargetController.DataSource.Target;
                        controller.TargetSource = target;
                        controller.Tracker      = ImageTracker;

                        if (Cube)
                        {
                            var cube = Instantiate(Cube);
                            cube.transform.parent = controller.transform;
                        }
                    }
                    else
                    {
                        throw new Exception("invalid parameter");
                    }
                }
            }
        }
コード例 #4
0
    private IEnumerator LoadImageTarget()
    {
        var path = TargetPath;
        var type = Type;
        WWW www;

        if (type == PathType.Absolute)
        {
            path = Utility.AddFileHeader(path);
#if UNITY_ANDROID && !UNITY_EDITOR
            path = "file://" + path;
#endif
        }
        else if (type == PathType.StreamingAssets)
        {
            path = Utility.AddFileHeader(Application.streamingAssetsPath + "/" + path);
        }
        Debug.Log("[EasyAR]:" + path);
        www = new WWW(path);
        while (!www.isDone)
        {
            yield return(0);
        }
        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.LogError(www.error);
            www.Dispose();
            yield break;
        }
        var           data   = www.bytes;
        easyar.Buffer buffer = easyar.Buffer.create(data.Length);
        var           ptr    = buffer.data();
        Marshal.Copy(data, 0, ptr, data.Length);

        Optional <easyar.ImageTarget> op_target;
        if (targetType == TargetType.LocalImage)
        {
            var image = ImageHelper.decode(buffer);
            if (!image.OnSome)
            {
                throw new System.Exception("decode image file data failed");
            }

            var p = new ImageTargetParameters();
            p.setImage(image.Value);
            p.setName(TargetName);
            p.setScale(TargetSize);
            p.setUid("");
            p.setMeta("");
            op_target = ImageTarget.createFromParameters(p);

            if (!op_target.OnSome)
            {
                throw new System.Exception("create image target failed from image target parameters");
            }

            image.Value.Dispose();
            buffer.Dispose();
            p.Dispose();
        }
        else
        {
            op_target = ImageTarget.createFromTargetData(buffer);

            if (!op_target.OnSome)
            {
                throw new System.Exception("create image target failed from image target target data");
            }

            buffer.Dispose();
        }

        target = op_target.Value;
        Destroy(www.texture);
        www.Dispose();
        if (ImageTracker == null)
        {
            yield break;
        }
        ImageTracker.LoadImageTarget(this, (_target, status) =>
        {
            targetImage = ((_target as ImageTarget).images())[0];
            Debug.Log("[EasyAR] Targtet name: " + _target.name() + " target runtimeID: " + _target.runtimeID() + " load status: " + status);
            Debug.Log("[EasyAR] Target size" + targetImage.width() + " " + targetImage.height());
        });
    }