C# - VS2019调用AForge库实现调用摄像头拍照功能
前言
作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别还需要自己重新写,费时费力。最近,心里滋生一些用其他语言实现的想法。本篇讲解使用VS2019 C#程序调用AForge库实现调用摄像头拍照功能,接下来的几天学习使用ZXing库实现一维码/二维码的生成和识别,这样就能够做到,程序实现摄像头扫描一维码/二维码,并获取到码值。
AForge库的引用
新建一个C# WinFrm应用程序,右键解决方案项目,选择管理NuGet程序包,并安装必要的AForge库,如下图。


如果没有搜索到时,需要新增程序包源,然后重新搜索安装即可,如下图:

窗体控件部署
在窗体上添加必要的控件。当AForge库安装成功之后,可以在工具栏看到AForge.NET控件包,如下图。

核心代码
窗体载入时,枚举当前设备所有视频输入设备,并加载到设备列表中。
/// <summary>
/// 窗体初始化时,枚举所有视频输入设备,并加载到cmbDeviceLists中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainFrm_Load(object sender, EventArgs e)
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == )
throw new ApplicationException();
foreach (FilterInfo device in videoDevices)
{
cmbDeviceLists.Items.Add(device.Name);
}
cmbDeviceLists.SelectedIndex = ;
}
catch (ApplicationException)
{
cmbDeviceLists.Items.Add("No local capture devices");
videoDevices = null;
}
}
打开相机
/// <summary>
/// 打开相机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpenCamera_Click(object sender, EventArgs e)
{
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[cmbDeviceLists.SelectedIndex].MonikerString);
this.videoSourcePlayer.VideoSource = videoSource;
this.videoSourcePlayer.Start();
this.btnOpenCamera.Enabled = false;
this.btnTakePic.Enabled = true;
this.btnExit.Enabled = true;
}
拍照
/// <summary>
/// 拍照
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnTakePic_Click(object sender, EventArgs e)
{
try
{
if (this.videoSourcePlayer.IsRunning)
{
this.pbImage.Visible = true;
Bitmap bitMap = this.videoSourcePlayer.GetCurrentVideoFrame();
this.pbImage.Image = bitMap;
//设置图片相对控件的大小
this.pbImage.SizeMode = PictureBoxSizeMode.StretchImage;
this.btnSavePic.Enabled = true;
}
}
catch (Exception ex)
{
MessageBox.Show("摄像头异常:" + ex.Message);
}
}
保存图片
/// <summary>
/// 保存图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSavePic_Click(object sender, EventArgs e)
{
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
videoSourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
PngBitmapEncoder pE = new PngBitmapEncoder();
pE.Frames.Add(BitmapFrame.Create(bitmapSource));
string picName = GetImagePath() + "\\" + "Test" + ".bmp";
if (File.Exists(picName))
{
File.Delete(picName);
}
using (Stream stream = File.Create(picName))
{
pE.Save(stream);
}
MessageBox.Show("保存成功!根路径Imgs文件夹下!");
}
关闭相机
/// <summary>
/// 关闭相机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click(object sender, EventArgs e)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
this.btnOpenCamera.Enabled = true;
this.btnTakePic.Enabled = false;
this.btnExit.Enabled = false;
this.pbImage.Visible = false;
}
程序退出
private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
}
实现效果



至此,已全部完成!
作者:Jeremy.Wu
出处:https://www.cnblogs.com/jeremywucnblog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
C# - VS2019调用AForge库实现调用摄像头拍照功能的更多相关文章
- 谷歌使用navigator.mediaDevices.getUserMedia 调用摄像头拍照功能,不兼容IE
<template> <div> <!--canvas截取流--> <canvas ref="canvas" ...
- C#利用摄像头拍照功能实现
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- AForge.net 使用之录像拍照功能实现
连接摄像头设备,这里需要引入 AForge.Video; AForge.Video.DirectShow; AForge.Video.FFMPEG; 还需要添加引用,aforge.dll,aforge ...
- h5获取摄像头拍照功能
完整代码展示 <!DOCTYPE html> <head> <title>HTML5 GetUserMedia Demo</title> <met ...
- 调用本地摄像头拍照(H5和画布)
关于H5 和 画布 调用本地摄像头拍照功能的实现 1.代码的实现(html部分) <input type="button" title="开启摄像头" v ...
- VS2010开发MFC ActiveX,摄像头拍照上传Webservice(1)
最近工作项目,BS中需要用到摄像头拍照,需要存储本地,同时上传到服务器,尝试使用vc++做ActiveX来实现. 完全没有使用过vc,上网搜索各种知识,初步完成.在这里记录下有帮助的资料. 第一步:编 ...
- VS2010开发MFC ActiveX,摄像头拍照上传Webservice(2)
继续记录,第二步开发摄像头拍照功能. 使用vfw.h开发摄像头拍照功能,关于vfw网上有很多文章,很多代码可以参考 参考:http://blog.163.com/huangqiao_8/blog/st ...
- AForge.net 录像拍照功能实现 转
AForge.net 使用之录像拍照功能实现 最近使用aforge.NET拍照录像功能实现 记录一下以便以后好学习,哈哈,直接上代码 连接摄像头设备,这里需要引入 AForge.Video; AFor ...
- AForge调用摄像头拍照时设置分辨率
简单记录下AForge2.2.5.0版本调用摄像头拍照时设置分辨率的方法. FilterInfo info = _videoDevices[0];//获取第一个摄像头 _cameraDevice = ...
随机推荐
- PlayJava Day004
今日所学: /* 2019.08.19开始学习,此为补档. */ JDK 1.6:byte , int , short , char , enum JDK 1.7:byte , int , short ...
- PHP+Ajax点击加载更多列表数据实例
一款简单实用的PHP+Ajax点击加载更多列表数据实例,实现原理:通过“更多”按钮向服务端发送Ajax请求,PHP根据分页参数查询将最新的几条记录,数据以JSON形式返回,前台Query解析JSON数 ...
- docker环境无法执行jmap -heap 56命令
很奇怪的问题,但是jstack可以执行 解决方法: docker 内部使用jmap 需要启动容器时候配置权限:docker run --privileged=true --cap-add=SYS_P ...
- js监听屏幕方向如何第一次默认不监听
this.supportOrientation = typeof window.orientation === 'number'; // 检查屏幕方向 checkScreenOrientation() ...
- linux 创建虚拟块设备,制作文件系统并挂载,用于测试lustre
1.制作块文件 3 个 [root@localhost yaoxu]# [root@localhost yaoxu]# [root@localhost yaoxu]# 2.创建回环设备 [root@l ...
- (入门SpringBoot)SpringBoot项目事务(三)
Spring声明式事务的使用:由@Transactional进行标注,可以使用在类和方法上.当标注在类上,类下面所有公共非静态的方法都将启用事务功能.接下来,运行事务注解标注的方法,Spring的事务 ...
- Linux的启动过程的分析
Linux的启动过程 Linux系统从启动大哦提供服务的基本过程为:首先机器家电,然后通过MBR或者UEFI装载GRUB,再启动内核,再由内核启动服务,最后开始对外服务 CentOS7要经历四个主要阶 ...
- html里js的execCommand的一点用法
editorDoc.execCommand ('italic', false, null); 添加斜体 参考 http://help.dottoro.com/ljcvtcaw.php
- web-文件包含
提示 构造payload ?file=flag.php 得到一串字符,那么我们用PHP伪协议尝试一下 构造payload ?file=php://filter/read=convert.base64- ...
- fis3 相关
fis3 静态资源存放 windows: C:\Users\Administrator\AppData\Local\.fis3-tmp