add camera driver layer

This commit is contained in:
2025-12-26 14:54:32 +08:00
parent c6be81d1dd
commit d7ef283b9d
11 changed files with 324 additions and 469 deletions

84
LibCamera/CameraDriver.cs Normal file
View File

@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridLibray;
namespace LibCamera
{
public class CameraDriver : ICameraDriver
{
public event OnCameraImage OnCameraImage;
private IDevice m_dev_cam;
private bool camOpened;
public void Initialize(string serial, string pixelFormat, double exposure, double gain)
{
try
{
camOpened = false;
List<IDeviceInfo> deviceList = Enumerator.EnumerateDevices();
m_dev_cam = Enumerator.GetDeviceByKey(serial);
if (m_dev_cam == null)
return;
if (!m_dev_cam.Open())
return;
using (IEnumParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.ImagePixelFormat])
{
p.SetValue(pixelFormat);
}
using (IFloatParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.ExposureTime])
{
p.SetValue(exposure);
}
using (IFloatParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.GainRaw])
{
p.SetValue(gain);
}
using (IEnumParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.AcquisitionMode])
{
p.SetValue("Continuous");
}
using (IEnumParameter p = m_dev_cam.ParameterCollection[ParametrizeNameSet.TriggerMode])
{
p.SetValue("On");
}
m_dev_cam.StreamGrabber.ImageGrabbed += StreamGrabber_ImageGrabbed;
m_dev_cam.TriggerSet.Open(TriggerSourceEnum.Software);
if (!m_dev_cam.GrabUsingGrabLoopThread())
return;
camOpened = true;
}
catch (Exception ex)
{
camOpened = false;
m_dev_cam = null;
}
}
private void StreamGrabber_ImageGrabbed(object sender, GrabbedEventArgs e)
{
OnCameraImage?.Invoke(e.GrabResult.ToBitmap(true));
}
public void Stop()
{
if (!IsOpened())
return;
m_dev_cam.StreamGrabber.ImageGrabbed -= StreamGrabber_ImageGrabbed;
m_dev_cam.ShutdownGrab();
m_dev_cam.Dispose();
m_dev_cam = null;
}
public void GrabImage()
{
if (!IsOpened())
return;
m_dev_cam?.ExecuteSoftwareTrigger();
}
public bool IsOpened()
{
return m_dev_cam != null && camOpened;
}
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibCamera
{
public delegate void OnCameraImage(Bitmap bmp);
public interface ICameraDriver
{
event OnCameraImage OnCameraImage;
void Initialize(string serial, string pixelFormat, double exposure, double gain);
void Stop();
void GrabImage();
bool IsOpened();
}
}

View File

@@ -0,0 +1,75 @@
<?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>{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}</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, Version=1.0.8452.36887, Culture=neutral, PublicKeyToken=71f71440696c7e6b, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\dll\CLIDelegate.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<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="CameraDriver.cs" />
<Compile Include="ICameraDriver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View 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("37ce1f86-519d-44de-92f7-00acb78ffdf1")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -43,14 +43,19 @@ namespace LibComm
cc24.NotifyCamAcqDisabled -= CC24_NotifyCamAcqDisabled;
cc24.Shutdown();
}
public void NoticeCamComplete(int index, byte[] datax)
public void NoticeCamComplete(int index, float data)
{
cc24?.NotifyCamInspectionComplete(index, datax);
cc24?.NotifyCamInspectionComplete(index, DataConverter.FloatToByte(data, true));
cc24?.NotifyCamAcqComplete(index);
}
public void NoticeCamComplete(int index, List<float> data)
{
cc24?.NotifyCamInspectionComplete(index, DataConverter.FloatToByte(data, true));
cc24?.NotifyCamAcqComplete(index);
}
private void CC24_NewUserDataReceived(object sender, CogNdmNewUserDataEventArgs e)
{
OnDataReceived?.Invoke(cc24.ReadBytesFromPLC(0, 4));
OnDataReceived?.Invoke(DataConverter.ByteToFloat(cc24.ReadBytesFromPLC(0, 4), true));
}
private void CC24_PlcConnectionStatusChanged(object sender, CogNdmProtocolStatusChangedEventArgs e)
{

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace LibComm
{
public delegate void OnDataReceived(byte[] data);
public delegate void OnDataReceived(float data);
public delegate void OnConnectStatus(bool connected);
public delegate void OnTrigCamera(int index);
public delegate void OnCameraStatus(int index, bool enable);
@@ -18,6 +18,7 @@ namespace LibComm
event OnCameraStatus OnCameraStatus;
void Start();
void Stop();
void NoticeCamComplete(int index, byte[] datax);
void NoticeCamComplete(int index, float data);
void NoticeCamComplete(int index, List<float> data);
}
}

View File

@@ -30,6 +30,24 @@
<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="Bjcve.Comm.FFP">
<HintPath>..\dll\Bjcve.Comm.FFP.dll</HintPath>

View File

@@ -61,9 +61,7 @@
this.label2 = new System.Windows.Forms.Label();
this.Location_Display = new Cognex.VisionPro.CogRecordDisplay();
this.Ocr_picBox = new System.Windows.Forms.PictureBox();
this.btn_manualOcr = new System.Windows.Forms.Button();
this.txt_readOcrResultShow = new System.Windows.Forms.TextBox();
this.btn_manualDet = new System.Windows.Forms.Button();
this.toolStrip_Status = new System.Windows.Forms.ToolStrip();
this.ttls_PLCStatus_lbl = new System.Windows.Forms.ToolStripLabel();
this.ttls_PCLStatusShow = new System.Windows.Forms.ToolStripButton();
@@ -95,7 +93,6 @@
this.list_Log = new System.Windows.Forms.ListView();
this.panel_Manual = new System.Windows.Forms.Panel();
this.lbl_Manual = new System.Windows.Forms.Label();
this.check_Autorun = new System.Windows.Forms.CheckBox();
this.panel_OrderInformation = new System.Windows.Forms.Panel();
this.lbl_orderinfo = new System.Windows.Forms.Label();
this.panel_AutoRun = new System.Windows.Forms.Panel();
@@ -510,20 +507,6 @@
this.Ocr_picBox.TabIndex = 22;
this.Ocr_picBox.TabStop = false;
//
// btn_manualOcr
//
this.btn_manualOcr.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btn_manualOcr.BackColor = System.Drawing.Color.DodgerBlue;
this.btn_manualOcr.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btn_manualOcr.Location = new System.Drawing.Point(79, 93);
this.btn_manualOcr.Name = "btn_manualOcr";
this.btn_manualOcr.Size = new System.Drawing.Size(154, 38);
this.btn_manualOcr.TabIndex = 23;
this.btn_manualOcr.Text = "手动OCR";
this.btn_manualOcr.UseVisualStyleBackColor = false;
this.btn_manualOcr.Click += new System.EventHandler(this.btn_manualOcr_Click);
//
// txt_readOcrResultShow
//
this.txt_readOcrResultShow.Font = new System.Drawing.Font("宋体", 12F);
@@ -534,20 +517,6 @@
this.txt_readOcrResultShow.TabIndex = 24;
this.txt_readOcrResultShow.Text = "等待读取字符...";
//
// btn_manualDet
//
this.btn_manualDet.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btn_manualDet.BackColor = System.Drawing.Color.DodgerBlue;
this.btn_manualDet.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btn_manualDet.Location = new System.Drawing.Point(79, 49);
this.btn_manualDet.Name = "btn_manualDet";
this.btn_manualDet.Size = new System.Drawing.Size(154, 38);
this.btn_manualDet.TabIndex = 25;
this.btn_manualDet.Text = "手动定位";
this.btn_manualDet.UseVisualStyleBackColor = false;
this.btn_manualDet.Click += new System.EventHandler(this.btn_manualDet_Click);
//
// toolStrip_Status
//
this.toolStrip_Status.Dock = System.Windows.Forms.DockStyle.Bottom;
@@ -918,8 +887,6 @@
| System.Windows.Forms.AnchorStyles.Right)));
this.panel_Manual.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel_Manual.Controls.Add(this.lbl_Manual);
this.panel_Manual.Controls.Add(this.btn_manualOcr);
this.panel_Manual.Controls.Add(this.btn_manualDet);
this.panel_Manual.Location = new System.Drawing.Point(2, 495);
this.panel_Manual.Name = "panel_Manual";
this.panel_Manual.Size = new System.Drawing.Size(315, 151);
@@ -939,21 +906,6 @@
this.lbl_Manual.Text = "手动运行操作";
this.lbl_Manual.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// check_Autorun
//
this.check_Autorun.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.check_Autorun.Appearance = System.Windows.Forms.Appearance.Button;
this.check_Autorun.BackColor = System.Drawing.SystemColors.Control;
this.check_Autorun.BackgroundImage = global::TetraPackOCR.Properties.Resources.OFF;
this.check_Autorun.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.check_Autorun.Location = new System.Drawing.Point(79, 56);
this.check_Autorun.Name = "check_Autorun";
this.check_Autorun.Size = new System.Drawing.Size(154, 64);
this.check_Autorun.TabIndex = 29;
this.check_Autorun.UseVisualStyleBackColor = false;
this.check_Autorun.CheckedChanged += new System.EventHandler(this.check_Autorun_CheckedChanged);
//
// panel_OrderInformation
//
this.panel_OrderInformation.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
@@ -985,7 +937,6 @@
| System.Windows.Forms.AnchorStyles.Right)));
this.panel_AutoRun.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel_AutoRun.Controls.Add(this.label9);
this.panel_AutoRun.Controls.Add(this.check_Autorun);
this.panel_AutoRun.Location = new System.Drawing.Point(2, 345);
this.panel_AutoRun.Name = "panel_AutoRun";
this.panel_AutoRun.Padding = new System.Windows.Forms.Padding(1);
@@ -1257,9 +1208,7 @@
private System.Windows.Forms.Label lbl_NO;
private Cognex.VisionPro.CogRecordDisplay Location_Display;
private System.Windows.Forms.PictureBox Ocr_picBox;
private System.Windows.Forms.Button btn_manualOcr;
private System.Windows.Forms.TextBox txt_readOcrResultShow;
private System.Windows.Forms.Button btn_manualDet;
private System.Windows.Forms.ToolStrip toolStrip_Status;
private System.Windows.Forms.ToolStripLabel ttls_PLCStatus_lbl;
private System.Windows.Forms.ToolStripButton ttls_PCLStatusShow;
@@ -1290,7 +1239,6 @@
private System.Windows.Forms.Label lbl_L6_verOcrRs;
private System.Windows.Forms.Panel panel_Manual;
private System.Windows.Forms.Label lbl_Manual;
private System.Windows.Forms.CheckBox check_Autorun;
private System.Windows.Forms.Panel panel_OrderInformation;
private System.Windows.Forms.Label lbl_orderinfo;
private System.Windows.Forms.Panel panel_AutoRun;

View File

@@ -1,17 +1,4 @@
/************************
* 作者CVE-安翀岚
*
* Vision:1.0.23.1007 (发布使用)
* Windows10 X64(PaddleOCR不能部署在32位操作系统上)
* PaddleOCR 3.10
* Vpro: 9.5 SR2
*
***********************/
using AVP.CRobot;
using Bjcve.Comm.FFP;
using CLIDelegate;
using AVP.CRobot;
using Cognex.VisionPro;
using Cognex.VisionPro.Comm;
using Cognex.VisionPro.ImageProcessing;
@@ -37,10 +24,10 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Forms;
using ThridLibray;
using LibDataBase;
using LibReadTetraExcel;
using LibComm;
using LibCamera;
namespace TetraPackOCR
{
@@ -51,13 +38,11 @@ namespace TetraPackOCR
InitializeComponent();
LogInit();
}
#region
//创建字段log
ILog log = LogManager.GetLogger(typeof(Form1));
ICommPLC commPLC;
ICameraDriver cameraOCR, cameraDET;
/// <summary>
/// 声明一个PaddleOCR对象
/// </summary>
@@ -78,22 +63,13 @@ namespace TetraPackOCR
string SaveImageFileDET = System.IO.Path.GetPathRoot(Application.ExecutablePath) + "SaveImage\\Det";//ocr存图
private CogJobManager myJobManagerDET;
private CogJob myJobDET;
bool PlcContinueFlag = false; //PLC状态旗帜
int mMatchingStr = 0;//接收当前OCR拍照位置
List<double> ocrAcc = new List<double>();
/// <summary>
/// 相机对象 1和2
/// </summary>
IDevice m_dev_cam_ocr, m_dev_cam_det;
delegate void DetResultDelegate(Object Sender, CogJobManagerActionEventArgs e);
delegate void OcrResultDelegate(Bitmap bmp);
#endregion
#region
/// <summary>
/// 窗体加载
/// </summary>
@@ -103,9 +79,6 @@ namespace TetraPackOCR
{
try
{
btn_manualDet.Enabled = false;
btn_manualOcr.Enabled = false;
check_Autorun.Enabled = false;
btn_StarDet_manual.Enabled = false;
btn_StarDet_manual.BackColor = Color.LightGray;
button1.BackColor = Color.LightGray;
@@ -135,14 +108,10 @@ namespace TetraPackOCR
InitializeComm();
log.Info("模型文件加载完成");
check_Autorun.BackgroundImage = Image.FromFile(Application.StartupPath + "\\logo_image\\ON.png");
ttls_SystemStatusShow.Visible = true;
ttls_CamStatusShow.Visible = true;
Invoke(new Action(() =>
{
btn_manualOcr.Enabled = true;
btn_manualDet.Enabled = true;
check_Autorun.Enabled = true;
Enabled = true;
btn_OrderNum.Enabled = true;
btn_OrderNum.BackColor = Color.DeepSkyBlue;
@@ -177,7 +146,6 @@ namespace TetraPackOCR
log.Error(ex.Message);
}
}
/// <summary>
/// 窗体关闭
/// </summary>
@@ -185,7 +153,6 @@ namespace TetraPackOCR
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
ClossCam();
@@ -208,11 +175,8 @@ namespace TetraPackOCR
System.Environment.Exit(0);
}
}
#endregion
#region
private void LogInit()
{
try
@@ -229,230 +193,52 @@ namespace TetraPackOCR
MessageBox.Show(string.Format("配置日志出错:{0}", ex.Message));
}
}
#endregion
#region
#region 1 OCR相机
private void InitializeCamerOCR()
{
try
{
camOCROpened = false;
List<IDeviceInfo> deviceList = Enumerator.EnumerateDevices(); //发现设备,搜索所有大华相机
m_dev_cam_ocr = Enumerator.GetDeviceByKey("Machine Vision:CK21686DAK00001");//通过"设备厂商名:设备序列号"获取
if (m_dev_cam_ocr == null)
{
log.Error("未发现OCR相机请检查相机连接");
return;
}
m_dev_cam_ocr.CameraOpened += m_dev_cam_ocr_CameraOpened;
m_dev_cam_ocr.CameraClosed += m_dev_cam_ocr_CameraClosed;
m_dev_cam_ocr.ConnectionLost += m_dev_cam_ocr_ConnectionLost;
if (!m_dev_cam_ocr.Open())
{
MessageBox.Show("OCR相机打开失败");
}
// 设置图像格式
using (IEnumParameter p = m_dev_cam_ocr.ParameterCollection[ParametrizeNameSet.ImagePixelFormat])
{
p.SetValue("BayerRG8");
}
// 设置曝光
using (IFloatParameter p = m_dev_cam_ocr.ParameterCollection[ParametrizeNameSet.ExposureTime])
{
p.SetValue(500000);
}
// 设置增益
using (IFloatParameter p = m_dev_cam_ocr.ParameterCollection[ParametrizeNameSet.GainRaw])
{
p.SetValue(2.5);
}
using (IEnumParameter p = m_dev_cam_ocr.ParameterCollection[ParametrizeNameSet.AcquisitionMode])
{
p.SetValue("Continuous");
}
using (IEnumParameter p = m_dev_cam_ocr.ParameterCollection[ParametrizeNameSet.TriggerMode])
{
p.SetValue("On");
}
m_dev_cam_ocr.StreamGrabber.ImageGrabbed += StreamGrabber_ImageGrabbed_OCR;
m_dev_cam_ocr.StreamGrabber.GrabStarted += StreamGrabber_GrabStarted_OCR;
// 打开Software Trigger
m_dev_cam_ocr.TriggerSet.Open(TriggerSourceEnum.Software);
if (!m_dev_cam_ocr.GrabUsingGrabLoopThread())
{
// 开启采集失败
log.Error("开启采集失败");
return;
}
camOCROpened = true;
cameraOCR = new CameraDriver();
cameraOCR.OnCameraImage += CameraOCR_OnCameraImage;
cameraOCR.Initialize("Machine Vision:CK21686DAK00001", "BayerRG8", 500000, 2.5);
if (cameraOCR.IsOpened())
log.Info("OCR相机加载完毕");
}
catch (Exception ex)
{
camOCROpened = false;
log.Error("OCR相机加载失败:" + ex.Message);
m_dev_cam_ocr = null;
}
}
private bool camOCROpened = false;
private bool camDETOpened = false;
#endregion
#region 1
void m_dev_cam_ocr_ConnectionLost(object sender, EventArgs e)
{
log.Error(m_dev_cam_ocr.DeviceInfo.Key + "OCR相机断线");
else
log.Error("OCR相机加载失败");
}
void m_dev_cam_ocr_CameraClosed(object sender, EventArgs e)
{
log.Error(m_dev_cam_ocr.DeviceInfo.Key + "OCR相机关闭");
}
void m_dev_cam_ocr_CameraOpened(object sender, EventArgs e)
{
}
void StreamGrabber_GrabStarted_OCR(object sender, EventArgs e)
{
log.Info("OCR相机启动码流");
}
/// <summary>
/// 拍照完成后触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StreamGrabber_ImageGrabbed_OCR(object sender, GrabbedEventArgs e)
private void CameraOCR_OnCameraImage(Bitmap bmp)
{
if (bmp == null)
return;
try
{
OCRResult(e.GrabResult.ToBitmap(true));
OCRResult(bmp);
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
}
#endregion
#endregion
#region 2
private void InitializeCamerDET()
{
try
{
camDETOpened = false;
List<IDeviceInfo> deviceList = Enumerator.EnumerateDevices(); //发现设备,搜索所有大华相机
m_dev_cam_det = Enumerator.GetDeviceByKey("Machine Vision:BK27185BAK00038");//通过"设备厂商名:设备序列号"获取
if (m_dev_cam_det == null)
{
log.Error("未发现OCR相机请检查相机连接");
return;
}
m_dev_cam_det.CameraOpened += m_dev_cam_det_CameraOpened;
m_dev_cam_det.CameraClosed += m_dev_cam_det_CameraClosed;
m_dev_cam_det.ConnectionLost += m_dev_cam_det_ConnectionLost;
if (!m_dev_cam_det.Open())
{
MessageBox.Show("定位相机打开失败");
}
// 设置图像格式
using (IEnumParameter p = m_dev_cam_det.ParameterCollection[ParametrizeNameSet.ImagePixelFormat])
{
p.SetValue("Mono");
}
// 设置曝光
using (IFloatParameter p = m_dev_cam_det.ParameterCollection[ParametrizeNameSet.ExposureTime])
{
p.SetValue(30000);
}
// 设置增益
using (IFloatParameter p = m_dev_cam_det.ParameterCollection[ParametrizeNameSet.GainRaw])
{
p.SetValue(1.0);
}
using (IEnumParameter p = m_dev_cam_det.ParameterCollection[ParametrizeNameSet.AcquisitionMode])
{
p.SetValue("Continuous");
}
using (IEnumParameter p = m_dev_cam_det.ParameterCollection[ParametrizeNameSet.TriggerMode])
{
p.SetValue("On");
}
m_dev_cam_det.StreamGrabber.ImageGrabbed += StreamGrabber_ImageGrabbed_DET;
m_dev_cam_det.StreamGrabber.GrabStarted += StreamGrabber_GrabStarted_DET;
// 打开Software Trigger
// Set Software Trigger
m_dev_cam_det.TriggerSet.Open(TriggerSourceEnum.Software);
if (!m_dev_cam_det.GrabUsingGrabLoopThread())
{
// 开启采集失败
log.Error("开启采集失败");
return;
}
camDETOpened = true;
log.Info("定位相机加载完毕");
}
catch (Exception ex)
{
camDETOpened = false;
log.Error("定位相机加载失败:" + ex.Message);
m_dev_cam_det = null;
}
cameraDET = new CameraDriver();
cameraDET.OnCameraImage += CameraDET_OnCameraImage;
cameraDET.Initialize("Machine Vision:BK27185BAK00038", "Mono", 30000, 1.0);
if (cameraDET.IsOpened())
log.Info("定位相机相机加载完毕");
else
log.Error("定位相机相机加载失败");
}
#endregion
#region 2
void m_dev_cam_det_ConnectionLost(object sender, EventArgs e)
{
log.Error(m_dev_cam_det.DeviceInfo.Key + "定位相机断线");
}
void m_dev_cam_det_CameraClosed(object sender, EventArgs e)
{
log.Error(m_dev_cam_det.DeviceInfo.Key + "定位相机关闭");
}
void m_dev_cam_det_CameraOpened(object sender, EventArgs e)
{
}
void StreamGrabber_GrabStarted_DET(object sender, EventArgs e)
{
log.Info("定位相机启动码流");
}
/// <summary>
/// 拍照完成后触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StreamGrabber_ImageGrabbed_DET(object sender, GrabbedEventArgs e)
private void CameraDET_OnCameraImage(Bitmap bmp)
{
try
{
Bitmap bmp = null;
bmp = e.GrabResult.ToBitmap(true);
//Bitmap bmp = new Bitmap(cogimg.ToBitmap());
if (bmp == null)
return;
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer();
@@ -471,7 +257,6 @@ namespace TetraPackOCR
fs.Close();
log.Info("定位存图已完成");
CogImage8Grey cogimage = new CogImage8Grey(bmp);
//把图像给job中convertImagetool
@@ -489,9 +274,7 @@ namespace TetraPackOCR
log.Error(ex.ToString());
}
}
#endregion
private void EnableStartDetect()
{
if (InvokeRequired)
@@ -510,29 +293,11 @@ namespace TetraPackOCR
#region
void ClossCam()
{
//注销相机事件
if (m_dev_cam_ocr != null)
{
m_dev_cam_ocr.CameraOpened -= m_dev_cam_ocr_CameraOpened;
m_dev_cam_ocr.CameraClosed -= m_dev_cam_ocr_CameraClosed;
m_dev_cam_ocr.ConnectionLost -= m_dev_cam_ocr_ConnectionLost;
m_dev_cam_ocr.ShutdownGrab();
m_dev_cam_ocr.Dispose();
m_dev_cam_ocr = null;
}
if (m_dev_cam_det != null)
{
m_dev_cam_det.CameraOpened -= m_dev_cam_det_CameraOpened;
m_dev_cam_det.CameraClosed -= m_dev_cam_det_CameraClosed;
m_dev_cam_det.ConnectionLost -= m_dev_cam_det_ConnectionLost;
m_dev_cam_det.ShutdownGrab();
m_dev_cam_det.Dispose();
m_dev_cam_det = null;
}
cameraDET?.Stop();
cameraOCR?.Stop();
}
#endregion
#endregion
#region OCR模型参数初始化
private void InitializePaddleOCR()
{
@@ -549,40 +314,8 @@ namespace TetraPackOCR
log.Error(ex.Message + ex.StackTrace);
}
}
#endregion
#region
/// <summary>
/// 自动运行状态改变按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void check_Autorun_CheckedChanged(object sender, EventArgs e)
{
if (check_Autorun.Checked)
{
log.Info("PC断开PLC连接,只可进行本地操作。");
check_Autorun.BackgroundImage = Image.FromFile(Application.StartupPath + "\\logo_image\\OFF.png");
panel_Manual.Visible = true;
btn_StarDet_manual.Enabled = false;
btn_StarDet_manual.BackColor = Color.LightGray;
ttls_PCLStatusShow.Visible = false;
}
else if (!check_Autorun.Checked)
{
log.Info("PC加载PLC已完成");
check_Autorun.BackgroundImage = Image.FromFile(Application.StartupPath + "\\logo_image\\ON.png");
panel_Manual.Visible = false;
if (orderLoaded)
{
btn_StarDet_manual.Enabled = true;
btn_StarDet_manual.BackColor = Color.LimeGreen;
}
ttls_PCLStatusShow.Visible = true;
}
}
private bool orderLoaded = false;
private Dictionary<int, List<string>> ocrTextRequest = new Dictionary<int, List<string>>();
private Dictionary<int, string> ocrTextDesign = new Dictionary<int, string>();
@@ -862,32 +595,11 @@ namespace TetraPackOCR
break;
}
}
/// <summary>
/// 手动ocr
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_manualOcr_Click(object sender, EventArgs e)
{
m_dev_cam_ocr?.ExecuteSoftwareTrigger();//相机1触发 = OCR拍照
log.Info("手动触发OCR");
}
/// <summary>
/// 手动定位
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_manualDet_Click(object sender, EventArgs e)
{
m_dev_cam_det?.ExecuteSoftwareTrigger();
log.Info("手动触发定位");
}
private bool autorunFlag = false;
private double m_textWidth = 0;
private DateTime m_startTime = DateTime.Now;
private void btn_StarDet_manual_Click(object sender, EventArgs e)
{
if (camOCROpened == false || camDETOpened == false)
if (cameraDET?.IsOpened() == false || cameraOCR?.IsOpened() == false)
{
MessageBox.Show("相机未打开,无法进行检测,请检查相机连接及状态!");
return;
@@ -916,14 +628,13 @@ namespace TetraPackOCR
m_startTime = DateTime.Now;
m_textWidth = 18 - (config.NumberOfLanes - 5);
ocrAcc.Clear();
autorunFlag = check_Autorun.Checked;
btn_StarDet_manual.Enabled = false;
button2.Enabled = false;
button1.Enabled = true;
btn_StarDet_manual.BackColor = Color.LightGray;
button2.BackColor = Color.LightGray;
button1.BackColor = Color.LimeGreen;
m_dev_cam_det?.ExecuteSoftwareTrigger();
cameraDET.GrabImage();
list_Log.Clear();
log.Info("手动触发开始");
m_BeginOCR = true;
@@ -958,29 +669,7 @@ namespace TetraPackOCR
}
}
#endregion
#region OCR相机拍照触发处理函数
/// <summary>
/// OCR相机拍照信号过来需要进行的操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OcrCamTriger()
{
try
{
log.Info("PLC触发OCR相机正在拍照计算...");
m_dev_cam_ocr?.ExecuteSoftwareTrigger();
}
catch (Exception ex)
{
log.Error(ex.Message);
}
}
#endregion
#region
#region
public void DetResult(object sender, CogJobManagerActionEventArgs e)
{
@@ -1029,7 +718,7 @@ namespace TetraPackOCR
if (xx > 350)
{
log.Error("定位横向轴偏左,疑似倒数第二张图,请重新摆放后识别");
NoticePLCCompleteDet(DataConverter.FloatToByte(0.0f, true));
NoticePLCCompleteDet(0.0f);
}
else
SendToPLC(xx, yy, rr);
@@ -1044,10 +733,9 @@ namespace TetraPackOCR
{
EnableStartDetect();
log.Error(ex.Message + "未检测到K标志");
NoticePLCCompleteDet(DataConverter.FloatToByte(0.0f, true));
NoticePLCCompleteDet(0.0f);
}
}
/// <summary>
/// 获取右下角定位标志坐标
/// </summary>
@@ -1101,7 +789,6 @@ namespace TetraPackOCR
return i;
}
#endregion
#region
List<TextPoint> paixu(List<TextPoint> points)
{
@@ -1139,9 +826,7 @@ namespace TetraPackOCR
return sortedPoints;
}
#endregion
#region OCR结果处理
private void OCRResult(Bitmap bmp)
{
@@ -1335,13 +1020,15 @@ namespace TetraPackOCR
}
private void NoticePLCCompleteOCR()
{
if (autorunFlag == false)
commPLC?.NoticeCamComplete(1, DataConverter.FloatToByte(0.0f, true));
commPLC?.NoticeCamComplete(1, 0.0f);
}
private void NoticePLCCompleteDet(byte[] datax)
private void NoticePLCCompleteDet(float data)
{
if (autorunFlag == false)
commPLC?.NoticeCamComplete(0, datax);
commPLC?.NoticeCamComplete(0, data);
}
private void NoticePLCCompleteDet(List<float> data)
{
commPLC?.NoticeCamComplete(0, data);
}
private OCRTextResult OCRBytes(byte[] ocrimagebyte)
{
@@ -1394,7 +1081,6 @@ namespace TetraPackOCR
return null;
}
}
#region
private void CutPicture(String picPath, int x, int y, int width, int height)
{
@@ -1427,9 +1113,6 @@ namespace TetraPackOCR
bmpCrop.Dispose();
}
#endregion
/// <summary>
/// 使用正则表达式提取结果
/// </summary>
@@ -1483,7 +1166,6 @@ namespace TetraPackOCR
button2.BackColor = Color.LimeGreen;
NoticePLCCompleteOCR();
}
/// <summary>
/// 将结果显示在对应状态栏
/// </summary>
@@ -1632,11 +1314,8 @@ namespace TetraPackOCR
}
}
#endregion
#endregion
#region
#region
void InitializeComm()
{
@@ -1655,7 +1334,6 @@ namespace TetraPackOCR
}
}
private void CommPLC_OnCameraStatus(int index, bool enable)
{
if (InvokeRequired)
@@ -1668,18 +1346,15 @@ namespace TetraPackOCR
else
ttls_AcqEnableShow.Visible = false;
}
private void CommPLC_OnDataReceived(byte[] data)
private void CommPLC_OnDataReceived(float data)
{
if (InvokeRequired)
{
BeginInvoke(new Action<byte[]>(CommPLC_OnDataReceived), data);
BeginInvoke(new Action<float>(CommPLC_OnDataReceived), data);
return;
}
float mMatchingStrf = DataConverter.ByteToFloat(data, true);
mMatchingStr = Convert.ToInt32(mMatchingStrf);
mMatchingStr = Convert.ToInt32(data);
log.Info("PC接收PLC数据数据内容" + mMatchingStr);
if (m_BeginOCR == false && m_GotoZero == false)
{
@@ -1716,7 +1391,6 @@ namespace TetraPackOCR
log.Info("当前检测合格,正常结束");
}
}
private void CommPLC_OnTrigCamera(int index)
{
if (InvokeRequired)
@@ -1730,7 +1404,8 @@ namespace TetraPackOCR
{
Ocr_picBox.BackgroundImage = null;
Thread.Sleep(1000);
OcrCamTriger();
log.Info("PLC触发OCR相机正在拍照计算...");
cameraOCR?.GrabImage();
}
}
catch (Exception ex)
@@ -1738,7 +1413,6 @@ namespace TetraPackOCR
log.Error($"Trigger AcqStart Error: {ex.Message}");
}
}
private void CommPLC_OnConnectStatus(bool connected)
{
if (InvokeRequired)
@@ -1764,27 +1438,21 @@ namespace TetraPackOCR
}
}
#endregion
private bool m_GotoZero = false;
private void button2_Click(object sender, EventArgs e)
{
autorunFlag = check_Autorun.Checked;
if (check_Autorun.Checked == false)
{
button1.Enabled = true;
button1.BackColor = Color.LimeGreen;
btn_StarDet_manual.Enabled = false;
btn_StarDet_manual.BackColor = Color.LightGray;
m_GotoZero = true;
List<float> plcXY = new List<float>();
plcXY.Add(1);
plcXY.Add(0);
plcXY.Add(0);
NoticePLCCompleteDet(DataConverter.FloatToByte(plcXY, true));
log.Info("零点坐标已发送");
}
button1.Enabled = true;
button1.BackColor = Color.LimeGreen;
btn_StarDet_manual.Enabled = false;
btn_StarDet_manual.BackColor = Color.LightGray;
m_GotoZero = true;
List<float> plcXY = new List<float>();
plcXY.Add(1);
plcXY.Add(0);
plcXY.Add(0);
NoticePLCCompleteDet(plcXY);
log.Info("零点坐标已发送");
}
#region
private void SendToPLC(double x, double y, double r)
{
@@ -1824,7 +1492,7 @@ namespace TetraPackOCR
log.Error("横向范围 0,1600纸张向左移动一点");
else
log.Error("横向范围 0,1600纸张向右移动一点");
NoticePLCCompleteDet(DataConverter.FloatToByte(0.0f, true));
NoticePLCCompleteDet(0.0f);
return;
}
@@ -1848,41 +1516,37 @@ namespace TetraPackOCR
log.Error("纵向范围 -50,400纸张向下移动一点");
else
log.Error("纵向范围 -50,400纸张向上移动一点");
NoticePLCCompleteDet(DataConverter.FloatToByte(0.0f, true));
NoticePLCCompleteDet(0.0f);
return;
}
locationXY.Add((float)ocrx);
locationXY.Add((float)ocry);
}
if (check_Autorun.Checked == false)
List<float> plcXY = new List<float>();
for (int i = 0; i < locationXY.Count; i++)
{
List<float> plcXY = new List<float>();
for (int i = 0; i < locationXY.Count; i++)
if (i == 0)
{
if (i == 0)
{
plcXY.Add(locationXY[i]);
}
else if (i % 2 == 1)
{
plcXY.Add(locationXY[locationXY.Count - i - 1]);
plcXY.Add(locationXY[locationXY.Count - i]);
}
plcXY.Add(locationXY[i]);
}
else if (i % 2 == 1)
{
plcXY.Add(locationXY[locationXY.Count - i - 1]);
plcXY.Add(locationXY[locationXY.Count - i]);
}
NoticePLCCompleteDet(DataConverter.FloatToByte(plcXY, true));
log.Info("坐标已发送完成。");
}
NoticePLCCompleteDet(plcXY);
log.Info("坐标已发送完成。");
}
catch (Exception ex)
{
EnableStartDetect();
log.Error(ex.Message + "未检测到K标志");
NoticePLCCompleteDet(DataConverter.FloatToByte(0.0f, true));
NoticePLCCompleteDet(0.0f);
}
}
#endregion
#region
private void StopComm()
{
@@ -1902,7 +1566,6 @@ namespace TetraPackOCR
}
}
#endregion
#endregion
}
}

View File

@@ -79,9 +79,6 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\dll\Bjcve.Comm.FFP.dll</HintPath>
</Reference>
<Reference Include="CLIDelegate">
<HintPath>..\dll\CLIDelegate.dll</HintPath>
</Reference>
<Reference Include="Cognex.VisionPro, Version=59.2.0.0, Culture=neutral, PublicKeyToken=ef0f902af9dee505, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\dll\Cognex.VisionPro.dll</HintPath>
@@ -200,9 +197,6 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="ThridLibray">
<HintPath>..\dll\ThridLibray.dll</HintPath>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
@@ -265,6 +259,10 @@
<None Include="Resources\OFF.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LibCamera\LibCamera.csproj">
<Project>{37ce1f86-519d-44de-92f7-00acb78ffdf1}</Project>
<Name>LibCamera</Name>
</ProjectReference>
<ProjectReference Include="..\LibComm\LibComm.csproj">
<Project>{e44b1774-093e-4617-af7a-474a88b89100}</Project>
<Name>LibComm</Name>

View File

@@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestPackOCR", "TestPackOCR\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibComm", "LibComm\LibComm.csproj", "{E44B1774-093E-4617-AF7A-474A88B89100}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibCamera", "LibCamera\LibCamera.csproj", "{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -67,12 +69,20 @@ Global
{717BA61B-8C31-473F-8A02-626337A90A5E}.Release|x64.Build.0 = Release|Any CPU
{E44B1774-093E-4617-AF7A-474A88B89100}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E44B1774-093E-4617-AF7A-474A88B89100}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E44B1774-093E-4617-AF7A-474A88B89100}.Debug|x64.ActiveCfg = Debug|Any CPU
{E44B1774-093E-4617-AF7A-474A88B89100}.Debug|x64.Build.0 = Debug|Any CPU
{E44B1774-093E-4617-AF7A-474A88B89100}.Debug|x64.ActiveCfg = Debug|x64
{E44B1774-093E-4617-AF7A-474A88B89100}.Debug|x64.Build.0 = Debug|x64
{E44B1774-093E-4617-AF7A-474A88B89100}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E44B1774-093E-4617-AF7A-474A88B89100}.Release|Any CPU.Build.0 = Release|Any CPU
{E44B1774-093E-4617-AF7A-474A88B89100}.Release|x64.ActiveCfg = Release|Any CPU
{E44B1774-093E-4617-AF7A-474A88B89100}.Release|x64.Build.0 = Release|Any CPU
{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}.Debug|x64.ActiveCfg = Debug|x64
{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}.Debug|x64.Build.0 = Debug|x64
{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}.Release|Any CPU.Build.0 = Release|Any CPU
{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}.Release|x64.ActiveCfg = Release|Any CPU
{37CE1F86-519D-44DE-92F7-00ACB78FFDF1}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -81,6 +91,7 @@ Global
{B88B2FA9-0D9D-4EBB-A87A-4DE2DC2DD70F} = {C0079401-F420-4256-B991-3BF16D3296C7}
{D03A85CC-A53B-4434-A560-7A89563292E8} = {C0079401-F420-4256-B991-3BF16D3296C7}
{E44B1774-093E-4617-AF7A-474A88B89100} = {C0079401-F420-4256-B991-3BF16D3296C7}
{37CE1F86-519D-44DE-92F7-00ACB78FFDF1} = {C0079401-F420-4256-B991-3BF16D3296C7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F550CA1D-FC09-446C-B501-170B642AAC00}