您的位置:首页 > 其它

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

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

调用设备的摄像头,需要用到的是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");
}


最后,说句题外话,调用摄像头属于用户隐私,在调用之前应先取得用户同意,如弹对话框。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐