win8 metro 自己写摄像头录像项目

这是要求不适用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>
            }

        }

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

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

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

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

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

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

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

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

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

时间: 2024-08-03 23:40:22

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

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

这个项目不是用的系统自带的CameraCaptureUI,是自己写的摄像头的调用,界面做的不好所以,不放了,但是可以实现拍照功能: 下面是using 程序命名空间: using Windows.Media.Capture; using Windows.Media.MediaProperties; using Windows.UI.Xaml.Media.Imaging; using Windows.Storage; using Windows.Storage.Pickers; using Wind

win8 metro 调用摄像头拍摄照片并将照片保存在相应的位置

刚刚做过这类开发,所以就先献丑了,当然所贴上的源码都是经过验证过的,已经运行成功了,希望可以给大家一些借鉴: 下面是metro UI代码: <Page x:Class="Camera.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

win8 metro 调用摄像头录制视频并将视频保存在相应的位置

上一篇文章介绍了在win8 metro 调用摄像头拍摄照片并将照片保存在相应的位置的功能,那么这一片文章主要介绍了的就是录制视频了,其实这个差不多,所用的思想也是一样的,因为图片和视频都可以转化为流文件,那么它们本质上都是一样的,但是还是有个别地方时不同的,接下来我们就介绍一下这个别地方的不同吧 下面是metro UI的代码: <Page x:Class="Camera1.MainPage" xmlns="http://schemas.microsoft.com/win

Win8 Metro动态加载内容框架

制作背景 为了参加ImagineCup 2013 世界公民类比赛,我们设计制作了一个可动态扩展的幼教类App.这个App需要能动态加载内容,内容包括带动画可交互的电子书,动画,视频,游戏. 技术支持 2012年10月第一次:因为SVG性能问题,将SVG换为cocos2d-x JSBind,可惜cocos2d-x JSBind不完善,最后换为cocos2d-x html5.11月第二次:cocos2d-x html5性能问题,破产.12月第三次:取消HTML5,转为使用XAML+JS模式. (微软

win8 metro MediaCapture 类

最近接触的项目是有关win8 metro 中camera的项目,其中比较重要的类就是 MediaCapture类,现在介绍一下MediaCapture类,也总结一下自己的一些项目体会: 下面是MediaCapture类的一些方法调用: using System; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.Foundation.Metadata; using Windows.Media

HteOS - Win8 Metro UI 风格的Web桌面

HteOS是一款Win8 Metro UI风格的Web桌面应用开发框架 现代的.简约的界面,丰富的组件.功能,全新的Web桌面 全新的Web桌面应用开发框架 全新设计 全新的Web桌面设计,告别古板与生硬 Win8 Metro风格界面,动态磁贴灵动轻巧 二次开发 应用场景广泛,能够适用各种项目兼容IE8+浏览器,快速进行二次开发 应用部署 没有环境限制,可以在任何平台进行部署不需要进行额外的部署设置,即插即用 不拘泥于平庸的Web桌面设计 卡片布局 Metro风格的卡片布局,不再是审美疲劳的树状

Win8 Metro风格的Web桌面HteOS

Checkbox类: 1.实现checkbox的全选功能 <script type="text/javascript"> //全选checkbox:1.当全选checkbox勾选,子checkbox(name为'ids'的checkbox)自动全部勾选 // 2.当全选checkbox取消勾选,子checkbox自动全部取消勾选 function checkAll(){ if($("#checkall")[0].checked){ $("inp

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

开发win8 metro游戏,使用monogame,并且还加上pubcenter广告,但是在x64的机子上遇到了问题,广告一出,游戏卡死.经过一系列的判断,发现monogame一直在update和draw,没有停止工作,但是我们看到的画面一直定格在广告出来的那一刻.既然是在广告出来的时候死的,那我们就把广告停了再开试试,在广告的AdRefreshed事件中添加这两行代码 MyAd.Suspend();            MyAd.Resume(); 测试后发现广告不会卡游戏了.

Win8 Metro(C#)数字图像处理--2.56简单统计法图像二值化

原文:Win8 Metro(C#)数字图像处理--2.56简单统计法图像二值化  [函数名称] 简单统计法图像二值化 WriteableBitmap StatisticalThSegment(WriteableBitmap src) /// <summary> /// Statistical method of image segmention. /// </summary> /// <param name="src">The source im