WPF通过USB获取摄像头数据
最简单的demo————WPF获取USB摄像头数据
一、操作步骤
1、单击“检测”按钮
获取摄像头数据
2、在下拉框选择相应摄像头
3、单击“连接”按钮
就能看到摄像头的拍摄画面
4、点击“拍照”按钮
二、代码部分
新建WPF项目后
1、引入程序集
AForge.dll
AForge.Controls.dll
AForge.Imaging.dll
AForge.Video.dll
AForge.Video.DirectShow.dll
2、页面布局
<Window x:Class="viewtest2.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="0,0,-6.333,-0.333">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="13*"/>
<ColumnDefinition Width="92*"/>
</Grid.ColumnDefinitions>
<Button Name="lj" Content="连接" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="93.926,72,0,0" RenderTransformOrigin="2.165,0.731" Grid.Column="1" Click="lj_Click"/>
<ComboBox Name="name1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" Margin="10,70,0,0"/>
<Button Name="jc" Content="检测" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Grid.Column="1" Margin="276.333,72,0,0" RenderTransformOrigin="0.436,0.529" Click="jc_Click"/>
<wfi:WindowsFormsHost Margin="5,110,34.667,5" Grid.ColumnSpan="2">
<aforge:VideoSourcePlayer x:Name="sourcePlayer0" Width="640" Height="480"></aforge:VideoSourcePlayer>
</wfi:WindowsFormsHost>
<WindowsFormsHost HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="156" Grid.Column="1" Margin="110.333,125,0,0"/>
<Button Content="拍照" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Grid.Column="1" Margin="375.333,72,0,0" Click="Button_Click_1"/>
</Grid>
</Window>
3、后台代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
FilterInfoCollection a;//全局变量摄像头数据
private void lj_Click(object sender, RoutedEventArgs e)
{
VideoCaptureDevice vcd= new VideoCaptureDevice(a[name1.SelectedIndex].MonikerString);
vcd.DesiredFrameSize = new System.Drawing.Size(320, 240);
vcd.DesiredFrameRate = 1;
sourcePlayer0.VideoSource = vcd;
sourcePlayer0.Start();
}
private void jc_Click(object sender, RoutedEventArgs e)
{
name1.Items.Clear();
FilterInfoCollection fics;
fics= new FilterInfoCollection(FilterCategory.VideoInputDevice);
a = fics;
foreach (FilterInfo fic in fics)
{
name1.Items.Add(fic .Name);
//可以做出处理
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
try
{
if (sourcePlayer0.IsRunning)
{
BitmapSource bs= System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
sourcePlayer0.GetCurrentVideoFrame().GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
PngBitmapEncoder pbe= new PngBitmapEncoder();
pbe.Frames.Add(BitmapFrame.Create(bs));
string t = DateTime.Now.ToLongTimeString().ToString();
t = t.Replace("-","");
t = t.Replace(":","");
string jpgName = GetImagePath() + "\\" + t + ".jpg";
if (File.Exists(jpgName ))
{
File.Delete(jpgName );
}
using (Stream stream = File.Create(jpgName ))
{
pbe.Save(stream);
}
//拍照
if (sourcePlayer0 != null && sourcePlayer0.IsRunning)
{
MessageBox.Show("照片储存地址:" + picName);
}
}
}
catch (Exception ex)
{
MessageBox.Show("异常:" + ex.Message);
}
}
private string GetImagePath()
{
string t = DateTime.Now.ToShortDateString().ToString();
string ImgPath = System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)
+ System.IO.Path.DirectorySeparatorChar.ToString() + "PersonImg";
if (!Directory.Exists(ImgPath ))
{
Directory.CreateDirectory(ImgPath );
}
return ImgPath ;
}
}