赞
踩
众所周知,懒是推动生产力的主要诱因。
当我寻遍网络的触角,没发现一个合适的相机库。
很沮丧。。。只能手搓一个
c# 相机库,含海康、大恒品牌2D相机的常用功能。
底层采用回调+信号量模式封装 ,最大程度减小线程资源,提高采图效率。
开源地址:https://gitee.com/laomaogu/mgcamctrl
现只兼容了大恒和海康,都是常用的
其他相机,看我老板啥时候换品牌吧,或者换老板?
当然如果小伙伴感兴趣,可以在这个框架下直接添加
ICamera myCamera;
myCamera= CamFactory.CreatCamera(CameraBrand.HIK);//CameraBrand.DaHeng
选取对应SN号的相机,初始化相机
调试的时候,经常直接中断程序
程序Kill了,但相机还未注销。。。。。。
导致每次中断重启都连不上相机,要重新断电才行
所以在初始化的时候都加了心跳(仅支持网口),
断连超1000ms后,相机就自动注销,
下次能直接连接相机啦!
中断算个diao,哈哈哈哈
//获取相机枚举
var devicelist = myCamera.GetListEnum();
//or
//var devicelist = CamFactory.GetDeviceEnum(CameraBrand.HIK);
//选对应SN,初始化相机
myCamera.InitDevice(devicelist.First());
启动相机有以下几种方式:
//硬触发模式 启动相机
myCamera.StartWith_HardTriggerModel(HardTriggerModel.Line0);
//软触发模式 启动相机
//myCamera.StartWith_SoftTriggerModel();
//硬触发 + 回调模式 启动相机
// myCamera.StartWith_HardTriggerModel_SetCallback(HardTriggerModel.Line0, CameraCallBack);
//软触发 + 回调模式 启动相机
//myCamera.StartWith_SoftTriggerModel_SetCallback(CameraCallBack);
回调函数:
/// <summary>
/// 回调函数
/// </summary>
/// <param name="bmp"></param>
private void CameraCallBack(Bitmap bmp)
{
//执行取图后的操作
bmp.Save("./test.bmp");
}
为了能兼容其他视觉平台,图像格式为Bitmap
这样无论是OpenCV Halcon VisionPro 都可任意转换想要的格式
//等待硬触发 获取图片, 设定超时:5000ms myCamera.GetImage(out Bitmap CaptureImage,5000); //or 使用默认超时时间 //myCamera.GetImage(out Bitmap CaptureImage); //软触发获取图像 //myCamera.GetImageWithSoftTrigger(out Bitmap CaptureImage); /// <summary> /// 回调函数 /// </summary> /// <param name="bmp"></param> private void CameraCallBack(Bitmap bmp) { //执行取图后的操作 bmp.Save("./test.bmp"); }
//注销当前实例
myCamera.CloseDevice();
or
//CamFactory.DestroyCamera(myCamera);
or
//CamFactory.DestroyAll();
含常用参数设置和获取
如曝光值、延时、硬触发方式等
详细如下
namespace MG.CamCtrl { public interface ICamera : IDisposable { #region operate /// <summary> /// 获取相机SN枚举 /// </summary> /// <returns></returns> List<string> GetListEnum(); /// <summary> /// 初始化相机 /// </summary> /// <param name="CamSN"></param> /// <returns></returns> bool InitDevice(string CamSN); /// <summary> /// 注销相机 /// </summary> void CloseDevice(); /// <summary> /// 回调 + 循环采图 启动相机 /// </summary> /// <param name="callbackfunc"></param> /// <returns></returns> bool StartWith_Continue_SetCallback(Action<Bitmap> callbackfunc); /// <summary> /// 软触发模式 启动相机 /// </summary> /// <returns></returns> bool StartWith_SoftTriggerModel(); /// <summary> /// 硬触发模式 启动相机 /// </summary> /// <param name="hardtriggeritem"></param> /// <returns></returns> bool StartWith_HardTriggerModel(TriggerSource hardtriggeritem); /// <summary> /// 硬触发 + 回调 启动相机 /// </summary> /// <param name="hardtriggeritem"></param> /// <param name="callbackfunc"></param> /// <returns></returns> bool StartWith_HardTriggerModel_SetCallback(TriggerSource hardtriggeritem, Action<Bitmap> callbackfunc); /// <summary> /// 软触发 + 回调 启动相机 /// </summary> /// <param name="callbackfunc"></param> /// <returns></returns> bool StartWith_SoftTriggerModel_SetCallback(Action<Bitmap> callbackfunc); /// <summary> /// 等待硬触发获取图像 /// </summary> /// <param name="bitmap"></param> /// <param name="outtime"></param> /// <returns></returns> bool GetImage(out Bitmap bitmap, int outtime = 3000); /// <summary> /// 软触发获取图像 /// </summary> /// <param name="bitmap"></param> /// <param name="outtime"></param> /// <returns></returns> bool GetImageWithSoftTrigger(out Bitmap bitmap, int outtime = 3000); /// <summary> /// 软触发 /// </summary> /// <returns></returns> bool SoftTrigger(); #endregion #region SettingConfig /// <summary> /// 设置相机参数 /// </summary> /// <param name="config"></param> void SetCamConfig(CamConfig config); /// <summary> /// 获取相机参数 /// </summary> /// <param name="config"></param> void GetCamConfig(out CamConfig config); /// <summary> /// 设置触发模式及触发源 /// </summary> /// <param name="mode"></param> /// <param name="triggerEnum"></param> /// <returns></returns> bool SetTriggerMode(TriggerMode mode, TriggerSource triggerEnum = TriggerSource.Line0); /// <summary> /// 获取触发模式及触发源 /// </summary> /// <param name="mode"></param> /// <param name="hardTriggerModel"></param> /// <returns></returns> bool GetTriggerMode(out TriggerMode mode, out TriggerSource hardTriggerModel); /// <summary> /// 设置曝光时长 /// </summary> /// <param name="value"></param> /// <returns></returns> bool SetExpouseTime(ushort value); /// <summary> /// 获取曝光时长 /// </summary> /// <param name="value"></param> /// <returns></returns> bool GetExpouseTime(out ushort value); /// <summary> /// 设置硬触发极性 /// </summary> /// <param name="polarity"></param> /// <returns></returns> bool SetTriggerPolarity(TriggerPolarity polarity); /// <summary> /// 获取硬触发极性 /// </summary> /// <param name="polarity"></param> /// <returns></returns> bool GetTriggerPolarity(out TriggerPolarity polarity); /// <summary> /// 设置触发滤波时间 (us) /// </summary> /// <param name="flitertime"></param> /// <returns></returns> bool SetTriggerFliter(ushort flitertime); /// <summary> /// 获取触发滤波时间 (us) /// </summary> /// <param name="flitertime"></param> /// <returns></returns> bool GetTriggerFliter(out ushort flitertime); /// <summary> /// 设置触发延时 /// </summary> /// <param name="delay"></param> /// <returns></returns> bool SetTriggerDelay(ushort delay); /// <summary> /// 获取触发延时 /// </summary> /// <param name="delay"></param> /// <returns></returns> bool GetTriggerDelay(out ushort delay); /// <summary> /// 设置增益 /// </summary> /// <param name="gain"></param> /// <returns></returns> bool SetGain(short gain); /// <summary> /// 获取增益值 /// </summary> /// <param name="gain"></param> /// <returns></returns> bool GetGain(out short gain); /// <summary> /// 设置信号线模式 /// </summary> /// <param name="line"></param> /// <param name="mode"></param> /// <returns></returns> bool SetLineMode(IOLines line, LineMode mode); /// <summary> /// 设置信号线电平状态 /// </summary> /// <param name="line"></param> /// <param name="linestatus"></param> /// <returns></returns> bool SetLineStatus(IOLines line, LineStatus linestatus); /// <summary> /// 获取信号线电平状态 /// </summary> /// <param name="line"></param> /// <param name="lineStatus"></param> /// <returns></returns> bool GetLineStatus(IOLines line, out LineStatus lineStatus); /// <summary> /// 自动白平衡 /// </summary> /// <returns></returns> bool AutoBalanceWhite(); #endregion } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。