Windows 8 Metro应用中使用调摄像头截图、截视频的功能

事先声明:本篇文章写的有些不负责任,因为本人机子上未装有摄像头,实际效果未能检测。

 

调用设备的摄像头,需要用到的是Windows.Media.Capture的命名空间。使用方法很简单,步骤如下:

 

1. 在XAML中加个按钮,用来点击调用摄像头。

<Button x:Name="CaptureButton" Content="截图" Click="CapturePhoto_Click"/>


2. 在.cs文件头部加入命名空间的引用。

using Windows.UI.Xaml.Controls;

 

3. 在按钮点击事件中调用摄像头。

CameraCaptureUI dialog = new CameraCaptureUI();


4. 设置截图后图片的纵横比。

dialog.PhotoSettings.CroppedAspectRatio = new Size(16, 9);


5. 截图后保存并获取该文件。(因为未实际测试,猜测是在 Document 下的 Photo 文件夹下,视频则在 Video 文件夹下)

StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);

 

6. 取得文件后就可以对操作,如在程序内显示。

if (file != null)
{
    BitmapImage bitmapImage = new BitmapImage();
    using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
    {
        bitmapImage.SetSource(fileStream);
    }
    CapturedPhoto.Source = bitmapImage;
}


调用摄像头截图的功能到此就结束了,再次声明,因条件有限,未能实际检测,此篇文章纯属抛砖引玉,如有什么错误,欢迎指正。

以下是调用摄像头拍摄照片的完整代码。录制视频和拍摄照片的代码大同小异,一起附上。

 

Photo

CameraCaptureUI dialog = new CameraCaptureUI();
dialog.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
    BitmapImage bitmapImage = new BitmapImage();
    using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
    {
        bitmapImage.SetSource(fileStream);
    }
    CapturedPhoto.Source = bitmapImage;
}


Video

CameraCaptureUI dialog = new CameraCaptureUI();
dialog.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4;

StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Video);
if (file != null)
{
    IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
    CapturedVideo.SetSource(fileStream, "video/mp4");
}


最后,说句题外话,调用摄像头属于用户隐私,在调用之前应先取得用户同意,如弹对话框。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值