win8 metro 調用網路攝影機錄製視頻並將視頻儲存在相應的位置

來源:互聯網
上載者:User

標籤:camera metro

上一篇文章介紹了在win8 metro 調用網路攝影機拍攝照片並將照片儲存在相應的位置的功能,那麼這一片文章主要介紹了的就是錄製視頻了,其實這個差不多,所用的思想也是一樣的,因為圖片和視頻都可以轉化為流檔案,那麼它們本質上都是一樣的,但是還是有個別地方時不同的,接下來我們就介紹一下這個別地方的不同吧

下面是metro UI的代碼:

<Page    x:Class="Camera1.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:Camera1"    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}">        <Button x:Name="btnCamera" Content="開啟網路攝影機" HorizontalAlignment="Left" Margin="67,179,0,0" VerticalAlignment="Top" Click="btnCamera_Click"/>        <MediaElement x:Name="media" HorizontalAlignment="Left" Height="604" Margin="273,45,0,0" VerticalAlignment="Top" Width="746"/>        <Button x:Name="btnSave" Content="儲存視頻" HorizontalAlignment="Left" Margin="67,316,0,0" VerticalAlignment="Top" Click="btnSave_Click"/>        <TextBox x:Name="txtBox1" HorizontalAlignment="Left" Margin="70,440,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>    </Grid></Page>

下面是UI的控制碼:

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.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.UI.Xaml.Media.Imaging;using Windows.Storage;using Windows.Storage.Streams;using Windows.Storage.Pickers;// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238namespace Camera1{    /// <summary>    /// An empty page that can be used on its own or navigated to within a Frame.    /// </summary>    public sealed partial class MainPage : Page    {        private StorageFile file = null;        public MainPage()        {            this.InitializeComponent();        }        private async void btnCamera_Click(object sender, RoutedEventArgs e)        {            CameraCaptureUI dialog = new CameraCaptureUI();            dialog.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4;            file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Video);            if (file != null)            {                IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);                media.SetSource(fileStream,file.ContentType);            }        }        private async void btnSave_Click(object sender, RoutedEventArgs e)        {                            FileSavePicker picker = new FileSavePicker();                picker.CommitButtonText = "儲存";                picker.SuggestedFileName = "hello";                picker.FileTypeChoices.Add("圖片",new string[]{".mp4",".avi",".mkv",".mpg",".rmvb"});                picker.SuggestedStartLocation = PickerLocationId.VideosLibrary;                StorageFile filePath = await picker.PickSaveFileAsync();                if (filePath != null)                {                    var streamRandom = await file.OpenAsync(FileAccessMode.Read);                    IBuffer buffer = RandomAccessStreamToBuffer(streamRandom);                    await FileIO.WriteBufferAsync(filePath, buffer);                }                    }        //將視頻寫入到緩衝區        private IBuffer RandomAccessStreamToBuffer(IRandomAccessStream randomstream)        {            Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomstream.GetInputStreamAt(0));            MemoryStream memoryStream = new MemoryStream();            if (stream != null)            {                byte[] bytes = ConvertStreamTobyte(stream);                if (bytes != null)                {                    var binaryWriter = new BinaryWriter(memoryStream);                    binaryWriter.Write(bytes);                }            }            IBuffer buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (int)memoryStream.Length);            return buffer;        }        //將流轉換成二進位        public static byte[] ConvertStreamTobyte(Stream input)        {            byte[] buffer = new byte[1024 * 1024];            using (MemoryStream ms = new MemoryStream())            {                int read;                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)                {                    ms.Write(buffer, 0, read);                }                return ms.ToArray();            }        }    }}

程式完全可以運行,希望大家不吝賜教。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.