摘要:public partial class FrmMain : Form{public FrmMain{InitializeComponent;}/// /// 摄像头集合/// private FilterInfoColl
有些业务场景没有复杂的工业图像处理应用,只是简易使用视频展示,或者GDI+简单二次开发,就没必要使用halcon、海康VM等,这时候可以采用简单的AForge开源库进行操作。
枚举所有摄像头,并找出需要的那个摄像头进行图像展示,存图、也可以进行图像旋转
素材来自于网络
枚举摄像头选择分辨率注册图像处理回调开始采图UI(来自于网络)6.代码部分,声明来自于网络,我增加了图像旋转部分
public partial class FrmMain : Form{public FrmMain{InitializeComponent;}/// /// 摄像头集合/// private FilterInfoCollection videoDevices;/// /// 具体摄像头/// private VideoCaptureDevice videoDevice;/// /// 分辨率对象/// private VideoCapabilities videoCapabilities;private void FrmMain_Load(object sender, EventArgs e){//获取内置、外接摄像头,非网络摄像头videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);if (videoDevices.Count != 0){foreach (FilterInfo device in videoDevices){cbo_camera.Items.Add(device.Name);}}else{cbo_camera.Items.Add("没有找到摄像头");}cbo_camera.SelectedIndex = 0;}private void cbo_camera_SelectedIndexChanged(object sender, EventArgs e){if (videoDevices.Count != 0){//获取每个摄像头支持的分辨率videoDevice = new VideoCaptureDevice(videoDevices[cbo_camera.SelectedIndex].MonikerString);GetDeviceResolution(videoDevice);}}/// /// 获取每个摄像头支持的分辨率/// /// private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice){cbo_rate.Items.Clear;videoCapabilities = videoCaptureDevice.VideoCapabilities;foreach (VideoCapabilities capabilty in videoCapabilities){cbo_rate.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");}cbo_rate.SelectedIndex = 0;}private void btn_conn_Click(object sender, EventArgs e){if (videoDevice != null){if ((videoCapabilities != null) && (videoCapabilities.Length != 0)){videoDevice.VideoResolution = videoCapabilities[cbo_rate.SelectedIndex];myvideo.VideoSource = videoDevice;myvideo.NewFrame += Myvideo_NewFrame;//进行图像二次处理myvideo.Start;btn_conn.Enabled = false;btn_stop.Enabled = true;btn_cap.Enabled = true;}}}private void Myvideo_NewFrame(object sender, ref Bitmap image){#region 方法一//image.RotateFlip(RotateFlipType.Rotate180FlipX);#endregion#region 方法二RotateBilinear rotate = new RotateBilinear((int)numRotate.Value); // 图像旋转值image = rotate.Apply(image);#endregion}private void btn_stop_Click(object sender, EventArgs e){if (myvideo.VideoSource != null){myvideo.SignalToStop;myvideo.WaitForStop;myvideo.VideoSource = null;btn_conn.Enabled = true;btn_stop.Enabled = false;btn_cap.Enabled = false;}}private void btn_cap_Click(object sender, EventArgs e){if (myvideo.VideoSource != null){Bitmap img = myvideo.GetCurrentVideoFrame;img.Save("001.jpg");MessageBox.Show("抓拍成功,文件在程序根目录下。");}}/// /// 窗体关闭程序退出时断开摄像头连接/// /// /// private void FrmMain_FormClosing(object sender, FormClosingEventArgs e){if (myvideo.VideoSource != null){myvideo.SignalToStop;myvideo.WaitForStop;myvideo.VideoSource = null;btn_conn.Enabled = true;btn_stop.Enabled = false;btn_cap.Enabled = false;}}/*栋哥自动化 V= dgzdh1*/private void btnRotate_Click(object sender, EventArgs e){}}来源:栋哥自动化
免责声明:本站系转载,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本站联系,我们将在第一时间删除内容!