首先要先了解几个类:
1) VideoCaptureDevice类: 描述视频捕获设备(如网络摄相机)需要的和支持的视频格式信息。
2) CaptureDeviceConfiguration类: 表示一个帮助器类,该类用来获取有关可用的捕获设备(音频或视频设备)的信息,然后用来请求访问可用设备的捕获操作的客户端用户权限。 他的方法:
![]() ![]() ![]() | GetDefaultVideoCaptureDevice | 返回表示客户端系统上的默认视频捕获设备的 VideoCaptureDevice 对象。 |
![]() ![]() ![]() | RequestDeviceAccess | 请求访问客户端系统上提供的所有可能的捕获设备(音频或视频设备)。如果用户根据他们对访问请求对话框中的提示做出的响应授予设备访问权限,或者在某些情况下自动准许了访问请求,则为true。如果用户未授予访问权限或者由于其他因素而拒绝了访问请求,则为false。 |
XAML:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="simpledemowebcam.MainPage"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="162"></RowDefinition>
<RowDefinition Height="190"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Canvas Height="120" Width="160" HorizontalAlignment="Center" x:Name="cVideo" Margin="0,10,0,10">
<Rectangle Width="160" Height="120" x:Name="rectVideo" StrokeThickness="1" Stroke="Black"></Rectangle>
<TextBlock x:Name="lblStatus" Text="菩提树下的杨过" Foreground="White" Canvas.Top="50" Width="160" TextAlignment="Center"></TextBlock>
</Canvas>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Width="80" Height="22" Content="检测摄像头" x:Name="btnCheck" Click="btnCheck_Click"></Button>
<Button Width="80" Height="22" Content="视频原始截图" x:Name="btnCapture1" Margin="5,0,0,0" Click="btnCapture1_Click"></Button>
<Button Width="80" Height="22" Content="bitmap截图" x:Name="btnCapture2" Margin="5,0,0,0" Click="btnCapture2_Click"></Button>
</StackPanel>
</StackPanel>
<ScrollViewer x:Name="imgList" Height="180" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden" Margin="0,10,0,0" Grid.Row="1">
<StackPanel x:Name="sp" Orientation="Horizontal"></StackPanel>
</ScrollViewer>
</Grid>
</UserControl>
xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
namespace simpledemowebcam
{
public partial class MainPage : UserControl
{
bool _isWorked = false;
CaptureSource _webCamSource;
public MainPage()
{
// Required to initialize variables
InitializeComponent();
}
private void btnCheck_Click(object sender, RoutedEventArgs e)
{
if (_isWorked) { return; }
VideoCaptureDevice webCam = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
if (webCam == null)
{
lblStatus.Text = "未检测到摄像头设备!";
return;
}
if (CaptureDeviceConfiguration.RequestDeviceAccess())
{
_webCamSource = new CaptureSource();
_webCamSource.VideoCaptureDevice = webCam;
VideoBrush video = new VideoBrush();
video.SetSource(_webCamSource);
//设置如何拉伸 UniformToFill:对 TileBrush 内容进行缩放,从而可以完全填充输出区域,但保持其原始纵横比。
video.Stretch = Stretch.UniformToFill;
try
{
_webCamSource.Start();
this.rectVideo.Fill = video;
lblStatus.Text = "摄像头正在工作中...";//测试用
_isWorked = true;
_webCamSource.CaptureFailed += webCamSource_CaptureFailed;
_webCamSource.CaptureImageCompleted += webCamSource_CaptureImageCompleted;
}
catch
{
lblStatus.Text = "摄像头无法使用(可能被占用)";
}
}
else
{
lblStatus.Text = "您不同意在本程序\n中使用摄像头设备.";
}
}
/// <summary>
/// CaptureImageAsync视频原始内容截图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCapture1_Click(object sender, RoutedEventArgs e)
{
if (_isWorked)
{
//启动一个异步图像捕获请求。通过处理此 CaptureSource 上的 CaptureImageCompleted 事件来检索返回的图像。
_webCamSource.CaptureImageAsync();
}
}
void webCamSource_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
{
WriteableBitmap wb = e.Result;
Image image = new Image();
image.Height = 120;
image.Margin = new Thickness(5);
image.Source = wb;
sp.Children.Add(image);
}
void webCamSource_CaptureFailed(object sender, ExceptionRoutedEventArgs e)
{
lblStatus.Text = "截屏失败!";
}
/// <summary>
/// 利用WriteableBitmap截屏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCapture2_Click(object sender, RoutedEventArgs e)
{
//截取整个图像 带有上面的文字
WriteableBitmap wb = new WriteableBitmap(cVideo, null);
Image image = new Image();
image.Height = 120;
image.Margin = new Thickness(5);
image.Source = wb;
sp.Children.Add(image);
}
}
}