/// <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)
        {
            resolution.ResizeTexture(webcamTex, ref resizedTexture, true);
            NativeArray <Color24> pixels = ((Texture2D)resizedTexture).GetRawTextureData <Color24>();          // _resizedTexture is Texture2D ? ((Texture2D)_resizedTexture).GetPixels32() : ((WebCamTexture)_resizedTexture).GetPixels32();

            if (aOnImageAcquired != null)
            {
                aOnImageAcquired(pixels, poseSource == null ? Matrix4x4.identity : poseSource.localToWorldMatrix, resizedTexture.width, resizedTexture.height);
            }
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public Task <ColorResult> RequestColorAsync()
        {
            resolution.ResizeTexture(webcamTex, ref resizedTexture, true);
            Texture2D t2d = resizedTexture as Texture2D;

            if (t2d != null)
            {
                return(Task.FromResult(new ColorResult(poseSource == null ? Matrix4x4.identity : poseSource.localToWorldMatrix, t2d)));
            }
            throw new NotSupportedException("This device does not support requesting colors.");
        }
        private void GetImage(Action <Texture2D, Matrix4x4> aOnFinished)
        {
            IsRequestingImage = true;

            if (camera == null)
            {
                Debug.LogError("[CameraCapture] camera hasn't been initialized!");
            }

            camera.StartPhotoModeAsync(cameraParams, startResult =>
            {
                camera.TakePhotoAsync((photoResult, frame) =>
                {
                    // Grab the camera matrix
                    Matrix4x4 transform;
                    if (!frame.TryGetCameraToWorldMatrix(out transform))
                    {
                        Debug.Log("[CameraCapture] Can't get camera matrix!!");
                        transform = Camera.main.transform.localToWorldMatrix;
                    }
                    else
                    {
                        transform[0, 2] = -transform[0, 2];
                        transform[1, 2] = -transform[1, 2];
                        transform[2, 2] = -transform[2, 2];
                        //transform = transform; //transform * Camera.main.transform.localToWorldMatrix.inverse;
                    }
                    Matrix4x4 proj;
                    if (!frame.TryGetProjectionMatrix(out proj))
                    {
                        fieldOfView = Mathf.Atan(1.0f / proj[0, 0]) * 2.0f * Mathf.Rad2Deg;
                    }

                    frame.UploadImageDataToTexture(cacheTex);
                    Texture tex = resizedTex;
                    resolution.ResizeTexture(cacheTex, ref tex, true);
                    resizedTex = (Texture2D)tex;

                    if (aOnFinished != null)
                    {
                        aOnFinished(resizedTex, transform);
                    }

                    camera.StopPhotoModeAsync((a) =>
                    {
                        IsRequestingImage = false;
                    });
                });
            });
        }
        /// <summary>
        /// Captures an image from the camera.
        /// </summary>
        /// <returns>
        /// A <see cref="Task"/> that yields a <see cref="TextureResult"/>.
        /// </returns>
        private async Task <TextureResult> CaptureImageAsync()
        {
            // Make sure we're initialized before proceeding
            EnsureInitialized();

            // Wait for the camera to start photo mode
            await camera.StartPhotoModeAsync(cameraParams);

            // Update camera values (which can only be done while streaming)
            await wrapper.SetExposureAsync(exposure);

            await wrapper.SetWhiteBalanceAsync(temperature);

            await wrapper.SetISOAsync(iso);

            // Take a picture and get the result
            var takeResult = await camera.TakePhotoAsync();

            // Camera matrix is broken in some unity builds
            // See: https://forum.unity.com/threads/locatable-camera-not-working-on-hololens-2.831514/

            // Shortcut to frame
            var frame = takeResult.Frame;

            // Grab the camera matrix
            Matrix4x4 transform;

            if ((frame.hasLocationData) && (frame.TryGetCameraToWorldMatrix(out transform)))
            {
                transform[0, 2] = -transform[0, 2];
                transform[1, 2] = -transform[1, 2];
                transform[2, 2] = -transform[2, 2];
            }
            else
            {
                if (!hasWarnedCameraMatrix)
                {
                    hasWarnedCameraMatrix = true;
                    Debug.LogWarning($"{nameof(CameraCaptureUWP)} can't get camera matrix. Falling back to main camera.");
                }
                transform = Camera.main.transform.localToWorldMatrix;
            }

            Matrix4x4 proj;

            if (!frame.TryGetProjectionMatrix(out proj))
            {
                fieldOfView = Mathf.Atan(1.0f / proj[0, 0]) * 2.0f * Mathf.Rad2Deg;
            }

            frame.UploadImageDataToTexture(cacheTex);
            Texture tex = resizedTex;

            resolution.ResizeTexture(cacheTex, ref tex, true);
            resizedTex = (Texture2D)tex;

            // Wait for camera to stop
            await camera.StopPhotoModeAsync();

            // Pass on results
            return(new TextureResult(transform, resizedTex));
        }