前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c# 利用AForge.NET组件操作摄像头

c# 利用AForge.NET组件操作摄像头

作者头像
用户5640963
发布2019-07-26 10:17:18
1.6K0
发布2019-07-26 10:17:18
举报
文章被收录于专栏:卯金刀GG

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Drawing.Imaging;

using System.Text;

using System.Windows.Forms;

using System.Threading;

using AForge;

using AForge.Video;

using AForge.Video.DirectShow;

using AForge.Imaging;

using AForge.Imaging.Filters;

using System.IO;

namespace Camera

{

public partial class Form1 : Form

{

private FilterInfoCollection videoDevices;

public VideoCaptureDevice videoSource;

private int flag = 1;

private string dirc = System.AppDomain.CurrentDomain.BaseDirectory + "JPG"; //截图保存的目录

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

if (!Directory.Exists(dirc))

Directory.CreateDirectory(dirc);

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 toolStripButton1_Click(object sender, EventArgs e)

{

CameraConn();

}

private void CameraConn()

{ //你这里重新定义了一个对象,所以出错

videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);

videoSource.DesiredFrameSize = new Size(320, 240);

videoSource.DesiredFrameRate = 1;

videPlayer.VideoSource = videoSource;

videPlayer.Start();

}

private void toolStripButton2_Click(object sender, EventArgs e)

{

videPlayer.SignalToStop();

videPlayer.WaitForStop();

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

toolStripButton2_Click(null, null);

}

private void toolStripButton3_Click(object sender, EventArgs e)

{

//不懂截图,但还是给你简单完善了下

flag = 0;

if (videoSource == null)

{

MessageBox.Show("请先连接摄像头");

}

else if (!videoSource.IsRunning)

{

MessageBox.Show("摄像头已经关闭,请重新打开");

}

else

{

videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);

}

}

private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)

{

Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();

if (flag == 0)

{

string img = dirc + "/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";

bitmap.Save(img);

flag = 1;

}

}

}

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档