这是要求不适用CameraCaptureUI等使用系统自带的 camera  UI界面。要求我们自己写调用摄像头摄像的方法,如今我把我的程序贴下:

UI界面的程序:

  1. <Page
  2. x:Class="Camera3.MainPage"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Camera3"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  11. <Button x:Name="btnCamera" Content="调用摄像头" HorizontalAlignment="Left" Margin="295,83,0,0" VerticalAlignment="Top" Click="btnCamera_Click"/>
  12. <Button x:Name="btnSettings" Content="摄像头设置" HorizontalAlignment="Left" Margin="482,83,0,0" VerticalAlignment="Top"/>
  13. <Button x:Name="btnVideo" Content="拍摄视频" HorizontalAlignment="Left" Margin="685,83,0,0" VerticalAlignment="Top" Click="btnVideo_Click"/>
  14. <Button x:Name="btnSave" Content="保存视频" HorizontalAlignment="Left" Margin="867,83,0,0" VerticalAlignment="Top" Click="btnSave_Click"/>
  15. <GridView HorizontalAlignment="Left" Margin="246,200,0,0" VerticalAlignment="Top" Width="800" Height="600">
  16. <CaptureElement x:Name="capture1" Height="600" Width="800"/>
  17. </GridView>
  18.  
  19. </Grid>
  20. </Page>

主程序里的代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using Windows.Foundation;
  7. using Windows.Foundation.Collections;
  8. using Windows.UI.Xaml;
  9. using Windows.UI.Xaml.Controls;
  10. using Windows.UI.Xaml.Controls.Primitives;
  11. using Windows.UI.Xaml.Data;
  12. using Windows.UI.Xaml.Input;
  13. using Windows.UI.Xaml.Media;
  14. using Windows.UI.Xaml.Navigation;
  15.  
  16. using Windows.Media.Capture;
  17. using Windows.Storage;
  18. using Windows.Storage.Pickers;
  19. using Windows.Devices.Enumeration;
  20. using Windows.Media.MediaProperties;
  21. using Windows.UI.Xaml.Media.Imaging;
  22. using Windows.UI.Xaml.Media;
  23. using Windows.Storage.Streams;
  24. using Windows.Media.Devices;
  25. using System.Threading.Tasks;
  26.  
  27. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  28.  
  29. namespace Camera3
  30. {
  31. /// <summary>
  32. /// An empty page that can be used on its own or navigated to within a Frame.
  33. /// </summary>
  34. public sealed partial class MainPage : Page
  35. {
  36. private MediaCapture mediaVideo = null;
  37. private IStorageFile video = null;
  38. private MediaEncodingProfile videoProfile = null;
  39. public MainPage()
  40. {
  41. this.InitializeComponent();
  42. }
  43.  
  44. public async void btnCamera_Click(object sender, RoutedEventArgs e)
  45. {
  46. try
  47. {
  48. DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
  49. if (devices.Count > 0)
  50. {
  51. if (mediaVideo == null)
  52. {
  53.  
  54. capture1.Source = await Initialize();
  55. await mediaVideo.StartPreviewAsync();
  56.  
  57. }
  58. }
  59. }
  60. catch (Exception msg)
  61. {
  62. mediaVideo = null;
  63. }
  64. }
  65.  
  66. public async Task<MediaCapture> Initialize()
  67. {
  68. mediaVideo = new MediaCapture();
  69. await mediaVideo.InitializeAsync();
  70. mediaVideo.VideoDeviceController.PrimaryUse = CaptureUse.Video;
  71. videoProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
  72. return mediaVideo;
  73. }
  74.  
  75. public async void btnVideo_Click(object sender, RoutedEventArgs e)
  76. {
  77. if (mediaVideo != null)
  78. {
  79. video = await KnownFolders.VideosLibrary.CreateFileAsync("some.mp4", CreationCollisionOption.GenerateUniqueName);
  80. await mediaVideo.StartRecordToStorageFileAsync(videoProfile, video);
  81. }
  82.  
  83. }
  84.  
  85. private async void btnSave_Click(object sender, RoutedEventArgs e)
  86. {
  87. if (video != null)
  88. {
  89. FileSavePicker videoPicker = new FileSavePicker();
  90. videoPicker.CommitButtonText = "保存视频";
  91. videoPicker.SuggestedFileName = "hello";
  92. videoPicker.FileTypeChoices.Add("视频", new string[] { ".mp4", ".mpg", ".rmvb", ".mkv" });
  93. videoPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
  94. IStorageFile videoFile = await videoPicker.PickSaveFileAsync();
  95.  
  96. if (videoFile != null)
  97. {
  98. var streamRandom = await video.OpenAsync(FileAccessMode.Read);
  99. IBuffer buffer = RandomAccessStreamToBuffer(streamRandom);
  100. await FileIO.WriteBufferAsync(videoFile, buffer);
  101. }
  102.  
  103. }
  104. }
  105. //将图片写入到缓冲区
  106. private IBuffer RandomAccessStreamToBuffer(IRandomAccessStream randomstream)
  107. {
  108. Stream stream = WindowsRuntimeStreamExtensions.AsStreamForRead(randomstream.GetInputStreamAt(0));
  109. MemoryStream memoryStream = new MemoryStream();
  110. IBuffer buffer = null;
  111. if (stream != null)
  112. {
  113. byte[] bytes = ConvertStreamTobyte(stream); //将流转化为字节型数组
  114. if (bytes != null)
  115. {
  116. var binaryWriter = new BinaryWriter(memoryStream);
  117. binaryWriter.Write(bytes);
  118. }
  119. }
  120.  
  121. buffer = WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream, 0, (int)memoryStream.Length);
  122.  
  123. return buffer;
  124. }
  125.  
  126. //将流转换成二进制
  127. public static byte[] ConvertStreamTobyte(Stream input)
  128. {
  129. byte[] buffer = new byte[1024 * 1024];
  130. using (MemoryStream ms = new MemoryStream())
  131. {
  132. int read;
  133. while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
  134. {
  135. ms.Write(buffer, 0, read);
  136. }
  137. return ms.ToArray();
  138. }
  139. }
  140.  
  141. }
  142. }

可是这里出现了一个问题,不知道怎么解决。

之所以放上来。希望有大牛能够帮我解决一下,看看究竟是出现了什么问题:

这是执行的界面。点击“调用摄像头”。能够调用摄像头:

会发现上面的界面已经调用了摄像头,这一个模块式没有什么问题的。

可是问题出如今以下,以下我点击“拍摄视频”的button。出现例如以下异常:

以下我把我捕获的异常给大家看看:

  1. System.Exception: The specified object or value does not exist.
  2. MediaStreamType
  3. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  4. at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  5. at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
  6. at Camera3.MainPage.<btnVideo_Click>d__9.MoveNext()

上面就是捕获的异常情况。能够从异常的截图上发现,出现异常的代码可能是:

  1. public async void btnVideo_Click(object sender, RoutedEventArgs e)
  2. {
  3. if (mediaVideo != null)
  4. {
  5. video = await KnownFolders.VideosLibrary.CreateFileAsync("some.mp4", CreationCollisionOption.GenerateUniqueName);
  6. <span style="color:#ff0000;">await mediaVideo.StartRecordToStorageFileAsync(videoProfile, video);</span>
  7. }
  8.  
  9. }

应该就是标记为红色的那部分代码,以下我对它进行断点调试:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl0aWFucGVuZ2hhaGE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

上面两幅断点调试的图片能够看出这

  1. <span style="color:#ff0000;">StartRecordToStorageFileAsync(videoProfile, video)</span>

这个函数的两个參数均没有出现故障,參数的传递的正确的。如今怎么会出现这种情况。

 希望知道的同学能够给我答案。告诉我为什么,等待您的回复,谢谢同志们。

PS:搞了一下午,最终发现了这个程序的问题。如今把正确的程序源码传上去。

以下是我的程序源码,希望打啊多多指正。

http://download.csdn.net/detail/litianpeng1991/7556273

win8 metro 自己写摄像头录像项目的更多相关文章

  1. win8 metro 自己写摄像头拍照项目

    这个项目不是用的系统自带的CameraCaptureUI.是自己写的摄像头的调用,界面做的不好所以,不放了.可是能够实现拍照功能: 以下是using 程序命名空间: using Windows.Med ...

  2. VC/Wince 实现仿Win8 Metro风格界面1——设计概述和自绘Button(附效果图)

    去年用VC做了一个仿Win8 Metro风格的界面,感觉挺有意思,最近打算把实现过程和一些技术原理记录下来. 主要是风格上类似Win8,其实功能上很多借鉴了Android的操作方式.界面只支持两种大小 ...

  3. Win8 Metro风格的Web桌面HteOS

    前言     曾经天天折腾ExtJS,折腾累了.近期这段时间開始用jquery来做一些东西,发现还是蛮有意思的.可是做到最后才发现,原来做好设计真的很重要. 上图就是HteOS项目的截图,眼下正在开发 ...

  4. VC/Wince 实现仿Win8 Metro风格界面2——页面滑动切换(附效果图)

    前几天开始写仿Win8 Metro界面文章,部分网友觉得不错,感谢各位的意见.本来今天一直在折腾Android VLC播放器,没时间写.不过明天休息,所以今天就抽时间先写一下. 言归正传,我们都知道W ...

  5. 怎么在myeclipse中导入已经写好的项目

    经常我们需要学习别人写好了的源码来提升自己的编码能力,本文将介绍如何从外部导入别人已经写好的项目到我们myeclipse里面.同时也将介绍怎么给导入的工程改名的问题.                 ...

  6. 开发win8 metro monogame,显示pubcenter广告时会使游戏卡住的问题的解决方法。

    开发win8 metro游戏,使用monogame,并且还加上pubcenter广告,但是在x64的机子上遇到了问题,广告一出,游戏卡死.经过一系列的判断,发现monogame一直在update和dr ...

  7. 推荐一本写给IT项目经理的好书

    原文地址:http://www.cnblogs.com/cbook/archive/2011/01/19/1939060.html (防止原文作者删除.只能拷贝一份了) 推荐一本写给IT项目经理的好书 ...

  8. android 随手记 摄像头录像

    1 xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...

  9. Win8 Metro中文件读写删除与复制操作

    Win8Metro中,我们不能在向以前那样调用WIN32的API函数来进行文件操作,因此,下面就来介绍一下Win8 Metro中文件的读写操作. 1 Windows 8 Metro Style App ...

随机推荐

  1. CPP-基础:快速排序

    快速排序(Quicksort)是对冒泡排序的一种改进. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分 ...

  2. HTML 5 <a> 标签

    href 属性 定义和用法 href 属性规定链接的目标地址. 如果未使用 href 属性,则 <a> 标签不是链接,而是链接的占位符. HTML 4.01 与 HTML 5 之间的差异 ...

  3. JavaEE-01 JSP动态网页基础

    学习要点 B/S架构的基本概念 Web项目的创建和运行 JSP页面元素 MyEclipse创建和运行Web项目 Web程序调试 Web简史 web前端技术演进三阶段 WEB 1.0:信息广播. WEB ...

  4. java使用数据库连接池

    连接池的实现方式是首先使用JNDI(JavaTM Naming and Directory Interface) 将数据源对象注册为一个命名服务,然后使用JNDI提供的服务接口,按照名称检索对应的数据 ...

  5. 任务一:零基础HTML编码

    面向人群: 零基础或初学者 难度: 简单 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学习难度的合理性,但即使如此,真正决定课 ...

  6. es6(三set和map数据结构)

    es6中提供了一个新的数据结构Set,他有点类似数组,但和数组不同的是,在里面你如果写入重复的值的话,他不会显示重复值. const s =new Set(); [2,3,4,5,6,6,6,7,8, ...

  7. POJ 3659 Cell phone Network (树的最小点覆盖, 树形DP)

    题意: 给定一棵树,每个点可以覆盖自己和相邻的点, 求最少要多少个点覆盖图 #include <cstdio> #include <cstring> #include < ...

  8. 新建oracle连接远程服务

    更新下面两个文件夹中的 D:\app\shisan\product\11.2.0\client_1\network\admin D:\ORACLE\product\11.2.0\dbhome_1\NE ...

  9. 大数据学习——面试用sql——累计报表

    create table t_access_times(username string,month string,salary int)row format delimited fields term ...

  10. Selenium学习系列---- FirePath的安装和使用

    在用Selenium编写测试用例的时候,需要对对网页元素上定位,而现在很多的浏览器是可以看到网页上相关的元素信息,可以查看某一个网页的元素信息,通过定位的方式查找元素.另外安装好Selenium ID ...