基于大恒相机SDK,二次开发(C#)
界面如下 有曝光、增益两个参数可以调整(平台是X64)
有问题可以问 看到就回答
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 GxIAPINET;
using System.IO;
using GxIAPINET.Sample.Common;
namespace _Cam_调用及参数设置
{
public partial class Form1 : Form
{
bool m_bIsOpen = false; ///<设备打开状态
bool m_bIsSnap = false; ///<发送开采命令标识
bool m_bTriggerMode = false; ///<是否支持触发模式
//bool m_bTriggerActive = false; ///<是否支持触发极性
bool m_bTriggerSource = false; ///<是否支持触发源
// bool m_bWhiteAuto = false; ///<标识是否支持白平衡
//bool m_bBalanceRatioSelector = false; ///<标识是否支持白平衡通道
//bool m_bWhiteAutoSelectedIndex = true; ///<白平衡列表框转换标志
IGXFactory m_objIGXFactory = null; ///
/// 设备打开后初始化界面
///
private void __InitUI()
{
__InitEnumComBoxUI(m_cb_TriggerMode, "TriggerMode", m_objIGXFeatureControl, ref m_bTriggerMode); //触发模式初始化
__InitEnumComBoxUI(m_cb_TriggerSource, "TriggerSource", m_objIGXFeatureControl, ref m_bTriggerSource); //触发源初始化
__InitShutterUI(); //曝光初始化
__InitGainUI();
//增益的初始化
}
///
/// 对枚举型变量按照功能名称设置值
///
/// 枚举功能名称
/// 功能的值
/// 属性控制器对像
private void __SetEnumValue(string strFeatureName, string strValue, IGXFeatureControl objIGXFeatureControl)
{
if (null != objIGXFeatureControl)
{
//设置当前功能值
objIGXFeatureControl.GetEnumFeature(strFeatureName).SetValue(strValue);
}
}
///
/// 枚举型功能ComBox界面初始化
///
/// ComboBox控件名称
/// 枚举型功能名称
/// 属性控制器对像
/// 是否支持
private void __InitEnumComBoxUI(ComboBox cbEnum, string strFeatureName, IGXFeatureControl objIGXFeatureControl, ref bool bIsImplemented)
{
string strTriggerValue = ""; //当前选择项
List list = new List(); //Combox将要填入的列表
bool bIsReadable = false; //是否可读
// 获取是否支持
if (null != objIGXFeatureControl)
{
bIsImplemented = objIGXFeatureControl.IsImplemented(strFeatureName);
// 如果不支持则直接返回
if (!bIsImplemented)
{
return;
}
bIsReadable = objIGXFeatureControl.IsReadable(strFeatureName);
if (bIsReadable)
{
list.AddRange(objIGXFeatureControl.GetEnumFeature(strFeatureName).GetEnumEntryList());
//获取当前功能值
strTriggerValue = objIGXFeatureControl.GetEnumFeature(strFeatureName).GetValue();
}
}
//清空组合框并更新数据到窗体
cbEnum.Items.Clear();
foreach (string str in list)
{
cbEnum.Items.Add(str);
}
//获得相机值和枚举到值进行比较,刷新对话框
for (int i = 0; i < cbEnum.Items.Count; i++)
{
string strTemp = cbEnum.Items[i].ToString();
if (strTemp == strTriggerValue)
{
cbEnum.SelectedIndex = i;
break;
}
}
}
///
/// 曝光控制界面初始化
///
private void __InitShutterUI()
{
double dCurShuter = 0.0; //当前曝光值
double dMin = 0.0; //最小值
double dMax = 0.0; //最大值
string strUnit = ""; //单位
string strText = ""; //显示内容
//获取当前相机的曝光值、最小值、最大值和单位
if (null != m_objIGXFeatureControl)
{
dCurShuter = m_objIGXFeatureControl.GetFloatFeature("ExposureTime").GetValue();
dMin = m_objIGXFeatureControl.GetFloatFeature("ExposureTime").GetMin();
dMax = m_objIGXFeatureControl.GetFloatFeature("ExposureTime").GetMax();
strUnit = m_objIGXFeatureControl.GetFloatFeature("ExposureTime").GetUnit();
}
//刷新曝光范围及单位到界面上
strText = string.Format("曝光时间({0}~{1}){2}", dMin.ToString("0.00"), dMax.ToString("0.00"), strUnit);
label1.Text = strText;
//当前的曝光值刷新到曝光的编辑框
textBox1.Text = dCurShuter.ToString("0.00");
}
///
/// 增益控制界面初始化
///
private void __InitGainUI()
{
double dCurGain = 0; //当前增益值
double dMin = 0.0; //最小值
double dMax = 0.0; //最大值
string strUnit = ""; //单位
string strText = ""; //显示内容
//获取当前相机的增益值、最小值、最大值和单位
if (null != m_objIGXFeatureControl)
{
dCurGain = m_objIGXFeatureControl.GetFloatFeature("Gain").GetValue();
dMin = m_objIGXFeatureControl.GetFloatFeature("Gain").GetMin();
dMax = m_objIGXFeatureControl.GetFloatFeature("Gain").GetMax();
strUnit = m_objIGXFeatureControl.GetFloatFeature("Gain").GetUnit();
}
//更新增益值范围到界面
strText = string.Format("增益({0}~{1}){2}", dMin.ToString("0.00"), dMax.ToString("0.00"), strUnit);
label2.Text = strText;
//当前的增益值刷新到增益的编辑框
string strCurGain = dCurGain.ToString("0.00");
textBox2.Text = strCurGain;
}
///
/// 更新界面
///
void __UpdateUI()
{
//相机控制相关使能操作
button1.Enabled = !m_bIsOpen;
button2.Enabled = m_bIsOpen;
m_btn_CloseDevice.Enabled = m_bIsOpen && !m_bIsSnap;
button4.Enabled = m_bIsSnap;
//增加
//相机参数相关的使能操作
m_cb_TriggerMode.Enabled = m_bIsOpen && m_bTriggerMode;
m_cb_TriggerSource.Enabled = m_bIsOpen && m_bTriggerSource;
textBox1.Enabled = m_bIsOpen;
textBox2.Enabled = m_bIsOpen;
}
///
/// 相机初始化
///
void __InitDevice()
{
if (null != m_objIGXFeatureControl)
{
//设置采集模式连续采集
m_objIGXFeatureControl.GetEnumFeature("AcquisitionMode").SetValue("Continuous");
}
}
///
/// 关闭流
///
private void __CloseStream()
{
try
{
//关闭流
if (null != m_objIGXStream)
{
m_objIGXStream.Close();
m_objIGXStream = null;
}
}
catch (Exception)
{
}
}
///
/// 关闭设备
///
private void __CloseDevice()
{
try
{
//关闭设备
if (null != m_objIGXDevice)
{
m_objIGXDevice.Close();
m_objIGXDevice = null;
}
}
catch (Exception)
{
}
}
///
/// 加载窗体执行初始化UI和库
///
///
///
///
private void Form1_Load_1(object sender, EventArgs e)
{ try
{
//刷新界面
__UpdateUI();
m_objIGXFactory = IGXFactory.GetInstance();
m_objIGXFactory.Init();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//private void Form1_Load(object sender, EventArgs e)
//{
// try
// {
// //刷新界面
// __UpdateUI();
// m_objIGXFactory = IGXFactory.GetInstance();
// m_objIGXFactory.Init();
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
//}
///
/// 打开设备打开流
///
///
///
private void button1_Click(object sender, EventArgs e)
{
try
{
List listGXDeviceInfo = new List();
//关闭流
__CloseStream();
// 如果设备已经打开则关闭,保证相机在初始化出错情况下能再次打开
__CloseDevice();
m_objIGXFactory.UpdateDeviceList(200, listGXDeviceInfo);
// 判断当前连接设备个数
if (listGXDeviceInfo.Count <= 0)
{
MessageBox.Show("未发现设备!");
return;
}
// 如果设备已经打开则关闭,保证相机在初始化出错情况下能再次打开
if (null != m_objIGXDevice)
{
m_objIGXDevice.Close();
m_objIGXDevice = null;
}
//打开列表第一个设备
m_objIGXDevice = m_objIGXFactory.OpenDeviceBySN(listGXDeviceInfo[0].GetSN(), GX_ACCESS_MODE.GX_ACCESS_EXCLUSIVE);
m_objIGXFeatureControl = m_objIGXDevice.GetRemoteFeatureControl();
//打开流
if (null != m_objIGXDevice)
{
m_objIGXStream = m_objIGXDevice.OpenStream(0);
}
// 建议用户在打开网络相机之后,根据当前网络环境设置相机的流通道包长值,
// 以提高网络相机的采集性能,设置方法参考以下代码。
GX_DEVICE_CLASS_LIST objDeviceClass = m_objIGXDevice.GetDeviceInfo().GetDeviceClass();
if (GX_DEVICE_CLASS_LIST.GX_DEVICE_CLASS_GEV == objDeviceClass)
{
// 判断设备是否支持流通道数据包功能
if (true == m_objIGXFeatureControl.IsImplemented("GevSCPSPacketSize"))
{
// 获取当前网络环境的最优包长值
uint nPacketSize = m_objIGXStream.GetOptimalPacketSize();
// 将最优包长值设置为当前设备的流通道包长值
m_objIGXFeatureControl.GetIntFeature("GevSCPSPacketSize").SetValue(nPacketSize);
}
}
//初始化相机参数
__InitDevice();
// 获取相机参数,初始化界面控件
__InitUI();
m_objGxBitmap = new GxBitmap(m_objIGXDevice, pictureBox1);
// 更新设备打开标识
m_bIsOpen = true;
//刷新界面
__UpdateUI();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
///
/// 停止采集
///
/// param >
/// < param name="e">
private void m_btn_StopDevice_Click(object sender, EventArgs e)
{
try
{
//发送停采命令
if (null != m_objIGXFeatureControl)
{
m_objIGXFeatureControl.GetCommandFeature("AcquisitionStop").Execute();
}
//关闭采集流通道
if (null != m_objIGXStream)
{
m_objIGXStream.StopGrab();
//注销采集回调函数
m_objIGXStream.UnregisterCaptureCallback();
}
m_bIsSnap = false;
// 更新界面UI
__UpdateUI();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
///
/// 图像的显示和存储
///
/// 图像信息对象
void ImageShowAndSave(IFrameData objIFrameData)
{
try
{
m_objGxBitmap.Show(objIFrameData);
}
catch (Exception)
{
}
// 是否需要进行图像保存
//if (m_bSaveBmpImg.Checked)
//{
// DateTime dtNow = System.DateTime.Now; // 获取系统当前时间
// string strDateTime = dtNow.Year.ToString() + "_"
// + dtNow.Month.ToString() + "_"
// + dtNow.Day.ToString() + "_"
// + dtNow.Hour.ToString() + "_"
// + dtNow.Minute.ToString() + "_"
// + dtNow.Second.ToString() + "_"
// + dtNow.Millisecond.ToString();
// string stfFileName = m_strFilePath + "\\" + strDateTime + ".bmp"; // 默认的图像保存名称
// m_objGxBitmap.SaveBmp(objIFrameData, stfFileName);
//}
}
///
/// 回调函数,用于获取图像信息和显示图像
///
/// 用户自定义传入参数
/// 图像信息对象
private void __CaptureCallbackPro(object objUserParam, IFrameData objIFrameData)
{
try
{
Form1 objGxSingleCam = objUserParam as Form1;
objGxSingleCam.ImageShowAndSave(objIFrameData);
}
catch (Exception)
{
}
}
/
/ 切换"触发模式"combox框响应函数
/
/
/
//private void m_cb_TriggerMode_SelectedIndexChanged(object sender, EventArgs e)
//{
// try
// {
// string strValue = m_cb_TriggerMode.Text;
// __SetEnumValue("TriggerMode", strValue, m_objIGXFeatureControl);
// // 更新界面UI
// __UpdateUI();
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
//}
/
/ 切换"触发源"Combox消息响应函数
/
/
/
//private void m_cb_TriggerSource_SelectedIndexChanged(object sender, EventArgs e)
//{
// try
// {
// string strValue = m_cb_TriggerSource.Text;
// __SetEnumValue("TriggerSource", strValue, m_objIGXFeatureControl);
// // 更新界面UI
// __UpdateUI();
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.Message);
// }
//}
///
/// 检查是否输入的是小数或整数,小数要求小数点最多8位
///
/// 控件对象
/// 按键响应事件
private void __CheckKeyPress(object sender, KeyPressEventArgs e)
{
if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
{
if (e.KeyChar == '.')
{
if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
e.Handled = true;
}
else
e.Handled = true;
}
else
{
if (e.KeyChar <= 31)
{
e.Handled = false;
}
else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
{
if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 8)
e.Handled = true;
}
}
}
private void m_cb_TriggerMode_SelectedIndexChanged_1(object sender, EventArgs e)
{
try
{
//发送软触发命令
if (null != m_objIGXFeatureControl)
{
m_objIGXFeatureControl.GetCommandFeature("TriggerSoftware").Execute();
}
// 更新界面UI
__UpdateUI();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
try
{
// 如果未停采则先停止采集
if (m_bIsSnap)
{
if (null != m_objIGXFeatureControl)
{
m_objIGXFeatureControl.GetCommandFeature("AcquisitionStop").Execute();
m_objIGXFeatureControl = null;
}
}
}
catch (Exception)
{
}
m_bIsSnap = false;
try
{
//停止流通道、注销采集回调和关闭流
if (null != m_objIGXStream)
{
m_objIGXStream.StopGrab();
//注销采集回调函数
m_objIGXStream.UnregisterCaptureCallback();
m_objIGXStream.Close();
m_objIGXStream = null;
}
}
catch (Exception)
{
}
try
{
//关闭设备
if (null != m_objIGXDevice)
{
m_objIGXDevice.Close();
m_objIGXDevice = null;
}
}
catch (Exception)
{
}
m_bIsOpen = false;
//刷新界面
__UpdateUI();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
//开启采集流通道
if (null != m_objIGXStream)
{
//RegisterCaptureCallback第一个参数属于用户自定参数(类型必须为引用
//类型),若用户想用这个参数可以在委托函数中进行使用
m_objIGXStream.RegisterCaptureCallback(this, __CaptureCallbackPro);
m_objIGXStream.StartGrab();
}
//发送开采命令
if (null != m_objIGXFeatureControl)
{
m_objIGXFeatureControl.GetCommandFeature("AcquisitionStart").Execute();
}
m_bIsSnap = true;
// 更新界面UI
__UpdateUI();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void m_btn_CloseDevice_Click(object sender, EventArgs e)
{
{
try
{
//发送停采命令
if (null != m_objIGXFeatureControl)
{
m_objIGXFeatureControl.GetCommandFeature("AcquisitionStop").Execute();
}
//关闭采集流通道
if (null != m_objIGXStream)
{
m_objIGXStream.StopGrab();
//注销采集回调函数
m_objIGXStream.UnregisterCaptureCallback();
}
m_bIsSnap = false;
// 更新界面UI
__UpdateUI();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
__CheckKeyPress(sender, e);
}
private void textBox1_Leave(object sender, EventArgs e)
{
double dShutterValue = 0.0; //曝光值
double dMin = 0.0; //最小值
double dMax = 0.0; //最大值
try
{
try
{
dShutterValue = Convert.ToDouble(label1.Text);
}
catch (Exception)
{
__InitShutterUI();
MessageBox.Show("请输入正确的曝光时间");
return;
}
//获取当前相机的曝光值、最小值、最大值和单位
if (null != m_objIGXFeatureControl)
{
dMin = m_objIGXFeatureControl.GetFloatFeature("ExposureTime").GetMin();
dMax = m_objIGXFeatureControl.GetFloatFeature("ExposureTime").GetMax();
//判断输入值是否在曝光时间的范围内
//若大于最大值则将曝光值设为最大值
if (dShutterValue > dMax)
{
dShutterValue = dMax;
}
//若小于最小值将曝光值设为最小值
if (dShutterValue < dMin)
{
dShutterValue = dMin;
}
label1.Text = dShutterValue.ToString("F2");
m_objIGXFeatureControl.GetFloatFeature("ExposureTime").SetValue(dShutterValue);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void textBox2_Leave(object sender, EventArgs e)
{
double dGain = 0; //增益值
double dMin = 0.0; //最小值
double dMax = 0.0; //最大值
try
{
try
{
dGain = Convert.ToDouble(label2.Text);
}
catch (Exception)
{
__InitGainUI();
MessageBox.Show("请输入正确的增益值");
return;
}
//当前相机的增益值、最小值、最大值
if (null != m_objIGXFeatureControl)
{
dMin = m_objIGXFeatureControl.GetFloatFeature("Gain").GetMin();
dMax = m_objIGXFeatureControl.GetFloatFeature("Gain").GetMax();
//判断输入值是否在增益值的范围内
//若输入的值大于最大值则将增益值设置成最大值
if (dGain > dMax)
{
dGain = dMax;
}
//若输入的值小于最小值则将增益的值设置成最小值
if (dGain < dMin)
{
dGain = dMin;
}
label2.Text = dGain.ToString("F2");
m_objIGXFeatureControl.GetFloatFeature("Gain").SetValue(dGain);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
this.pictureBox1.Focus();
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
__CheckKeyPress(sender, e);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
// 如果未停采则先停止采集
if (m_bIsSnap)
{
if (null != m_objIGXFeatureControl)
{
m_objIGXFeatureControl.GetCommandFeature("AcquisitionStop").Execute();
}
}
}
catch (Exception)
{
}
try
{
//停止流通道、注销采集回调和关闭流
if (null != m_objIGXStream)
{
m_objIGXStream.StopGrab();
m_objIGXStream.UnregisterCaptureCallback();
m_objIGXStream.Close();
m_objIGXStream = null;
}
}
catch (Exception)
{
}
try
{
//关闭设备
if (null != m_objIGXDevice)
{
m_objIGXDevice.Close();
m_objIGXDevice = null;
}
}
catch (Exception)
{
}
try
{
//反初始化
if (null != m_objIGXFactory)
{
m_objIGXFactory.Uninit();
}
}
catch (Exception)
{
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}