prepare reset
This commit is contained in:
31
LibCamera/IImageCollector.cs
Normal file
31
LibCamera/IImageCollector.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Drawing;
|
||||
|
||||
namespace LibCamera
|
||||
{
|
||||
public delegate void OnImageCollectorMessage(string msg);
|
||||
public delegate void OnImageCollectorInfo(string info);
|
||||
public delegate void OnImageCollectorConnected(string serial);
|
||||
public delegate void OnImageCollectorDisConnected(string serial);
|
||||
public delegate void OnImageCollectorData(byte[] imageData);
|
||||
public interface IImageCollector
|
||||
{
|
||||
event OnImageCollectorMessage OnImageCollectorMessage;
|
||||
event OnImageCollectorInfo OnImageCollectorInfo;
|
||||
event OnImageCollectorConnected OnImageCollectorConnected;
|
||||
event OnImageCollectorDisConnected OnImageCollectorDisConnected;
|
||||
event OnImageCollectorData OnImageCollectorData;
|
||||
void Start();
|
||||
void Stop();
|
||||
void SetPixelFormat(string pixelFormat);
|
||||
void SetExposureTime(double exposureTime);
|
||||
void SetGainRawGain(double gain);
|
||||
void SetAcquisitionMode(string acquisitionMode);
|
||||
void SetTriggerMode(string triggerMode);
|
||||
void StartSoftwareGrabbing();
|
||||
}
|
||||
}
|
||||
152
LibCamera/ImageCollector.cs
Normal file
152
LibCamera/ImageCollector.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ThridLibray;
|
||||
|
||||
namespace LibCamera
|
||||
{
|
||||
public class ImageCollector : IImageCollector
|
||||
{
|
||||
public event OnImageCollectorMessage OnImageCollectorMessage;
|
||||
public event OnImageCollectorInfo OnImageCollectorInfo;
|
||||
public event OnImageCollectorConnected OnImageCollectorConnected;
|
||||
public event OnImageCollectorDisConnected OnImageCollectorDisConnected;
|
||||
public event OnImageCollectorData OnImageCollectorData;
|
||||
private IDevice m_dev_cam;
|
||||
private bool m_abort = false;
|
||||
private DateTime m_start = DateTime.Now;
|
||||
private string m_serial;
|
||||
public ImageCollector(string serial)
|
||||
{
|
||||
m_serial = serial;
|
||||
}
|
||||
public void Start()
|
||||
{
|
||||
m_abort = false;
|
||||
Task.Run(() => { ConnectDevice(); });
|
||||
}
|
||||
private void ConnectDevice()
|
||||
{
|
||||
m_start = DateTime.Now.AddSeconds(-6);
|
||||
while (true)
|
||||
{
|
||||
if (m_abort)
|
||||
return;
|
||||
Thread.Sleep(100);
|
||||
try
|
||||
{
|
||||
if (m_dev_cam == null || (m_dev_cam.IsOpen == false && ((DateTime.Now - m_start).TotalSeconds >= 5)))
|
||||
{
|
||||
OnImageCollectorDisConnected?.Invoke(m_serial);
|
||||
if (m_dev_cam != null && !m_dev_cam.IsOpen)
|
||||
m_dev_cam.StreamGrabber.ImageGrabbed -= StreamGrabber_ImageGrabbed;
|
||||
m_dev_cam = null;
|
||||
Enumerator.EnumerateDevices();
|
||||
m_dev_cam = Enumerator.GetDeviceByKey(m_serial);
|
||||
if (m_dev_cam == null)
|
||||
continue;
|
||||
m_dev_cam.Open();
|
||||
if (m_dev_cam.IsOpen)
|
||||
{
|
||||
m_dev_cam.StreamGrabber.ImageGrabbed += StreamGrabber_ImageGrabbed;
|
||||
OnImageCollectorInfo?.Invoke($"Device {m_serial} connected.");
|
||||
OnImageCollectorConnected?.Invoke(m_serial);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
m_dev_cam = null;
|
||||
OnImageCollectorMessage?.Invoke($"ConnectDevice {m_serial} Exception: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void StreamGrabber_ImageGrabbed(object sender, GrabbedEventArgs e)
|
||||
{
|
||||
OnImageCollectorData?.Invoke(e.GrabResult.Image);
|
||||
}
|
||||
public void Stop()
|
||||
{
|
||||
try
|
||||
{
|
||||
m_abort = true;
|
||||
if (m_dev_cam != null)
|
||||
{
|
||||
m_dev_cam.StreamGrabber.ImageGrabbed -= StreamGrabber_ImageGrabbed;
|
||||
if (m_dev_cam.IsOpen)
|
||||
{
|
||||
m_dev_cam.Close();
|
||||
}
|
||||
m_dev_cam = null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnImageCollectorMessage?.Invoke($"Stop {m_serial} Exception: {ex.Message}");
|
||||
}
|
||||
}
|
||||
private bool IsDeviceConnected()
|
||||
{
|
||||
return m_dev_cam != null && m_dev_cam.IsOpen;
|
||||
}
|
||||
public void SetPixelFormat(string pixelFormat)
|
||||
{
|
||||
if (!IsDeviceConnected())
|
||||
return;
|
||||
using (IEnumParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.ImagePixelFormat])
|
||||
{
|
||||
p.SetValue(pixelFormat);
|
||||
}
|
||||
}
|
||||
public void SetExposureTime(double exposureTime)
|
||||
{
|
||||
if (!IsDeviceConnected())
|
||||
return;
|
||||
using (IFloatParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.ExposureTime])
|
||||
{
|
||||
p.SetValue(exposureTime);
|
||||
}
|
||||
}
|
||||
public void SetGainRawGain(double gain)
|
||||
{
|
||||
if (!IsDeviceConnected())
|
||||
return;
|
||||
using (IFloatParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.GainRaw])
|
||||
{
|
||||
p.SetValue(gain);
|
||||
}
|
||||
}
|
||||
public void SetAcquisitionMode(string acquisitionMode)
|
||||
{
|
||||
if (!IsDeviceConnected())
|
||||
return;
|
||||
using (IEnumParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.AcquisitionMode])
|
||||
{
|
||||
p.SetValue(acquisitionMode);
|
||||
}
|
||||
}
|
||||
public void SetTriggerMode(string triggerMode)
|
||||
{
|
||||
if (!IsDeviceConnected())
|
||||
return;
|
||||
using (IEnumParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.TriggerMode])
|
||||
{
|
||||
p.SetValue(triggerMode);
|
||||
}
|
||||
}
|
||||
public void StartSoftwareGrabbing()
|
||||
{
|
||||
if (!IsDeviceConnected())
|
||||
return;
|
||||
m_dev_cam.TriggerSet.Open(TriggerSourceEnum.Software);
|
||||
if (!m_dev_cam.GrabUsingGrabLoopThread())
|
||||
OnImageCollectorMessage("开启采集失败");
|
||||
else
|
||||
OnImageCollectorInfo($"相机{m_serial}加载完毕");
|
||||
}
|
||||
}
|
||||
}
|
||||
73
LibCamera/LibCamera.csproj
Normal file
73
LibCamera/LibCamera.csproj
Normal file
@@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{884C8BB2-78D9-4EED-A2FA-492F075E1F64}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>LibCamera</RootNamespace>
|
||||
<AssemblyName>LibCamera</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<OutputPath>bin\x64\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<DebugType>full</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
|
||||
<OutputPath>bin\x64\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="CLIDelegate">
|
||||
<HintPath>..\dll\CLIDelegate.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="ThridLibray">
|
||||
<HintPath>..\dll\ThridLibray.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="IImageCollector.cs" />
|
||||
<Compile Include="ImageCollector.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
33
LibCamera/Properties/AssemblyInfo.cs
Normal file
33
LibCamera/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("LibCamera")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("HP")]
|
||||
[assembly: AssemblyProduct("LibCamera")]
|
||||
[assembly: AssemblyCopyright("Copyright © HP 2025")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("884c8bb2-78d9-4eed-a2fa-492f075e1f64")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user