tijian_tieying/web/dccdc.Selfhelp/frm_QRSetting.cs

178 lines
6.2 KiB
C#
Raw Permalink Normal View History

2025-02-20 12:14:39 +08:00
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace dccdc.Selfhelp
{
public partial class frm_QRSetting : Form
{
public frm_QRSetting()
{
InitializeComponent();
}
private FilterInfoCollection videoDevices;
private void frm_QRSetting_Load(object sender, EventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count != 0)
{
// add all devices to combo
foreach (FilterInfo device in videoDevices)
{
cb_cjk.Items.Add(device);
}
}
else
{
var fi = new FilterInfo("No DirectShow devices found");
cb_cjk.Items.Add(fi);
}
cb_cjk.SelectedIndex = 0;
//throw new NotImplementedException();
}
private VideoCaptureDevice videoDevice;
private void cb_cjk_SelectedIndexChanged(object sender, EventArgs e)
{
FilterInfo device = cb_cjk.SelectedItem as FilterInfo;
if (device.MonikerString != "")
{
videoDevice = new VideoCaptureDevice(device.MonikerString);
EnumeratedSupportedFrameSizes(videoDevice);
}
}
private VideoCapabilities[] videoCapabilities;
private void EnumeratedSupportedFrameSizes(VideoCaptureDevice videoDevice)
{
this.Cursor = Cursors.WaitCursor;
cb_fbl.Items.Clear();
//snapshotResolutionsCombo.Items.Clear();
try
{
videoCapabilities = videoDevice.VideoCapabilities;
//snapshotCapabilities = videoDevice.SnapshotCapabilities;
foreach (VideoCapabilities capabilty in videoCapabilities)
{
cb_fbl.Items.Add(string.Format("{0}x{1}",
capabilty.FrameSize.Width, capabilty.FrameSize.Height));
}
/*
foreach (VideoCapabilities capabilty in snapshotCapabilities)
{
snapshotResolutionsCombo.Items.Add(string.Format("{0} x {1}",
capabilty.FrameSize.Width, capabilty.FrameSize.Height));
}*/
if (videoCapabilities.Length == 0)
{
cb_fbl.Items.Add("Not supported");
}
/*
if (snapshotCapabilities.Length == 0)
{
snapshotResolutionsCombo.Items.Add("Not supported");
}
*/
cb_fbl.SelectedIndex = 0;
//snapshotResolutionsCombo.SelectedIndex = 0;
}
finally
{
this.Cursor = Cursors.Default;
}
}
private VideoCaptureDevice videoSource;
private void start_Click(object sender, EventArgs e)
{
//视频捕获设备
videoSource = new VideoCaptureDevice(videoDevices[cb_cjk.SelectedIndex].MonikerString);
//捕获到新画面时触发
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
//先关一下,下面再打开。避免重复打开的错误
CloseVideoSource();
//设置画面大小
//videoSource.DesiredFrameSize = new Size(160, 120);
//启动视频组件
//videoDevice = new VideoCaptureDevice(sb);
var videoCapabilities = videoDevice.VideoCapabilities;
int h = int.Parse(cb_fbl.SelectedItem.ToString().Split('x')[1]);
int w = int.Parse(cb_fbl.SelectedItem.ToString().Split('x')[0]);
//this.Width = w;
//this.Height = h;
foreach (var vc in videoCapabilities)
{
if (vc.FrameSize.Width == w && vc.FrameSize.Height == h)
videoDevice.VideoResolution = vc;
videoSource.Start();
}
}
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
/// <summary>
/// 全局变量,保存每一次捕获的图像
/// </summary>
Bitmap img = null;
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
img = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = img;
}
private void btn_test_Click(object sender, EventArgs e)
{
start_Click(sender, e);
}
private void bt_save_Click(object sender, EventArgs e)
{
CloseVideoSource();
if ((extend.Config.AppSettings.Settings.AllKeys as ICollection<string>).Contains("vid"))
{
extend.Config.AppSettings.Settings["vid"].Value = videoDevices[cb_cjk.SelectedIndex].MonikerString;
}
else
{
extend.Config.AppSettings.Settings.Add("vid", videoDevices[cb_cjk.SelectedIndex].MonikerString);
}
//System.Configuration.ConfigurationManager.AppSettings["vid"] = videoDevices[cb_cjk.SelectedIndex].MonikerString;
//System.Configuration.ConfigurationManager.AppSettings["vsize"] = cb_fbl.SelectedText ;
if ((extend.Config.AppSettings.Settings.AllKeys as ICollection<string>).Contains("vsize"))
{
extend.Config.AppSettings.Settings["vsize"].Value = cb_fbl.SelectedItem.ToString();
}
else
{
extend.Config.AppSettings.Settings.Add("vsize", cb_fbl.SelectedItem.ToString());
}
//System.Configuration.ConfigurationManager.RefreshSection("appSettings");
extend.Config.Save();
}
}
}