前面我们完成了“模拟LED点阵”、“将图片转化为01信息”,本节中我们要做最重要的一步“从摄像头获取实时图片”。本来以为会有点难度,结果还挺简单。
我参考的是这篇文章:https://www.cnblogs.com/lidedong/p/5564851.html
简单来讲是先安装AForge这个组件,然后在窗口中放入一个videoSourcePlayer,指定其源后videoSourcePlayer就能实时播放摄像头拍到的视频了,最后用videoSourcePlayer可以直接截取图片。本节只是试验性地实现了摄像头实时图片获取,并串联了前面的几个模块,实现效果如下:
完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video.DirectShow;
namespace CaptureImg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
FilterInfoCollection videoDevices;
VideoCaptureDevice videoSource;
public int selectedDeviceIndex = 0;
private void Form1_Load(object sender, EventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
selectedDeviceIndex = 0;
videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);//连接摄像头
videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex];
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Start();
System.Threading.Thread.Sleep(2000);
Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();
string fileName = "test.jpg";
bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
作者: 布兰姥爷, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-3887969.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
curton 2019-4-25 08:13
CationLiu 2019-4-20 21:51
测量无处不在 2019-4-20 17:29
eeNick 2019-4-18 08:52
curton 2019-4-16 21:23