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对象,弹出拍照或摄像 ...
随机推荐
- 腾讯云禁止root用户登录
背景 买了腾讯云的云主机服务,装的 OpenCloudOS 系统,结果没几天就提示异常登录和恶意文件.结果还改了我的密码,导致我xshell登陆不了,通过腾讯云后台登进去发现有挖矿病毒,但还没完全跑起 ...
- 如何使用程序生成一个复杂的2D迷宫游戏地图
相关: I Solved The World's Hardest Maze (with Code) 本文不做过多的内容介绍,本文主要是分享上面的这个视频内容,该内容介绍了一些自动生成复杂2D迷宫的算法 ...
- 实证化讨论OpenAI的ChatGPT的政治倾向性
- 制作并量化GGUF模型上传到HuggingFace和ModelScope
llama.cpp 是 Ollama.LMStudio 和其他很多热门项目的底层实现,也是 GPUStack 所支持的推理引擎之一,它提供了 GGUF 模型文件格式.GGUF (General Gau ...
- 大便系统怎样安装RPM包
alien包转换工具 如果我们有很喜欢的RPM包,而又没有deb版本. 怎么办~? 可以同过alien来转换或者直接安装,这个小家伙可是个很方便的东西! 基本命令如下: 首先通过apt-get ins ...
- 三剑客-grep-awk-sed
三剑客-grep-awk-sed grep 格式: grep 参数 过滤文件内容 文件名称 cat file|grep '过滤的内容' 参数: -v 取反 -E 支持扩展正则 | 或者 egr ...
- java公式解析器学习与开发(2)——前缀表达式
释义 前缀表达式就是前序表达式. 前缀表达式就是不含括号的算术表达式,而且它是将运算符写在前面,操作数写在后面的表达式,为纪念其发明者波兰数学家Jan Lukasiewicz也称为"波兰式& ...
- Winform在主窗体里切换多个窗体
1.点击解决方案资源管理器的项目名称,右键添加用户控件(Windows窗体). 2.在主窗体代码中实例化添加的用户控件(Windows窗体). 点击查看代码 UserControl1 userCont ...
- 规模法则(Scaling Law)与参数效率的提高,
上一篇:<人工智能大语言模型起源篇(三),模型规模与参数效率> 规模法则与效率提高 如果你想了解更多关于提高变换器效率的各种技术,我推荐阅读2020年的<Efficient Tran ...
- codeforces1849 D. Array Painting
题目链接 https://codeforces.com/problemset/problem/1849/D 题意 输入 \(n(1 \leq n \leq 2e5)\) 和长为 \(n\) 的数组 \ ...