最近由于某种原因呢,需要做一下拍照的功能,本来我纠结到底用AForge类库还是用WPFMediaKit.dll ,不过后来看网上说WPFMediaKit.dll 是截图而AForge是直接通过摄像头拍照,于是乎,我就选择了AForge类库。

首先留下发帖时我所用的AForge    链接:https://pan.baidu.com/s/1htmOjPi 密码:tovd

第一步

  将AForge中的DLL添加进引用,然后添加引用System.Drawing,System.Windows.Forms,WindowsFormsIntegration

第二步

  加上

   xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"

  还有

  <wfi:WindowsFormsHost Grid.Row="0" HorizontalAlignment="Left" Width="517" Height="320" VerticalAlignment="Top">
            <aforge:VideoSourcePlayer x:Name="player" Height="480" Width="640"/>
        </wfi:WindowsFormsHost>

  下面贴详细代码

MainWindow.xaml

<Window x:Class="FaceOne.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:FaceOne"xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"mc:Ignorable="d"Title="MainWindow" Height="600" Width="800" Loaded="Window_Loaded"><Grid><wfi:WindowsFormsHost Grid.Row="0" HorizontalAlignment="Left" Width="517" Height="320" VerticalAlignment="Top"><aforge:VideoSourcePlayer x:Name="player" Height="480" Width="640"/></wfi:WindowsFormsHost><Button x:Name="connectBtn" Content="连接" HorizontalAlignment="Left" Margin="53,439,0,0" VerticalAlignment="Top" Width="75" Click="connectBtn_Click"/><Button x:Name="captureBtn" Content="拍照" HorizontalAlignment="Left" Margin="180,439,0,0" VerticalAlignment="Top" Width="75" Click="captureBtn_Click"/><Button x:Name="closeBtn" Content="断开" HorizontalAlignment="Left" Margin="312,439,0,0" VerticalAlignment="Top" Width="75" Click="closeBtn_Click"/><Image x:Name="imageCapture" HorizontalAlignment="Left" Height="210" Margin="522,66,0,0" VerticalAlignment="Top" Width="239"/></Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace FaceOne
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{MyCapture myCapture = MyCapture.Instance();public MainWindow(){InitializeComponent();}private void Window_Loaded(object sender, RoutedEventArgs e){//myCapture.sourcePlayer = player;//配置好XAML后再加上这句,即可看到摄像头的视图
myCapture.getCamera();//获取摄像头myCapture.saveOk += ImageSaveOk;//保存照片后触发
        }//连接private void connectBtn_Click(object sender, RoutedEventArgs e){myCapture.CameraConn();}//拍照private void captureBtn_Click(object sender, RoutedEventArgs e){myCapture.CaputreImage();}//断开private void closeBtn_Click(object sender, RoutedEventArgs e){myCapture.CloseDevice();}//保存照片后触发(想看预览就用,不想看就不需要了)private void ImageSaveOk(string str){/*//老套的赋值方式,不会释放内存BitmapImage bit = new BitmapImage();bit.BeginInit();bit.UriSource = new Uri(AppDomain.CurrentDomain.BaseDirectory+str);bit.EndInit();imageCapture.Source = bit;*/BitmapImage bmi = myCapture.InitImage(str);imageCapture.Source = bmi;}}
}

MyCapture.cs

using AForge.Controls;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;namespace FaceOne
{class MyCapture{public delegate void SaveOk(string str);public SaveOk saveOk;private static MyCapture instance;public static MyCapture Instance(){if (instance == null){instance = new MyCapture();}return instance;}//private static FilterInfoCollection _cameraDevices;private FilterInfoCollection videoDevices;//private static VideoCaptureDevice div = null;
        VideoCaptureDevice videoSource;public VideoSourcePlayer sourcePlayer = new VideoSourcePlayer();public void getCamera(){// 获取电脑已经安装的视频设备videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);if (videoDevices != null && videoDevices.Count > 0){foreach (FilterInfo device in videoDevices){Console.WriteLine(device.Name);}//CameraConn();
            }}//连接并且打开摄像头public void CameraConn(){if (videoDevices.Count <= 0){Console.WriteLine("请插入视频设备");return;}videoSource= new VideoCaptureDevice(videoDevices[0].MonikerString);//连接到第一个设备,可灵活改动//videoSource.DesiredFrameSize = new System.Drawing.Size(240, 320);//videoSource.DesiredFrameRate = 1;
sourcePlayer.VideoSource = videoSource;videoSource.Start();sourcePlayer.Start();}//截取一帧图像并保存public void CaputreImage(string filePath=null,string fileName=null){if (sourcePlayer.VideoSource == null){return;}//判断是否有这个目录,如果没有则新创建这个目录/*if (!Directory.Exists(filePath)){Directory.CreateDirectory(filePath);}*/try{Image bitmap = sourcePlayer.GetCurrentVideoFrame();/*if (fileName == null){fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");}*///string str = @"" + fileName + ".jpg";string str = "asd.jpg";//使用InitImage可以重复读写一张图,不用的话,就只能一直保存新的图片了bitmap.Save(str,ImageFormat.Jpeg); //想看预览就用,不想看就不需要了
bitmap.Dispose();saveOk(str);}catch(Exception e){Console.WriteLine(e.Message.ToString());}}//关闭摄像头设备public void CloseDevice(){if (videoSource != null && videoSource.IsRunning){sourcePlayer.Stop();videoSource.SignalToStop();videoSource = null;videoDevices = null;}}//解决不同进程读取同一张图片的问题,使用流来读图片public BitmapImage InitImage(string filePath){BitmapImage bitmapImage;using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open))){FileInfo fi = new FileInfo(filePath);byte[] bytes = reader.ReadBytes((int)fi.Length);reader.Close();//image = new Image();bitmapImage = new BitmapImage();bitmapImage.BeginInit();bitmapImage.StreamSource = new MemoryStream(bytes);bitmapImage.EndInit();bitmapImage.CacheOption = BitmapCacheOption.OnLoad;//image.Source = bitmapImage;
                reader.Dispose();}return bitmapImage;}}
}

转载于:https://www.cnblogs.com/lingLuoChengMi/p/8466559.html

WPF中通过AForge实现USB摄像头拍照相关推荐

  1. arm linux 识别新硬盘_嵌入式Linux系列第13篇:USB摄像头拍照

    1.引言 本篇介绍USB摄像头的使用,实现的功能是通过摄像头进行拍照,生成jpg格式图片. 2.环境介绍 2.1.硬件 1) NUC972开发板 2) USB摄像头 2.2.软件 1) Uboot继续 ...

  2. diy感应usb摄像头拍照_DIY无线感应充电器

    diy感应usb摄像头拍照 Courtesy of Instructables user Inducktion shares a very detailed tutorial on how to bu ...

  3. 树莓派红外线报警c语言,【BASH SCRIPT】在树莓派上用HC-SR501红外感应器触发USB摄像头拍照...

    5-1假期闲的没事继续折腾树莓派.这次是尝试模拟一个防盗报警器,用HC-SR501 被动红外动作感应器 (Passive Infrared/PIR motion sensor)来触发USB camer ...

  4. python+opencv+usb摄像头 拍照太暗问题

    打开摄像头(不拍照,就一直打开着),里面的图像是比较清晰的(说明摄像头参数设置没问题),但用下面程序拍照,得到的照片比较暗 import cv2 cap = cv2.VideoCapture(1)#打 ...

  5. android yuy2,Android使用USB摄像头拍照yuy2转jpeg

    8种机械键盘轴体对比 本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选? #说明 由于项目需要,使用通用的usb摄像头进行拍照,这样成本低且比较通用,市面上一大堆支持yuv的usb摄像头.而且l ...

  6. python linux usb摄像头,树莓派用python中的OpenCV输出USB摄像头画面

    本文实例为大家分享了python OpenCV来表示USB摄像头画面的具体代码,供大家参考,具体内容如下 确认Python版本 $ python Python 2.7.13 (default, Jan ...

  7. C# 使用AForge调用笔记本摄像头拍照及录像

    文章目录 1.添加引用 2.AForge相关类库介绍 3.WinForm界面实现 4.属性 5.窗口加载与关闭 6.实例化 7. 摄像头的连接读取与关闭 8.拍照与保存 9.录像 10.计时器 11. ...

  8. ROS中usb摄像头的使用_(usb_cam)

    http://xiaoyatec.com/2015/10/10/ros%E4%B8%ADusb%E6%91%84%E5%83%8F%E5%A4%B4%E7%9A%84%E4%BD%BF%E7%94%A ...

  9. C++中调用usb摄像头并保存图片【学习记录第1篇】

    [学习记录第1篇]C++中通过OPENCV调用usb摄像头并保存图片 准备 外置USB摄像头代码 外置USB摄像头运行结果 外置USB超声波探头代码 外置USB超声波探头运行结果 第一篇博客的感想 准 ...

最新文章

  1. java 判断日期是同一天_如何检查Java中的两个日期是否在同一天
  2. Synchronize对象改变
  3. 发现一个骨灰级图形学大神的博客
  4. c语言memcopy_C语言中memcpy 函数的用法详解
  5. 所谓的Dumb Question
  6. 前端学习(3124):react-hello-react之批量传递props
  7. qt中解析json字符串的时候出现错误missingNameSeperator
  8. js中将html文档写入静态界面当中
  9. mysql存过游标_mysql存储过程游标使用
  10. 【2021-02】实时获取百度搜索词接口
  11. 抖音昵称html,抖音个性网名带特殊符号 带漂亮符号的抖音昵称
  12. 20个Mac软件下载常用的经典网站
  13. word文档通配符换行_Word效率指南(二)
  14. ​一文看尽MAE最新进展!恺明的MAE已经提出大半年,目前发展如何?
  15. 关于华为应用市场审核App无法启动的问题
  16. C++-容器-string:数字to字符串【std::to_string()】、字符串to数字【std::stoi、stol、stoll、stoul、stoull、stof、stod、stold】
  17. 如何从有故障的 SD 卡恢复文件
  18. 电脑C盘满了有什么影响?如何正确清理C盘?
  19. 什么是HTTP? HTTP和HTTPS的区别?
  20. 新氧云原生全栈数仓最佳实践

热门文章

  1. nowcoder172C 保护 (倍增lca+dfs序+主席树)
  2. [Java开发]打印当前路径到控制台
  3. 铁乐学python_Day42_锁和队列
  4. redux-4-ways
  5. JS(JQEERY) 获取JSON对象中的KEY VALUE
  6. Windows Mobile 编程 (Win32) - 获取设备能力
  7. matlab bwdist
  8. c语言打砖块游戏代码,打砖块游戏的源代码(请多指教)
  9. mac cad石材填充图案_CAD电视背景墙画法步骤
  10. 图谱(学习地图)系列总结,持续更新中