范例 示范使用摄像头
图9-1是项目启动时画面。
图9-1
◎ 单击“启动”时,会提示是否容许应用程序访问你的本机视频设备,如图9-2。
图9-2
◎ 选择“是”后,开始视频捕获,单击“截屏”下方会出现相应的载图,如图9-3。
图9-3
◎
主要XAML标记以下:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="189*" />
<RowDefinition Height="36*" />
<RowDefinition Height="75*" />
</Grid.RowDefinitions>
<Border x:Name="bordVider" Margin="3" CornerRadius="3" Width="400" BorderBrush="Gray" HorizontalAlignment="Left" BorderThickness="1" >
<Border.Background>
<VideoBrush x:Name="brshMyVideo"/>
</Border.Background>
</Border>
<Button Name="btStart" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1"
Width="78" Height="33"
Content="启动" FontSize="14" Click="btStart_Click" Margin="12,0,0,0" />
<Button Content="关闭" Height="33" HorizontalAlignment="Left" Margin="107,0,0,0" Name="btStop" VerticalAlignment="Top" Width="78" Grid.Row="1" FontSize="14" Click="btStop_Click" />
<Button Content="截屏" Height="33" HorizontalAlignment="Left" Margin="196,0,0,0" Name="btPing" VerticalAlignment="Top" Width="78" Grid.Row="1" FontSize="14" Click="btPing_Click" />
<Image Grid.Row="2" Height="70" HorizontalAlignment="Left" Margin="20,5,0,0" Name="imgMy" Stretch="UniformToFill" VerticalAlignment="Top" Width="99" />
</Grid>
</UserControl>
◎ 主要代码以下:
void btStart_Click(object sender, RoutedEventArgs e)
{
//取得默认视频设备
VideoCaptureDevice video = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
//建立视频捕获源
capSource = new CaptureSource();
if (CaptureDeviceConfiguration.RequestDeviceAccess())
{
//设置视频设备
capSource.VideoCaptureDevice = video;
brshMyVideo.SetSource(capSource);
brshMyVideo.Stretch = Stretch.Fill;
//启动摄像头
capSource.Start();
}
}
private void btStop_Click(object sender, RoutedEventArgs e)
{
//关闭摄像头
capSource.Stop();
}
private void btPing_Click(object sender, RoutedEventArgs e)
{
if (capSource.State == CaptureState.Started)
{
WriteableBitmap wBitmap = new WriteableBitmap(bordVider, new MatrixTransform());
imgMy.Source = wBitmap;
}
}
更详细内容及源代码下载:
http://www.amazon.cn/mn/detailApp/ref=sr_1_1?_encoding=UTF8&s=books&qid=1287058088&asin=B0043RT7I2&sr=8-1