winform 引用AForge调用摄像头拍照
Nuget安装这个2个:
AForge.Controls;
AForge.Video.DirectShow;
code:
namespace WindowsFormsApp1
{
partial class FormCameraVideo
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tscbxCameras = new System.Windows.Forms.ComboBox();
this.panelVideo = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// tscbxCameras
//
this.tscbxCameras.FormattingEnabled = true;
this.tscbxCameras.Location = new System.Drawing.Point(55, 50);
this.tscbxCameras.Name = "tscbxCameras";
this.tscbxCameras.Size = new System.Drawing.Size(379, 21);
this.tscbxCameras.TabIndex = 0;
this.tscbxCameras.SelectedIndexChanged += new System.EventHandler(this.tscbxCameras_SelectedIndexChanged);
//
// panelVideo
//
this.panelVideo.Location = new System.Drawing.Point(55, 94);
this.panelVideo.Name = "panelVideo";
this.panelVideo.Size = new System.Drawing.Size(379, 282);
this.panelVideo.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 392);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 23);
this.button1.TabIndex = 2;
this.button1.Text = "btnPhotograph";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.btnPhotograph_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(335, 392);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(99, 23);
this.button2.TabIndex = 3;
this.button2.Text = "btnClose";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.btnClose_Click);
//
// FormCameraVideo
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(540, 451);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panelVideo);
this.Controls.Add(this.tscbxCameras);
this.Name = "FormCameraVideo";
this.Text = "FormCameraVideo";
this.Load += new System.EventHandler(this.FormCameraVideo_Load);
this.ResumeLayout(false); } #endregion private System.Windows.Forms.ComboBox tscbxCameras;
private System.Windows.Forms.Panel panelVideo;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
} using AForge.Controls;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging; namespace WindowsFormsApp1
{
public partial class FormCameraVideo : Form
{
public FormCameraVideo()
{
InitializeComponent();
panelVideo.Controls.Add(videoSourcePlayer);
videoSourcePlayer.Dock = DockStyle.Fill; }
private VideoSourcePlayer videoSourcePlayer = new VideoSourcePlayer();
private FilterInfoCollection videoDevices;
private void FormCameraVideo_Load(object sender, EventArgs e)
{ try
{
// 枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count == 0)
throw new ApplicationException(); foreach (FilterInfo device in videoDevices)
{
tscbxCameras.Items.Add(device.Name);
} tscbxCameras.SelectedIndex = 0; }
catch (ApplicationException)
{
tscbxCameras.Items.Add("No local capture devices");
videoDevices = null;
}
} private void CameraConn()
{
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);
// videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
// videoSource.DesiredFrameRate = 1; videoSourcePlayer.VideoSource = videoSource;
videoSourcePlayer.Start();
} private void tscbxCameras_SelectedIndexChanged(object sender, EventArgs e)
{
CameraConn();
} private void btnPhotograph_Click(object sender, EventArgs e)
{
try
{
if (videoSourcePlayer.IsRunning)
{
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() + "\\CameraPic" + ".jpg";
if (File.Exists(picName))
{
File.Delete(picName);
}
using (Stream stream = File.Create(picName))
{
pE.Save(stream);
}
//---------------拍照完成后关摄像头并刷新同时关窗体
if (videoSourcePlayer != null && videoSourcePlayer.IsRunning)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
} this.Close();
//----------------------------------------------
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("摄像头异常:" + ex.Message);
}
} private string GetImagePath()
{
string personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)
+ Path.DirectorySeparatorChar.ToString() + "PersonImg";
if (!Directory.Exists(personImgPath))
{
Directory.CreateDirectory(personImgPath);
} return personImgPath;
} /// <summary>
/// Close the camera
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, EventArgs e)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop(); } }
}
以上是全部代码。测试笔记本电脑摄像头OK。
winform 引用AForge调用摄像头拍照的更多相关文章
- AForge调用摄像头拍照时设置分辨率
简单记录下AForge2.2.5.0版本调用摄像头拍照时设置分辨率的方法. FilterInfo info = _videoDevices[0];//获取第一个摄像头 _cameraDevice = ...
- C#使用Aforge调用摄像头拍照
一. 新建一个Winform项目 二.使用Nuget添加引用 安装下图中红色框住的两个程序包 安装完后发现安装了如下图的程序包,这是因为上述两个程序包存在对其它程序包的依赖. 三.编写程序 1. 窗体 ...
- C# - VS2019调用AForge库实现调用摄像头拍照功能
前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...
- android: 调用摄像头拍照
很多应用程序都可能会使用到调用摄像头拍照的功能,比如说程序里需要上传一张图片 作为用户的头像,这时打开摄像头拍张照是最简单快捷的.下面就让我们通过一个例子来学 习一下,如何才能在应用程序里调用手机的摄 ...
- vue实现PC端调用摄像头拍照人脸录入、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式
进入正题 1. PC端调用摄像头拍照上传base64格式到后台,这个没什么花里胡哨的骚操作,直接看代码 (canvas + video) <template> <div> &l ...
- 【MediaKit】WPF项目中 调用摄像头拍照的开发包
今天遇到一个 人事的项目,项目中需要调用摄像头给员工照相.如何解决这个问题呢? 介绍一个开发包给你,MediaKit.论坛里头的人都说好,但是黑兔觉得大家好才是真的好.你不妨试试~ 第一步:添加WPF ...
- android ——调用摄像头拍照和相册
先在布局文件中加入两个按钮和一个图片控件 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
- Cordova - 使用Cordova开发iOS应用实战4(调用摄像头拍照,并编辑)
使用Cordova可以很方便的通过js代码来使用设备摄像头拍照,只需把camera插件添加进来即可. 一,添加camera插件 首先我们要在“终端”中进入工程所在的目录,然后运行如下命令: 1 cor ...
- 使用js调用摄像头拍照
在一些浏览器里已经可以使用web api调用摄像头功能了. 基于此可以经行拍照摄像功能,网上找了些资料,然后实现了简单的拍照功能 演示地址 bingxl.cn/webrtc.html 代码 <! ...
- html5中调用摄像头拍照
方法: getCamera: 获取摄像头管理对象 对象: Camera: 摄像头对象 CameraOption: JSON对象.调用摄像头的參数 PopPosition: JSON对象,弹出拍照或摄像 ...
随机推荐
- 0.2 Afterword to the Tenth Anniversary Edition(2010)
近十年来的进展: 1. 在实验实施领域. 超导电路:2-qubit量子算法:3-qubit 系统. 基于核自旋和单光子: 演示'量子纠错'和'量子模拟' 离子阱系统: '量子搜索算法'和'量子傅里叶变 ...
- 一款.NET开源的屏幕实时翻译工具
前言 今天大姚给大家推荐一款.NET开源的屏幕实时翻译工具:Translumo. 工具介绍 Translumo是一个.NET开源的高级屏幕翻译工具,能够实时检测和翻译选定区域内的文本(如字幕).Tra ...
- unique:数组去重,返回一个新数组
function unique(arr){ if(!isArrayLink(arr)){ //不是类数组对象 return arr } let result = [] let objarr = [] ...
- python实现的扫雷游戏的AI解法(启发式算法)
相关: python编写的扫雷游戏 如何使用计算机程序求解扫雷游戏 本文中实现的<扫雷>游戏的AI解法的项目地址: https://openi.pcl.ac.cn/devilmaycry8 ...
- .NET使用Umbraco CMS快速构建一个属于自己的内容管理系统
前言 今天大姚给大家分享一个.NET开源.免费(MIT License).功能强大.灵活易用的内容管理系统:Umbraco CMS.本文将介绍在.NET中如何使用Umbraco CMS快速构建一个属于 ...
- JPEG格式研究——(1)压缩原理及流程
之前一直很好奇图片和视频是如何压缩的,由于视频格式会更复杂,所以先从JPEG下手 因为网上资料太难找太分散,有些又看不太懂,所以根据自己的理解整理了一下 JPEG简介 JPEG(Joint Photo ...
- Codeforces Round 890 (Div. 2)
Tales of a Sort 题解 找到最大的能够产生逆序对的数即可 暴力\(O(n^2)\)枚举即可 const int N = 2e5 + 10, M = 4e5 + 10; int n; in ...
- 使用nginx 压缩
现在的程序使用单页面应用,因此程序会在一开始就会加载页面JS.如果带宽不够,那么会影响页面下载速度. 我们可以使用NGINX 进行压缩,加快文件下载. gzip on; gzip_min_length ...
- C# Linq 的三种去重方式(Distinct)
前言 关于C#中默认的Distinct方法在什么情况下才能去重,这个就不用我再多讲,针对集合对象去重默认实现将不再满足,于是乎我们需要自定义实现来解决这个问题,接下来我们详细讲解几种常见去重方案,孰好 ...
- LeetCode题集-6 - Z 字形变换
题目:将一个给定字符串 s 根据给定的行数 numRows ,以从上往下.从左到右进行 Z 字形排列. 这一题作为中等难度,下面和大家分享几种不同的解法. 01.二维矩阵模拟法 所谓二维矩阵模拟法就是 ...