一篇说一下AForge.net具体使用和注意的地方。
1、获取设备上的所有摄像头:
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// 设定初始视频设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
xcount = videoDevices.Count;
if (videoDevices.Count > 0)
{ // 默认设备
sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
}
sourcePlayer.Start();
// 设置图片框初始图像
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
Properties.Resources.noimage.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
fingerPictureBox1.InitialImage = bs;
if (sourcePlayer.IsRunning)
{
button_Capture.IsEnabled = true;
}
}
sourcePlayer.IsRunning标示的是摄像头是否工作。
2、多个摄像头切换:
///
/// 切换摄像头
///
///
///
private void button_Qh_Click(object sender, RoutedEventArgs e)
{
if (j < xcount-1)
{
j = j + 1;
}
else if (j == xcount-1)
{
j = 0;
}
sourcePlayer.Stop();
sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[j].MonikerString);
sourcePlayer.Start();
}
3、拍摄照片:
///
/// 拍摄图像
///
///
///
private void button_Capture_Click(object sender, RoutedEventArgs e)
{
// 判断视频设备是否开启
try
{
if (sourcePlayer.IsRunning)
{ // 进行拍照
if (fingerPictureBox1.Visibility == Visibility.Hidden)
{
object box = this.FindName("fingerPictureBox1");
if (box is FingerPictureBox)
{
(box as FingerPictureBox).ActiveImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
sourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
fingerPictureBox1.Visibility = Visibility.Visible;
cam_sumit.Visibility = Visibility.Hidden;
lb_text.Content = "启动摄像头";
}
else {
fingerPictureBox1.Visibility = Visibility.Hidden;
cam_sumit.Visibility = Visibility.Visible;
lb_text.Content = "拍摄图像";
}
}
}
catch {
MessageBox.Show("请等待摄像头准备就绪再拍照!");
}
}
4、保存并返回:
///
/// 保存返回
///
///
///
private void button_Close_Click(object sender, RoutedEventArgs e)
{
var tempfilepath = "";
RenderTargetBitmap bmp = new RenderTargetBitmap(
(int)fingerPictureBox1.ActualWidth, (int)fingerPictureBox1.ActualHeight,
96, 96, PixelFormats.Default);
fingerPictureBox1.Measure(fingerPictureBox1.RenderSize);
fingerPictureBox1.Arrange(new Rect(fingerPictureBox1.RenderSize));
bmp.Render(fingerPictureBox1);
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
CaptureData = ms.ToArray();
var temppath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyy-mm-dd-hh-mm-sss") + ".jpg";
File.WriteAllBytes(temppath, CaptureData);
tempfilepath = FileOperation.AddFile(Common.Contract.WebModel.AyncModuleEnum.系统拍照, temppath, true);
}
if (CameraDoEvent != null)
{
CameraDoEvent(tempfilepath);
}
this.Close();
}
5、拍摄窗口关闭时一定要关闭摄像头,不然摄像头会一直开着:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (sourcePlayer.IsRunning)
{ // 停止视频
sourcePlayer.SignalToStop();
sourcePlayer.WaitForStop();
sourcePlayer.Stop();
}
}
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔