win8 metro 自己寫攝像頭錄像項目
阿新 • • 發佈:2017-07-16
進制 fonts text back 應該 record -s find inf
可是這裏出現了一個問題,不知道怎麽解決。
上面就是捕獲的異常情況。能夠從異常的截圖上發現,出現異常的代碼可能是:
應該就是標記為紅色的那部分代碼,以下我對它進行斷點調試:
這是要求不適用CameraCaptureUI等使用系統自帶的 camera UI界面。要求我們自己寫調用攝像頭攝像的方法,如今我把我的程序貼下:
UI界面的程序:
<Page x:Class="Camera3.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Camera3" 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="295,83,0,0" VerticalAlignment="Top" Click="btnCamera_Click"/> <Button x:Name="btnSettings" Content="攝像頭設置" HorizontalAlignment="Left" Margin="482,83,0,0" VerticalAlignment="Top"/> <Button x:Name="btnVideo" Content="拍攝視頻" HorizontalAlignment="Left" Margin="685,83,0,0" VerticalAlignment="Top" Click="btnVideo_Click"/> <Button x:Name="btnSave" Content="保存視頻" HorizontalAlignment="Left" Margin="867,83,0,0" VerticalAlignment="Top" Click="btnSave_Click"/> <GridView HorizontalAlignment="Left" Margin="246,200,0,0" VerticalAlignment="Top" Width="800" Height="600"> <CaptureElement x:Name="capture1" Height="600" Width="800"/> </GridView> </Grid> </Page>
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.Storage; using Windows.Storage.Pickers; using Windows.Devices.Enumeration; using Windows.Media.MediaProperties; using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Media; using Windows.Storage.Streams; using Windows.Media.Devices; using System.Threading.Tasks; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 namespace Camera3 { /// <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 MediaCapture mediaVideo = null; private IStorageFile video = null; private MediaEncodingProfile videoProfile = null; public MainPage() { this.InitializeComponent(); } public async void btnCamera_Click(object sender, RoutedEventArgs e) { try { DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); if (devices.Count > 0) { if (mediaVideo == null) { capture1.Source = await Initialize(); await mediaVideo.StartPreviewAsync(); } } } catch (Exception msg) { mediaVideo = null; } } public async Task<MediaCapture> Initialize() { mediaVideo = new MediaCapture(); await mediaVideo.InitializeAsync(); mediaVideo.VideoDeviceController.PrimaryUse = CaptureUse.Video; videoProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); return mediaVideo; } public async void btnVideo_Click(object sender, RoutedEventArgs e) { if (mediaVideo != null) { video = await KnownFolders.VideosLibrary.CreateFileAsync("some.mp4", CreationCollisionOption.GenerateUniqueName); await mediaVideo.StartRecordToStorageFileAsync(videoProfile, video); } } private async void btnSave_Click(object sender, RoutedEventArgs e) { if (video != null) { FileSavePicker videoPicker = new FileSavePicker(); videoPicker.CommitButtonText = "保存視頻"; videoPicker.SuggestedFileName = "hello"; videoPicker.FileTypeChoices.Add("視頻", new string[] { ".mp4", ".mpg", ".rmvb", ".mkv" }); videoPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary; IStorageFile videoFile = await videoPicker.PickSaveFileAsync(); if (videoFile != null) { var streamRandom = await video.OpenAsync(FileAccessMode.Read); IBuffer buffer = RandomAccessStreamToBuffer(streamRandom); await FileIO.WriteBufferAsync(videoFile, buffer); } } } //將圖片寫入到緩沖區 private IBuffer RandomAccessStreamToBuffer(IRandomAccessStream randomstream) { Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomstream.GetInputStreamAt(0)); MemoryStream memoryStream = new MemoryStream(); IBuffer buffer = null; if (stream != null) { byte[] bytes = ConvertStreamTobyte(stream); //將流轉化為字節型數組 if (bytes != null) { var binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(bytes); } } 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(); } } } }
可是這裏出現了一個問題,不知道怎麽解決。
之所以放上來。希望有大牛能夠幫我解決一下,看看究竟是出現了什麽問題:
這是執行的界面。點擊“調用攝像頭”。能夠調用攝像頭:
會發現上面的界面已經調用了攝像頭,這一個模塊式沒有什麽問題的。
可是問題出如今以下,以下我點擊“拍攝視頻”的button。出現例如以下異常:
以下我把我捕獲的異常給大家看看:
System.Exception: The specified object or value does not exist. MediaStreamType at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Camera3.MainPage.<btnVideo_Click>d__9.MoveNext()
上面就是捕獲的異常情況。能夠從異常的截圖上發現,出現異常的代碼可能是:
public async void btnVideo_Click(object sender, RoutedEventArgs e) { if (mediaVideo != null) { video = await KnownFolders.VideosLibrary.CreateFileAsync("some.mp4", CreationCollisionOption.GenerateUniqueName); <span style="color:#ff0000;">await mediaVideo.StartRecordToStorageFileAsync(videoProfile, video);</span> } }
應該就是標記為紅色的那部分代碼,以下我對它進行斷點調試:
上面兩幅斷點調試的圖片能夠看出這
<span style="color:#ff0000;">StartRecordToStorageFileAsync(videoProfile, video)</span>這個函數的兩個參數均沒有出現故障,參數的傳遞的正確的。如今怎麽會出現這種情況。
希望知道的同學能夠給我答案。告訴我為什麽,等待您的回復,謝謝同誌們。
PS:搞了一下午,最終發現了這個程序的問題。如今把正確的程序源碼傳上去。
以下是我的程序源碼,希望打啊多多指正。
http://download.csdn.net/detail/litianpeng1991/7556273
win8 metro 自己寫攝像頭錄像項目