|
|
|
|
using HalconDotNet;
|
|
|
|
|
using MvCamCtrl.NET;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Rs.Cameras
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 方向
|
|
|
|
|
/// </summary>
|
|
|
|
|
public enum EDir
|
|
|
|
|
{
|
|
|
|
|
X,
|
|
|
|
|
Y
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum ETriggerSource
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 硬触发
|
|
|
|
|
/// </summary>
|
|
|
|
|
Line0,
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 软触发
|
|
|
|
|
/// </summary>
|
|
|
|
|
Software
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract class BaseCameraInfo
|
|
|
|
|
{
|
|
|
|
|
public MyCamera Camera { get; set; }
|
|
|
|
|
public string CameraName { get; set; }
|
|
|
|
|
|
|
|
|
|
public int Index { get; set; }
|
|
|
|
|
|
|
|
|
|
private List<HObject> Images;
|
|
|
|
|
|
|
|
|
|
int apiResult = -1;
|
|
|
|
|
|
|
|
|
|
public BaseCameraInfo()
|
|
|
|
|
{
|
|
|
|
|
Images = new List<HObject>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Deinit()
|
|
|
|
|
{
|
|
|
|
|
if (Camera != null)
|
|
|
|
|
{
|
|
|
|
|
Camera.MV_CC_CloseDevice_NET();
|
|
|
|
|
Camera.MV_CC_DestroyDevice_NET();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 清除内存中的图片
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void ClearImage()
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
if (Images != null)
|
|
|
|
|
{
|
|
|
|
|
Images.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 取图
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Grab()
|
|
|
|
|
{
|
|
|
|
|
if (Camera != null)
|
|
|
|
|
return Camera.MV_CC_SetCommandValue_NET("TriggerSoftware");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int StartGrabbing()
|
|
|
|
|
{
|
|
|
|
|
if (Camera != null)
|
|
|
|
|
return Camera.MV_CC_StartGrabbing_NET();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddImage(HObject image)
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
Images.Add(image);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置曝光时间
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="timer"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int SetExposure(float timer)
|
|
|
|
|
{
|
|
|
|
|
if (Camera != null)
|
|
|
|
|
{
|
|
|
|
|
apiResult = Camera.MV_CC_SetEnumValue_NET("ExposureAuto", 0);
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return apiResult;
|
|
|
|
|
}
|
|
|
|
|
apiResult = Camera.MV_CC_SetFloatValue_NET("ExposureTime", timer);
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
{
|
|
|
|
|
return apiResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置相机增益
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="camName"></param>
|
|
|
|
|
/// <param name="gain"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int SetGain(float gain)
|
|
|
|
|
{
|
|
|
|
|
if (Camera != null)
|
|
|
|
|
{
|
|
|
|
|
apiResult = Camera.MV_CC_SetEnumValue_NET("GainAuto", 0);
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
{
|
|
|
|
|
return apiResult;
|
|
|
|
|
}
|
|
|
|
|
apiResult = Camera.MV_CC_SetFloatValue_NET("Gain", gain);
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
{
|
|
|
|
|
return apiResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置相机的触发模式
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="camName"></param>
|
|
|
|
|
/// <param name="triggerSource"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int SetTrigger(ETriggerSource triggerSource = ETriggerSource.Line0)
|
|
|
|
|
{
|
|
|
|
|
if (Camera != null)
|
|
|
|
|
{
|
|
|
|
|
if (triggerSource == ETriggerSource.Line0)
|
|
|
|
|
{
|
|
|
|
|
apiResult = Camera.MV_CC_SetEnumValue_NET("TriggerSource", (uint)MyCamera.MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_LINE0);
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
{
|
|
|
|
|
apiResult = Camera.MV_CC_SetEnumValue_NET("TriggerActivation", 0);//设置上升沿触发
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
{
|
|
|
|
|
return apiResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return apiResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
apiResult = Camera.MV_CC_SetEnumValue_NET("TriggerSource", (uint)MyCamera.MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_SOFTWARE);
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
{
|
|
|
|
|
return apiResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置相机XY方向是否反转
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="camName"></param>
|
|
|
|
|
/// <param name="dir"></param>
|
|
|
|
|
/// <param name="reverse"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int SetReverse(EDir dir = EDir.X, bool reverse = false)
|
|
|
|
|
{
|
|
|
|
|
if (Camera != null)
|
|
|
|
|
{
|
|
|
|
|
if (dir == EDir.X)
|
|
|
|
|
{
|
|
|
|
|
apiResult = Camera.MV_CC_SetBoolValue_NET("ReverseX", reverse);
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
return apiResult;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
apiResult = Camera.MV_CC_SetBoolValue_NET("ReverseY", reverse);
|
|
|
|
|
if (apiResult != 0)
|
|
|
|
|
return apiResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取最后一张图片
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public HObject GetLastImage()
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
if (Images != null && Images.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
return Images[Images.Count - 1];
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取所有的图片
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public List<HObject> GetAllImages()
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
return Images;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取图片数量
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int GetImageCount()
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
if (Images != null && Images.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
return Images.Count;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|