Example #1
0
        /// <inheritdoc/>
        public Task <ColorResult> RequestColorAsync()
        {
            Vector2Int size = resolution.AdjustSize(new Vector2Int(sourceCamera.pixelWidth, sourceCamera.pixelHeight));

            GrabScreen(size);
            return(Task.FromResult(new ColorResult(sourceCamera.transform.localToWorldMatrix, captureTex)));
        }
        /// <summary>
        /// Request an image from the camera, and provide it as an array of colors on the CPU!
        /// </summary>
        /// <param name="onImageAcquired">This is the function that will be called when the image is ready. Matrix is the transform of the device when the picture was taken, and integers are width and height of the NativeArray.</param>
        public void RequestImage(Action <NativeArray <Color24>, Matrix4x4, int, int> aOnImageAcquired)
        {
            Vector2Int size = resolution.AdjustSize(new Vector2Int(sourceCamera.pixelWidth, sourceCamera.pixelHeight));

            GrabScreen(size);

            if (aOnImageAcquired != null)
            {
                aOnImageAcquired(captureTex.GetRawTextureData <Color24>(), sourceCamera.transform.localToWorldMatrix, size.x, size.y);
            }
        }
        /// <summary>
        /// Gets the image data from ARFoundation, preps it, and drops it into captureTex.
        /// </summary>
        /// <returns>
        /// A <see cref="Task"/> that yields <c>true</c> if the capture was successful; otherwise <c>false</c>.
        /// </returns>
        private Task <bool> GrabScreenAsync()
        {
            // Grab the latest image from ARFoundation
            XRCameraImage image;

            if (!cameraManager.TryGetLatestImage(out image))
            {
                Debug.LogError("[CameraCaptureARFoundation] Could not get latest image!");
                Task.FromResult <bool>(false);
            }

            // Set up resizing parameters
            Vector2Int size             = resolution.AdjustSize(new Vector2Int(image.width, image.height));
            var        conversionParams = new XRCameraImageConversionParams
            {
                inputRect        = new RectInt(0, 0, image.width, image.height),
                outputDimensions = new Vector2Int(size.x, size.y),
                outputFormat     = TextureFormat.RGB24,
                transformation   = CameraImageTransformation.MirrorY,
            };

            // make sure we have a texture to store the resized image
            if (captureTex == null || captureTex.width != size.x || captureTex.height != size.y)
            {
                if (captureTex != null)
                {
                    GameObject.Destroy(captureTex);
                }
                captureTex = new Texture2D(size.x, size.y, TextureFormat.RGB24, false);
            }

            // Create a completion source to wait for the async operation
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

            // And do the resize!
            image.ConvertAsync(conversionParams, (status, p, data) =>
            {
                if (status == AsyncCameraImageConversionStatus.Ready)
                {
                    captureTex.LoadRawTextureData(data);
                    captureTex.Apply();
                }
                if (status == AsyncCameraImageConversionStatus.Ready || status == AsyncCameraImageConversionStatus.Failed)
                {
                    image.Dispose();

                    // TODO: Should we log the failure or fail the task? Previously this completed no matter what.
                    tcs.SetResult(status == AsyncCameraImageConversionStatus.Ready);
                }
            });

            // Return the completion source task so callers can await
            return(tcs.Task);
        }
Example #4
0
        /// <summary>
        /// Gets the image data from ARFoundation, preps it, and drops it into captureTex.
        /// </summary>
        /// <param name="aOnFinished">Gets called when this method is finished with getting the image.</param>
        private void GrabScreen(Action aOnFinished)
        {
            // Grab the latest image from ARFoundation
            CameraImage image;

            if (!ARSubsystemManager.cameraSubsystem.TryGetLatestImage(out image))
            {
                Debug.LogError("[CameraCaptureARFoundation] Could not get latest image!");
                return;
            }

            // Set up resizing parameters
            Vector2Int size             = resolution.AdjustSize(new Vector2Int(image.width, image.height));
            var        conversionParams = new CameraImageConversionParams
            {
                inputRect        = new RectInt(0, 0, image.width, image.height),
                outputDimensions = new Vector2Int(size.x, size.y),
                outputFormat     = TextureFormat.RGB24,
                transformation   = CameraImageTransformation.MirrorY,
            };

            // make sure we have a texture to store the resized image
            if (captureTex == null || captureTex.width != size.x || captureTex.height != size.y)
            {
                if (captureTex != null)
                {
                    GameObject.Destroy(captureTex);
                }
                captureTex = new Texture2D(size.x, size.y, TextureFormat.RGB24, false);
            }

            // And do the resize!
            image.ConvertAsync(conversionParams, (status, p, data) =>
            {
                if (status == AsyncCameraImageConversionStatus.Ready)
                {
                    captureTex.LoadRawTextureData(data);
                    captureTex.Apply();
                }
                if (status == AsyncCameraImageConversionStatus.Ready || status == AsyncCameraImageConversionStatus.Failed)
                {
                    image.Dispose();
                    aOnFinished();
                }
            });
        }