Esempio n. 1
0
        /// <summary>
        /// Asynchronously saves the <paramref name="photo" /> given to the camera roll album.
        /// </summary>
        /// <param name="photo">Photo to save.</param>
        /// <returns>Image with thumbnail.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="photo"/> is <see langword="null"/>.</exception>
        public async Task<IThumbnailedImage> SaveResultToCameraRollAsync(ICapturedPhoto photo)
        {
            if (photo == null)
            {
                throw new ArgumentNullException("photo");
            }

            Tracing.Trace("StorageService: Trying to save picture to the Camera Roll.");

            Picture cameraRollPicture;
            string name = this.GeneratePhotoName();

            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                // Convert image to JPEG format and rotate in accordance with the original photo orientation.
                Tracing.Trace("StorageService: Converting photo to JPEG format. Size: {0}x{1}, Rotation: {2}", photo.Width, photo.Height, photo.Rotation);
                byte[] pixelData = await photo.DetachPixelDataAsync();

                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                encoder.BitmapTransform.Rotation = photo.Rotation;
                encoder.BitmapTransform.Flip     = photo.Flip;
                encoder.IsThumbnailGenerated     = true;

                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, photo.Width, photo.Height, Constants.DefaultDpiX, Constants.DefaultDpiY, pixelData);
                await encoder.FlushAsync();

                cameraRollPicture = this.lazyMediaLibrary.Value.SavePictureToCameraRoll(name, stream.AsStream());
            }

            Tracing.Trace("StorageService: Saved to Camera Roll as {0}", name);

            return new MediaLibraryThumbnailedImage(cameraRollPicture);
        }
        /// <summary>
        /// Asynchronously saves the <paramref name="photo" /> given to the camera roll album.
        /// </summary>
        /// <param name="photo">Photo to save.</param>
        /// <returns>Image with thumbnail.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="photo"/> is <see langword="null"/>.</exception>
        public async Task <IThumbnailedImage> SaveResultToCameraRollAsync(ICapturedPhoto photo)
        {
            if (photo == null)
            {
                throw new ArgumentNullException("photo");
            }

            Tracing.Trace("StorageService: Trying to save picture to the Camera Roll.");

            Picture cameraRollPicture;
            string  name = this.GeneratePhotoName();

            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                // Convert image to JPEG format and rotate in accordance with the original photo orientation.
                Tracing.Trace("StorageService: Converting photo to JPEG format. Size: {0}x{1}, Rotation: {2}", photo.Width, photo.Height, photo.Rotation);
                byte[] pixelData = await photo.DetachPixelDataAsync();

                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

                encoder.BitmapTransform.Rotation = photo.Rotation;
                encoder.BitmapTransform.Flip     = photo.Flip;
                encoder.IsThumbnailGenerated     = true;

                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, photo.Width, photo.Height, Constants.DefaultDpiX, Constants.DefaultDpiY, pixelData);
                await encoder.FlushAsync();

                cameraRollPicture = this.lazyMediaLibrary.Value.SavePictureToCameraRoll(name, stream.AsStream());
            }

            Tracing.Trace("StorageService: Saved to Camera Roll as {0}", name);

            return(new MediaLibraryThumbnailedImage(cameraRollPicture));
        }
Esempio n. 3
0
        /// <summary>
        /// The <see cref="PhotoCamera.PhotoCaptured"/> event handler.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="capturedPhoto">Captured photo.</param>
        private void CameraOnPhotoCaptured(object sender, ICapturedPhoto capturedPhoto)
        {
            // In our application we capture only single frame, so we can update view model state in
            // the current method.
            // If your application captures multiple frames, you may want to modify the 'CameraOnCaptureStopped'
            // method or add additional logic here.
            DispatcherHelper.BeginInvokeOnUIThread(async() =>
            {
                this.State = MainPageViewModelState.SavingCaptureResult;
                this.NotifyFocusStateChanged(new FocusStateChangedEventArgs(FocusState.Unlocked));

                try
                {
                    using (capturedPhoto)
                    {
                        IThumbnailedImage image = await this.storageService.SaveResultToCameraRollAsync(capturedPhoto);

                        // Get the preview image.
                        BitmapImage source = new BitmapImage();
                        source.SetSource(image.GetThumbnailImage());

                        this.Items.Add(image);
                        this.ReviewImageBrush = new ImageBrush {
                            ImageSource = source
                        };
                    }
                }
                finally
                {
                    this.State = MainPageViewModelState.Loaded;
                }
            });
        }
        /// <summary>
        /// Notifies the <see cref="PhotoCaptured"/> event listeners.
        /// </summary>
        /// <param name="frame">Captured frame.</param>
        private void NotifyPhotoCaptured(ICapturedPhoto frame)
        {
            EventHandler <ICapturedPhoto> handler = this.PhotoCaptured;

            if (handler != null)
            {
                handler.Invoke(this, frame);
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Notifies the <see cref="PhotoCaptured"/> event listeners.
 /// </summary>
 /// <param name="frame">Captured frame.</param>
 private void NotifyPhotoCaptured(ICapturedPhoto frame)
 {
     EventHandler<ICapturedPhoto> handler = this.PhotoCaptured;
     if (handler != null)
     {
         handler.Invoke(this, frame);
     }
 }
        /// <summary>
        /// The <see cref="PhotoCamera.PhotoCaptured"/> event handler.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="capturedPhoto">Captured photo.</param>
        private void CameraOnPhotoCaptured(object sender, ICapturedPhoto capturedPhoto)
        {
            // In our application we capture only single frame, so we can update view model state in
            // the current method.
            // If your application captures multiple frames, you may want to modify the 'CameraOnCaptureStopped'
            // method or add additional logic here.
            DispatcherHelper.BeginInvokeOnUIThread(async () =>
            {
                this.State = MainPageViewModelState.SavingCaptureResult;
                this.NotifyFocusStateChanged(new FocusStateChangedEventArgs(FocusState.Unlocked));

                try
                {
                    using (capturedPhoto)
                    {
                        IThumbnailedImage image = await this.storageService.SaveResultToCameraRollAsync(capturedPhoto);

                        // Get the preview image.
                        BitmapImage source = new BitmapImage();
                        source.SetSource(image.GetThumbnailImage());

                        this.Items.Add(image);
                        this.ReviewImageBrush = new ImageBrush { ImageSource = source };
                    }
                }
                finally
                {
                    this.State = MainPageViewModelState.Loaded;
                }
            });
        }