User Tools

Site Tools


notes:uwp:examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:uwp:examples [2017/03/08]
admin [Master/details view]
notes:uwp:examples [2019/04/20] (current)
leszek [Capture an image from a camera]
Line 66: Line 66:
  
  
- 
-=====Capture an image from a camera===== 
- 
-Method #1: 
- 
-<code xml> 
-<Page x:​Class="​TestApp.MainPage"​ 
-      xmlns="​http://​schemas.microsoft.com/​winfx/​2006/​xaml/​presentation"​ 
-      xmlns:​x="​http://​schemas.microsoft.com/​winfx/​2006/​xaml">​ 
-    <​StackPanel>​ 
-        <Image x:​Name="​Image1"​ /> 
-        <Button Content="​Capture Image" Click="​CaptureImageButton_Click"​ /> 
-    </​StackPanel>​ 
-</​Page>​ 
-</​code>​ 
- 
-<code csharp> 
-using System; 
-using System.Threading.Tasks;​ 
-using Windows.Media.Capture;​ 
-using Windows.Storage;​ 
-using Windows.Storage.Streams;​ 
-using Windows.UI.Xaml;​ 
-using Windows.UI.Xaml.Controls;​ 
-using Windows.UI.Xaml.Media.Imaging;​ 
- 
-namespace TestApp 
-{ 
-    public sealed partial class MainPage : Page 
-    { 
-        public MainPage() 
-        { 
-            this.InitializeComponent();​ 
-        } 
- 
-        private async void CaptureImageButton_Click(object sender, RoutedEventArgs e) 
-        { 
-            await CaptureImage();​ 
-        } 
- 
-        private async Task CaptureImage() 
-        { 
-            CameraCaptureUI cameraUI = new CameraCaptureUI();​ 
-            BitmapImage image = new BitmapImage();​ 
-            Image1.Source = image; 
- 
-            cameraUI.PhotoSettings.AllowCropping = true; 
-            cameraUI.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable;​ 
-            cameraUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;​ 
- 
-            // Wait for a user to capture a photo. 
-            StorageFile tempFile = await cameraUI.CaptureFileAsync(CameraCaptureUIMode.Photo);​ 
-            if (tempFile != null) 
-            { 
-                // Transfer the captured photo to a stream. 
-                IRandomAccessStreamWithContentType stream = await tempFile.OpenReadAsync();​ 
- 
-                // Pass the stream to a BitmapImage. The BitmapImage is the source of an Image XAML element. 
-                await image.SetSourceAsync(stream);​ 
-                ​ 
-                // Store the photo in the local folder. 
-                StorageFolder folder = ApplicationData.Current.LocalFolder;​ 
-                var file = await tempFile.CopyAsync(folder,​ "​picture.jpg",​ NameCollisionOption.ReplaceExisting);​ 
-            } 
-        } 
-    } 
-} 
-</​code>​ 
- 
-Method #2: This method uses //​SoftwareBitmap//​ which you can use to perform some post-processing on the captured image: 
- 
-<code csharp> 
-using System; 
-using System.Threading.Tasks;​ 
-using Windows.Foundation;​ 
-using Windows.Graphics.Imaging;​ 
-using Windows.Media.Capture;​ 
-using Windows.Storage;​ 
-using Windows.Storage.Streams;​ 
-using Windows.UI.Xaml;​ 
-using Windows.UI.Xaml.Controls;​ 
-using Windows.UI.Xaml.Media.Imaging;​ 
- 
-namespace TestApp 
-{ 
-    public sealed partial class MainPage : Page 
-    { 
-        public MainPage() 
-        { 
-            this.InitializeComponent();​ 
-        } 
- 
-        private async void CaptureImageButton_Click(object sender, RoutedEventArgs e) 
-        { 
-            await CaptureImage();​ 
-        } 
- 
-        private async Task CaptureImage() 
-        { 
-            CameraCaptureUI cameraUI = new CameraCaptureUI();​ 
- 
-            cameraUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200); 
-            cameraUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;​ 
- 
-            // Wait for a user to capture a photo. 
-            StorageFile file = await cameraUI.CaptureFileAsync(CameraCaptureUIMode.Photo);​ 
- 
-            if (file != null) 
-            { 
-                IRandomAccessStream stream = await file.OpenReadAsync();​ 
- 
-                // -or- 
-                // using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read) { ... } 
- 
-                // The Image control requires that the image source be in the BGRA8 format 
-                // with pre-multiplied alpha or no alpha channel. We need to create a software 
-                // bitmap with the desired format. 
-                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);​ // decode the image file stream 
-                SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync();​ 
-                SoftwareBitmap bitmapBGR8 = SoftwareBitmap.Convert(bitmap, ​ 
-                    BitmapPixelFormat.Bgra8, ​ 
-                    BitmapAlphaMode.Premultiplied);​ 
- 
-                SoftwareBitmapSource source = new SoftwareBitmapSource();​ 
-                await source.SetBitmapAsync(bitmapBGR8);​ 
-                Image1.Source = source; 
-            } 
-        } 
-    } 
-} 
-</​code>​ 
- 
-**Win10 update**: In UWP apps, you can use the //​SoftwareBitmapSource//​ type as a XAML image source. This lets you pass un-encoded images to the XAML framework to be immediately displayed on screen, bypassing image decoding by the XAML framework. 
- 
-More information can be found [[https://​docs.microsoft.com/​en-us/​windows/​uwp/​audio-video-camera/​capture-photos-and-video-with-cameracaptureui|here]]. 
  
  
notes/uwp/examples.txt ยท Last modified: 2019/04/20 by leszek