/// <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 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)); } }
private async void SelectImageButton_Click(object sender, RoutedEventArgs e) { await _photoCaptureManager.StopPreviewAsync(); var openPicker = new Windows.Storage.Pickers.FileOpenPicker { SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary, ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail }; // Filter to include a sample subset of file types openPicker.FileTypeFilter.Clear(); foreach (string postfix in _supportedImageFilePostfixes) { openPicker.FileTypeFilter.Add(postfix); } // Open the file picker Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync(); // File is null if user cancels the file picker if (file != null) { var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); // Reset the streams _dataContext.ResetStreams(); var image = new BitmapImage(); image.SetSource(fileStream); int width = image.PixelWidth; int height = image.PixelHeight; var bitmap = new WriteableBitmap(width, height); _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 ex) { Debug.WriteLine(DebugTag + "Cannot use stream as such (not probably jpeg): " + ex.Message); } if (!success) { // TODO: Test this part! It may not work. // // Image format is not jpeg. Can be anything, so first // load it into a bitmap image and then write as jpeg. bitmap.SetSource(fileStream); var inStream = (IRandomAccessStream)_dataContext.FullResolutionStream.AsInputStream(); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, inStream); Stream outStream = bitmap.PixelBuffer.AsStream(); var pixels = new byte[outStream.Length]; await outStream.ReadAsync(pixels, 0, pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)width, (uint)height, 96.0, 96.0, pixels); await encoder.FlushAsync(); } await AppUtils.ScaleImageStreamAsync( _dataContext.FullResolutionStream, _dataContext.FullResolution, _dataContext.PreviewResolutionStream, _dataContext.PreviewResolution); _dataContext.WasCaptured = false; Frame.Navigate(typeof(PreviewPage)); } // if (file != null) }
/// <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); }