範例 示範使用攝像頭
圖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