新時尚Windows8開發(28):多媒體捕捉(低級篇)

來源:互聯網
上載者:User

先解釋一下啥是“多媒體捕捉”,這隻是一種直譯,說得通俗一點,就是希望應用程式可以調用網路攝影機拍攝“豔照”或者錄製視頻。比如美圖秀秀可以處理你已經儲存的圖片,但也可能讓你現場來“秀”一下。如果我們需要在應用程式中讓使用者即時拍攝照片,這個功能就顯得必須了。

比如,你想弄個偷拍程式,當然,這是做夢,因為出於安全和尊重使用者的隱私考慮,Win8“板磚”應用在使用網路攝影機之前會先問一下使用者:“我能用用你的網路攝影機嗎?”如果使用者說:“OK,木有問題,那就調用。”而如果使用者說:“靠,滾!”那你這個功能只能一邊站了。

 

那為什麼叫“低級篇”呢?因為我們本節只用到系統預設帶的拍攝UI,已經封裝好了,用起來很輕鬆,如果你的應用只是簡單的拍攝,就優先考慮這個。因為這世界上只有傻X才會把簡單的事情複雜化。

 

那麼這具組件在哪裡?按名字找,嘿嘿,找著了,在命名空間Windows.Media.Capture下,她的大名叫CameraCaptureUI,後面帶有UI兩個字母,你都猜到她是誰了。

在“物件瀏覽器”認真研究一下這位MM,你發現,她太單純了,兩個分別用來設定照片和視頻參數的屬性,即PhotoSettings和VideoSettings。而好的思想更簡單,要拍攝嗎?請調用CaptureFileAsync方法吧,看方法名,是非同步,用的時候記得不要忘了await關鍵字啊。

純真的女孩就是可愛,如果你只需要單純的拍攝功能,不要猶豫了,果斷選擇她吧。

 

有句話叫“知行合一”,還有一句話叫“天人合一”,所以,光嘴巴上吹也不行,親,別光坐著,動手幹活!

 

1、啟動VS ,新項一個空白頁面應用。

2、首頁的布局我直接貼代碼,因為我不相信你看不懂。

【XAML】

<Page    x:Class="Example27.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:Example27"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d">    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">        <Grid.ColumnDefinitions>            <ColumnDefinition Width="auto"/>            <ColumnDefinition Width="*"/>        </Grid.ColumnDefinitions>        <Canvas Grid.Column="0" Width="180">            <Button Canvas.Left="13" Canvas.Top="20" Padding="10,8" Content="拍照" Click="onClick" FontSize="38"/>            <StackPanel Orientation="Vertical" Canvas.Left="15" Canvas.Top="140">                <RadioButton Content="照片" GroupName="setting" IsChecked="True" Checked="onRadChecked"/>                <RadioButton Content="視頻" GroupName="setting" Checked="onRadChecked"/>            </StackPanel>        </Canvas>        <Image Grid.Column="1" Margin="15" x:Name="imgPhoto" Stretch="Uniform"/>        <MediaElement Grid.Column="1" x:Name="me" Margin="18" AutoPlay="False" Visibility="Collapsed" CurrentStateChanged="me_CurrentStateChanged_1"/>    </Grid></Page>

後台代碼嘛,我就先帖後述吧。

【C#】

using System;using System.Collections.Generic;using System.IO;using System.Linq;using Windows.Foundation;using Windows.Foundation.Collections;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;using Windows.Media.Capture;using Windows.Storage;using Windows.Storage.Streams;using Windows.UI.Xaml.Media.Imaging;namespace Example27{    /// <summary>    /// 可用於自身或導航至 Frame 內部的空白頁。    /// </summary>    public sealed partial class MainPage : Page    {        public MainPage()        {            this.InitializeComponent();        }        /// <summary>        /// 捕捉模式        /// </summary>        CameraCaptureUIMode CaptureMode = CameraCaptureUIMode.Photo;        private void onRadChecked(object sender, RoutedEventArgs e)        {            RadioButton rdb = sender as RadioButton;            if (rdb != null)            {                string content = rdb.Content as string;                if (content == "照片")                {                    this.CaptureMode = CameraCaptureUIMode.Photo;                }                else                {                    this.CaptureMode = CameraCaptureUIMode.Video;                }            }        }        private async void onClick(object sender, RoutedEventArgs e)        {            CameraCaptureUI ui = new CameraCaptureUI();            // 照片用PNG格式,視頻用WMV格式吧            ui.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;            ui.VideoSettings.Format = CameraCaptureUIVideoFormat.Wmv;            // Action,開始偷拍            StorageFile file = await ui.CaptureFileAsync(this.CaptureMode);            if (file != null)            {                if (this.CaptureMode == CameraCaptureUIMode.Photo)                {                    BitmapImage bmp = new BitmapImage();                    bmp.SetSource(await file.OpenAsync(FileAccessMode.Read));                    this.imgPhoto.Source = bmp;                    this.imgPhoto.Visibility = Windows.UI.Xaml.Visibility.Visible;                    this.me.Visibility = Windows.UI.Xaml.Visibility.Collapsed;                }                else                {                    me.SetSource(await file.OpenAsync(FileAccessMode.Read), file.ContentType);                    this.imgPhoto.Visibility = Windows.UI.Xaml.Visibility.Collapsed;                    this.me.Visibility = Windows.UI.Xaml.Visibility.Visible;                }            }        }        private void me_Tapped(object sender, TappedRoutedEventArgs e)        {            if (me.CurrentState == MediaElementState.Playing)            {                me.Pause();            }            else            {                me.Play();            }        }        /*         * 如果MediaElement中的多媒體可用,就附加Tapped事件處理,只要點擊就會播放視頻;         * 如MediaElement中的多媒體不可用,便解除Tapped事件的處理綁定         */        private void me_CurrentStateChanged_1(object sender, RoutedEventArgs e)        {            switch (this.me.CurrentState)            {                case MediaElementState.Closed:                    this.me.Tapped -= me_Tapped;                    break;                case MediaElementState.Opening:                    this.me.Tapped += me_Tapped;                    break;            }        }    }}

 

注意,核心代碼我再Copy一下。

            CameraCaptureUI ui = new CameraCaptureUI();            // 照片用PNG格式,視頻用WMV格式吧            ui.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;            ui.VideoSettings.Format = CameraCaptureUIVideoFormat.Wmv;            // Action,開始偷拍            StorageFile file = await ui.CaptureFileAsync(this.CaptureMode);

在調用CaptureFileAsync方法時,還有一個參數,是CameraCaptureUIMode枚舉,它就是告訴系統,我是要拍照片還是錄製視頻,或者是兩個都要。

 

別急,現在還不能運行,不然會XXX,我們還有一件事要做。

 

3、開啟資訊清單檔,切換到 功能 選項卡,把麥克風和網路攝影機勾上。

 

好了,現在運行就沒問題了,開始光明正大地偷拍吧。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.