Exemple #1
0
        /// <summary>
        /// Captures a photo. Photo data is stored to ImageStream, and
        /// application is navigated to the preview page after capturing.
        /// </summary>
        private async Task Capture()
        {
            bool goToPreview = false;

            if (!_capturing)
            {
                CapturePreview.Source = null;
                Progress.IsActive     = true;
                _capturing            = true;

                _dataContext.ResetStreams();

                IRandomAccessStream stream = _dataContext.FullResolutionStream.AsRandomAccessStream();
                await _photoCaptureManager.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);

                await _photoCaptureManager.StopPreviewAsync();

                await AppUtils.ScaleImageStreamAsync(
                    _dataContext.FullResolutionStream,
                    _dataContext.FullResolution,
                    _dataContext.PreviewResolutionStream,
                    _dataContext.PreviewResolution);

                _capturing  = false;
                goToPreview = true;
            }

            if (goToPreview)
            {
                _dataContext.WasCaptured = true;
                Frame.Navigate(typeof(PreviewPage));
            }
        }
        /// <summary>
        /// Called when an image has been selected using PhotoChooserTask.
        /// </summary>
        /// <param name="sender">PhotoChooserTask that is completed.</param>
        /// <param name="e">Result of the task, including chosen photo.</param>
        private async void PhotoChooserTask_Completed_Async(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
            {
                DataContext dataContext = FilterEffects.DataContext.Instance;

                // Reset the streams
                dataContext.ResetStreams();

                // Use the largest possible dimensions
                WriteableBitmap bitmap = new WriteableBitmap(3552, 2448);

                BitmapImage image = new BitmapImage();
                image.SetSource(e.ChosenPhoto);

                try
                {
                    // Jpeg images can be used as such
                    bitmap.LoadJpeg(e.ChosenPhoto);
                    e.ChosenPhoto.Position = 0;
                    e.ChosenPhoto.CopyTo(dataContext.FullResolutionStream);
                }
                catch (Exception /*ex*/)
                {
                    // Image format is not jpeg. Can be anything, so first
                    // load it into a bitmap image and then write as jpeg
                    bitmap = new WriteableBitmap(image);
                    bitmap.SaveJpeg(dataContext.FullResolutionStream, image.PixelWidth, image.PixelHeight, 0, 100);
                }

                dataContext.SetFullResolution(image.PixelWidth, image.PixelHeight);

                dataContext.PreviewResolution = new Windows.Foundation.Size(
                    FilterEffects.DataContext.DefaultPreviewResolutionWidth, 0);
                int previewWidth  = (int)FilterEffects.DataContext.DefaultPreviewResolutionWidth;
                int previewHeight = 0;

                AppUtils.CalculatePreviewResolution(
                    (int)dataContext.FullResolution.Width, (int)dataContext.FullResolution.Height,
                    ref previewWidth, ref previewHeight);

                dataContext.SetPreviewResolution(previewWidth, previewHeight);

                await AppUtils.ScaleImageStreamAsync(
                    e.ChosenPhoto, dataContext.PreviewResolutionStream, dataContext.PreviewResolution);

                // Get the storyboard from application resources
                Storyboard sb = (Storyboard)Resources["CaptureAnimation"];
                sb.Begin();

                NavigationService.Navigate(new Uri("/PreviewPage.xaml", UriKind.Relative));
            }
        }
        /// <summary>
        /// Captures a photo. Photo data is stored to ImageStream, and
        /// application is navigated to the preview page after capturing.
        /// </summary>
        private async Task Capture()
        {
            bool goToPreview = false;

            if (!_capturing)
            {
                _capturing = true;

                DataContext dataContext = FilterEffects.DataContext.Instance;

                // Reset the streams
                dataContext.ResetStreams();

                CameraCaptureSequence sequence = _photoCaptureDevice.CreateCaptureSequence(1);
                sequence.Frames[0].CaptureStream   = dataContext.FullResolutionStream.AsOutputStream();
                sequence.Frames[0].ThumbnailStream = dataContext.PreviewResolutionStream.AsOutputStream();

                await _photoCaptureDevice.PrepareCaptureSequenceAsync(sequence);

                await sequence.StartCaptureAsync();

                // Get the storyboard from application resources
                Storyboard sb = (Storyboard)Resources["CaptureAnimation"];
                sb.Begin();

                _capturing  = false;
                goToPreview = true;
            }

            _photoCaptureDevice.SetProperty(
                KnownCameraPhotoProperties.LockedAutoFocusParameters,
                AutoFocusParameters.None);

            if (goToPreview)
            {
                NavigationService.Navigate(new Uri("/PreviewPage.xaml", UriKind.Relative));
            }
        }
        /// <summary>
        /// Captures a photo. Photo data is stored to ImageStream, and
        /// application is navigated to the preview page after capturing.
        /// </summary>
        private async Task CaptureAsync()
        {
            bool goToPreview = false;

            if (CameraState == CameraStates.Initialized)
            {
                CameraState                = CameraStates.Capturing;
                CaptureButton.IsEnabled    = false;
                MyCaptureElement.Source    = null;
                ProgressIndicator.IsActive = true;

                _dataContext.ResetStreams();

                IRandomAccessStream stream = _dataContext.FullResolutionStream.AsRandomAccessStream();
                await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);

                await _mediaCapture.StopPreviewAsync();

                _mediaCapture.Dispose();
                _mediaCapture = null;

                await AppUtils.ScaleImageStreamAsync(
                    _dataContext.FullResolutionStream,
                    _dataContext.FullResolution,
                    _dataContext.PreviewResolutionStream,
                    _dataContext.PreviewResolution);

                CameraState = CameraStates.Initialized;
                goToPreview = true;
            }

            if (goToPreview)
            {
                _dataContext.WasCaptured = true;
                Frame.Navigate(typeof(PreviewPage));
            }
        }
Exemple #5
0
        /// <summary>
        /// Reads the given image file and writes it to the buffers while also
        /// scaling a preview image.
        ///
        /// Note that this method can't handle null argument!
        /// </summary>
        /// <param name="file">The selected image file.</param>
        /// <returns>True if successful, false otherwise.</returns>
        private async Task <bool> HandleSelectedImageFileAsync(StorageFile file)
        {
            System.Diagnostics.Debug.WriteLine(DebugTag + "HandleSelectedImageFile(): " + file.Name);
            var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

            DataContext dataContext = DataContext.Instance;

            // Reset the streams
            dataContext.ResetStreams();

            var image = new BitmapImage();

            image.SetSource(fileStream);
            int width  = image.PixelWidth;
            int height = image.PixelHeight;

            dataContext.SetFullResolution(width, height);

            int previewWidth  = (int)FilterEffects.DataContext.DefaultPreviewResolutionWidth;
            int previewHeight = 0;

            AppUtils.CalculatePreviewResolution(width, height, ref previewWidth, ref previewHeight);
            dataContext.SetPreviewResolution(previewWidth, previewHeight);

            bool success = false;

            try
            {
                // JPEG images can be used as such
                Stream stream = fileStream.AsStream();
                stream.Position = 0;
                stream.CopyTo(dataContext.FullResolutionStream);
                success = true;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(DebugTag
                                                   + "Cannot use stream as such (not probably in JPEG format): " + e.Message);
            }

            if (!success)
            {
                try
                {
                    await AppUtils.FileStreamToJpegStreamAsync(fileStream,
                                                               (IRandomAccessStream)dataContext.FullResolutionStream.AsInputStream());

                    success = true;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(DebugTag
                                                       + "Failed to convert the file stream content into JPEG format: "
                                                       + e.ToString());
                }
            }

            if (success)
            {
                await AppUtils.ScaleImageStreamAsync(
                    dataContext.FullResolutionStream,
                    dataContext.FullResolution,
                    dataContext.PreviewResolutionStream,
                    dataContext.PreviewResolution);

                dataContext.WasCaptured = false;
            }

            return(success);
        }