10 April 2017

Windows Store và Graphics: Camera Capture trên App Windows Store C#

MainPage.xaml
Windows Store 2017
<Page
    x:Class="CaptureDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CaptureDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <CaptureElement x:Name="capture" HorizontalAlignment="Left" Height="336" Margin="135,155,0,0" VerticalAlignment="Top" Width="680"/>
        <Button x:Name="btn_start" Content="Start Camera" HorizontalAlignment="Left" Margin="242,577,0,0" VerticalAlignment="Top" Click="btn_start_Click"/>
        <Button x:Name="btn_capture" Content="Capture and Save" HorizontalAlignment="Left" Margin="592,577,0,0" VerticalAlignment="Top" Click="btn_capture_Click"/>

    </Grid>
</Page>
Cấp quyền Camera
MainPage.xaml.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Capture;
using Windows.Media.MediaProperties;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace CaptureDemo
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void btn_start_Click(object sender, RoutedEventArgs e)
        {
            //Start Camera
            MediaCapture captureManager = new MediaCapture();
            await captureManager.InitializeAsync();
            capture.Source = captureManager;
            await captureManager.StartPreviewAsync();
        }

        private async void btn_capture_Click(object sender, RoutedEventArgs e)
        {
            //Capture image
            Windows.Media.Capture.MediaCapture takePhotoManager = new Windows.Media.Capture.MediaCapture();
            await takePhotoManager.InitializeAsync();
            //Save file pic-001.jpg
            ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("pic-001.jpg", CreationCollisionOption.ReplaceExisting);
            await takePhotoManager.CapturePhotoToStorageFileAsync(imgFormat, file);
        }
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang