完成取图和通信

This commit is contained in:
moco 2024-09-26 01:41:50 +08:00
parent 25382615ac
commit cc6859e814
14 changed files with 1176 additions and 293646 deletions

Binary file not shown.

BIN
Dlls/halcon.dll Normal file

Binary file not shown.

BIN
Dlls/halcondotnet.dll Normal file

Binary file not shown.

View File

@ -1,87 +0,0 @@
using Camera;
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 PanSight3DForm.Model;
namespace PanSight3DForm
{
public partial class DetectForm : Form
{
CameraManger cameraManger;
public DetectForm(CameraManger _cameraManger)
{
InitializeComponent();
cameraManger = _cameraManger;
btn_scan_Click(null, null);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void btn_scan_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
cameraManger.ScannCamera();
var _camera = cameraManger.GetCameras;
foreach (var item in _camera)
{
dataGridView1.Rows.Add(item.sourceIp,item.targetIp,item.cameraType,item.cameraStatus);
}
}
private void btn_connect_Click(object sender, EventArgs e)
{
DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
if (selectedRow.Cells[0].Value != null)
{
string sourceIp = selectedRow.Cells[0].Value.ToString();
string targetIp = selectedRow.Cells[1].Value.ToString();
var ret = cameraManger.ConnectCamera(sourceIp,targetIp);
if (ret == true)
{
selectedRow.Cells[3].Value = "已连接";
}
else
{
selectedRow.Cells[3].Value = "未连接";
}
}
}
private void btn_disConnect_Click(object sender, EventArgs e)
{
DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
if (selectedRow.Cells[0].Value != null)
{
string sourceIp = selectedRow.Cells[0].Value.ToString();
string targetIp = selectedRow.Cells[1].Value.ToString();
var ret = cameraManger.DisConnectCamera();
if (!ret)
{
selectedRow.Cells[3].Value = "已连接";
}
else
{
selectedRow.Cells[3].Value = "未连接";
}
}
}
}
}

View File

@ -1,6 +1,6 @@
namespace PanSight3DForm
{
partial class DetectForm
partial class DetectForm1
{
/// <summary>
/// Required designer variable.
@ -35,7 +35,6 @@
this.cameraType = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.cameraStatus = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.panel1 = new System.Windows.Forms.Panel();
this.btn_disConnect = new System.Windows.Forms.Button();
this.btn_connect = new System.Windows.Forms.Button();
this.btn_scan = new System.Windows.Forms.Button();
this.tableLayoutPanel1.SuspendLayout();
@ -52,11 +51,12 @@
this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 1);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 189F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 236F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(613, 287);
this.tableLayoutPanel1.Size = new System.Drawing.Size(817, 359);
this.tableLayoutPanel1.TabIndex = 0;
//
// dataGridView1
@ -69,66 +69,67 @@
this.cameraStatus});
this.tableLayoutPanel1.SetColumnSpan(this.dataGridView1, 2);
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.Location = new System.Drawing.Point(3, 3);
this.dataGridView1.Location = new System.Drawing.Point(4, 4);
this.dataGridView1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.dataGridView1.MultiSelect = false;
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersWidth = 51;
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(607, 183);
this.dataGridView1.Size = new System.Drawing.Size(809, 228);
this.dataGridView1.TabIndex = 0;
//
// localIP
//
this.localIP.HeaderText = "本地IP";
this.localIP.MinimumWidth = 6;
this.localIP.Name = "localIP";
this.localIP.ReadOnly = true;
this.localIP.Width = 125;
//
// cameraIp
//
this.cameraIp.HeaderText = "相机IP";
this.cameraIp.MinimumWidth = 6;
this.cameraIp.Name = "cameraIp";
this.cameraIp.ReadOnly = true;
this.cameraIp.Width = 125;
//
// cameraType
//
this.cameraType.HeaderText = "相机型号";
this.cameraType.MinimumWidth = 6;
this.cameraType.Name = "cameraType";
this.cameraType.ReadOnly = true;
this.cameraType.Width = 125;
//
// cameraStatus
//
this.cameraStatus.HeaderText = "连接状态";
this.cameraStatus.MinimumWidth = 6;
this.cameraStatus.Name = "cameraStatus";
this.cameraStatus.ReadOnly = true;
this.cameraStatus.Width = 125;
//
// panel1
//
this.tableLayoutPanel1.SetColumnSpan(this.panel1, 2);
this.panel1.Controls.Add(this.btn_disConnect);
this.panel1.Controls.Add(this.btn_connect);
this.panel1.Controls.Add(this.btn_scan);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(3, 192);
this.panel1.Location = new System.Drawing.Point(4, 240);
this.panel1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(607, 92);
this.panel1.Size = new System.Drawing.Size(809, 115);
this.panel1.TabIndex = 1;
//
// btn_disConnect
//
this.btn_disConnect.Location = new System.Drawing.Point(515, 27);
this.btn_disConnect.Name = "btn_disConnect";
this.btn_disConnect.Size = new System.Drawing.Size(83, 38);
this.btn_disConnect.TabIndex = 2;
this.btn_disConnect.Text = "断开连接";
this.btn_disConnect.UseVisualStyleBackColor = true;
this.btn_disConnect.Click += new System.EventHandler(this.btn_disConnect_Click);
//
// btn_connect
//
this.btn_connect.Location = new System.Drawing.Point(413, 27);
this.btn_connect.Location = new System.Drawing.Point(670, 32);
this.btn_connect.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btn_connect.Name = "btn_connect";
this.btn_connect.Size = new System.Drawing.Size(83, 38);
this.btn_connect.Size = new System.Drawing.Size(111, 48);
this.btn_connect.TabIndex = 1;
this.btn_connect.Text = "连接";
this.btn_connect.UseVisualStyleBackColor = true;
@ -136,9 +137,10 @@
//
// btn_scan
//
this.btn_scan.Location = new System.Drawing.Point(9, 26);
this.btn_scan.Location = new System.Drawing.Point(12, 32);
this.btn_scan.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btn_scan.Name = "btn_scan";
this.btn_scan.Size = new System.Drawing.Size(83, 38);
this.btn_scan.Size = new System.Drawing.Size(111, 48);
this.btn_scan.TabIndex = 0;
this.btn_scan.Text = "搜索";
this.btn_scan.UseVisualStyleBackColor = true;
@ -146,13 +148,14 @@
//
// DetectForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(613, 287);
this.ClientSize = new System.Drawing.Size(817, 359);
this.Controls.Add(this.tableLayoutPanel1);
this.Margin = new System.Windows.Forms.Padding(2);
this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.Name = "DetectForm";
this.Text = "DetectForm";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.DetectForm_FormClosing);
this.tableLayoutPanel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.panel1.ResumeLayout(false);
@ -171,6 +174,5 @@
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button btn_connect;
private System.Windows.Forms.Button btn_scan;
private System.Windows.Forms.Button btn_disConnect;
}
}

View File

@ -0,0 +1,110 @@
using Camera;
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;
namespace PanSight3DForm
{
public partial class DetectForm1 : Form
{
public struct cameras
{
public string sourceIp;
public string targetIp;
public string cameraType;
public string cameraStatus;
}
public string sourceIp;
public string targetIp;
private List<cameras> camerasList = new List<cameras>();
public DetectForm1()
{
InitializeComponent();
btn_scan_Click(null, null);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void btn_scan_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
camerasList.Clear();
IntPtr ptr = CameraWrapper.SgDetectNetCameras();
string sip = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ptr);
Common.DETECTINFO[] devices = Common.analysisIpInfos(sip);
if (devices != null && devices.Length > 0)
{
GetDeviceIPPair(devices).ToList().ForEach((item) =>
{
cameras _camera = new cameras();
_camera.sourceIp = item.Item1;
_camera.targetIp = item.Item2.Split('(')[0];
_camera.cameraType = item.Item2.Split('(')[1].Split(')')[0].Split('=')[0];
_camera.cameraStatus = "未连接";
foreach (var v in camerasList)
{
if (v.cameraType == _camera.cameraType)
{
return;
}
}
camerasList.Add(_camera);
});
}
foreach (var item in camerasList)
{
dataGridView1.Rows.Add(item.sourceIp,item.targetIp,item.cameraType,item.cameraStatus);
}
}
IEnumerable<Tuple<string, string>> GetDeviceIPPair(Common.DETECTINFO[] infos)
{
foreach (var i in infos)
{
foreach (var j in i._ips)
{
foreach (var k in j._cameraIps)
{
yield return Tuple.Create(j._hostIp, k);
}
}
}
}
private void btn_connect_Click(object sender, EventArgs e)
{
DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
if (selectedRow.Cells[0].Value != null)
{
sourceIp = selectedRow.Cells[0].Value.ToString();
targetIp = selectedRow.Cells[1].Value.ToString();
this.DialogResult = DialogResult.OK;
}
}
private void DetectForm_FormClosing(object sender, FormClosingEventArgs e)
{
}
}
}

View File

@ -129,4 +129,16 @@
<metadata name="cameraStatus.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="localIP.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="cameraIp.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="cameraType.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="cameraStatus.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root>

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,12 @@
using PanSight3DForm.Model;
using Camera;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -14,13 +17,31 @@ namespace PanSight3DForm
public partial class Form1 : Form
{
#region
CameraWrapper.SGEXPORT_ROI m_roi = new CameraWrapper.SGEXPORT_ROI();
CameraWrapper.SGEXPORT_MODE m_mode = new CameraWrapper.SGEXPORT_MODE();
CameraWrapper.SGEXPORT_CAMCONFIG m_cfg = new CameraWrapper.SGEXPORT_CAMCONFIG();
CameraWrapper.SGEXPORT_PRODUCTINFO m_prod = new CameraWrapper.SGEXPORT_PRODUCTINFO();
private CameraSyn m_camera = new CameraSyn();
private bool isConnect = false;
CameraManger cameraManger;
public string sourceIp;
public string targetIp;
float m_fTemperature = 0;
enum TEST_TYPE { TEST_TYPE_GRAB_PNTS_CLOUD, TEST_TYPE_LOOP_GRAB, TEST_TYPE_PROFILES };
TEST_TYPE m_testType = TEST_TYPE.TEST_TYPE_GRAB_PNTS_CLOUD;
#endregion
public Form1()
{
InitializeComponent();
cameraManger = new CameraManger();
m_camera.DepthEvent += onDepth;
m_camera.prepare();
}
private void M_camera_ImageEvent(IntPtr arg1, CameraWrapper.SG_IMGDATA_PARAM arg2, IntPtr arg3)
{
throw new NotImplementedException();
}
private void btn_cameraSetting_Click(object sender, EventArgs e)
@ -37,34 +58,77 @@ namespace PanSight3DForm
{
if (!isConnect)
{
DetectForm detectForm = new DetectForm(cameraManger);
DetectForm1 detectForm = new DetectForm1();
detectForm.TopMost = true;
detectForm.ShowDialog();
if (cameraManger.isConnect)
if (detectForm.ShowDialog() == DialogResult.OK)
{
sourceIp = detectForm.sourceIp;
targetIp = detectForm.targetIp;
if (m_camera.Connect2Camera(sourceIp, targetIp))
{
if (m_camera.GetRoiAndMedia(ref m_roi) == false)
{
MessageBox.Show("获取ROI失败");
return;
}
if (m_camera.GetCamMode(ref m_mode) == false)
{
MessageBox.Show("获取模式失败");
return;
}
cb_transmode.SelectedIndex = m_mode._ucTransMode;
if (m_mode._ucDataType == (byte)CameraWrapper.DATATYPE.DATATYPE_PROFILE_GRAY)
{
checkBoxGray.Checked = true;
}
else
{
checkBoxGray.Checked = false;
}
numericUpDownLineNum.Value = m_mode._uiGrabNumber;
if (m_camera.GetCamConfig(ref m_cfg) == false)
{
MessageBox.Show("获取配置失败");
return;
}
if (m_camera.GetCamProductInfos(ref m_prod) == false)
{
MessageBox.Show("获取产品信息失败");
return;
}
btn_connect.Text = "断开";
isConnect = true;
btn_connect.BackColor = Color.Red;
lb_status.Text = "状态: 已连接";
lb_status.BackColor = Color.Green;
textBoxLocalIp.Text = cameraManger.localIp;
textBoxRemoteIp.Text = cameraManger.targetIp;
var mode = cameraManger.GetMode();
nud_expo.Value = mode._uiExpo;
cbb_gain.Text = mode._usGain.ToString();
nup_jiguang.Value = mode._ucLaserPower;
nud_frameData.Value = mode._usFrame;
nud_x.Value = (decimal)mode._fXScaling;
nud_y.Value = (decimal)mode._fYScaling;
var camMode = cameraManger.GetCamMode();
cb_transmode.SelectedIndex = camMode._ucTransMode;
textBoxLocalIp.Text = sourceIp;
textBoxRemoteIp.Text = targetIp;
nud_expo.Value = m_cfg._uiExpo;
cbb_gain.Text = m_cfg._usGain.ToString();
nup_jiguang.Value = m_cfg._ucLaserPower;
nud_frameData.Value = m_cfg._usFrame;
nud_x.Value = (decimal)m_cfg._fXScaling;
nud_y.Value = (decimal)m_cfg._fYScaling;
}
}
}
else
{
cameraManger.DisConnectCamera();
m_camera.CloseCamera();
btn_connect.Text = "连接";
isConnect = false;
btn_connect.BackColor = Color.OliveDrab;
@ -73,6 +137,465 @@ namespace PanSight3DForm
textBoxLocalIp.Text = "";
textBoxRemoteIp.Text = "";
}
}
unsafe void saveDepthData(float* pBuf, uint bufSize)
{
string dataDir = "c:\\data";
if (Directory.Exists(dataDir) == false)
{
Directory.CreateDirectory(dataDir);
}
string timestamp = DateTime.Now.ToString("yyMMddHHmmss_fff");
string filename = string.Format("{0}\\Depth_t{1}.txt", dataDir, timestamp);
StreamWriter sw = new StreamWriter(filename);
string content = "";
for (int i = 0; i < bufSize;)
{
content = string.Format("{0},{1},{2}", pBuf[i], pBuf[i + 1], pBuf[i + 2]);
i += 3;
sw.WriteLine(content);
}
sw.Close();
Common.InvokeFunc(this, () => {
labelTips.Text = "点云文件抓取成功," + filename;
});
}
int m_iGrabProfileNum = 0;
int m_iGrabCallbackCount = 0;
unsafe void onDepth(IntPtr pBuf, IntPtr pGrayBuf, CameraWrapper.SG_DEPTHDATA_PARAM param, IntPtr pOwner)
{
if (pBuf == null)
{
return;
}
Common.InvokeFunc(this, () => {
labelTips.Text = "数据全部采集完成,开始转换显示";
});
if (param._bProfile == 1)
{
return;
}
int w = param._iPointNumPerLine;
int h = param._iCapturedProfileLineNum;
if (pGrayBuf != null && param._iCapturedProfileLineNum > 0)
{
// 显示灰度图
showGrayImg((byte*)pGrayBuf, w, h);
}
if (pBuf != null && param._iCapturedProfileLineNum > 0)
{
// 显示高度图
//showDepthImg(pBuf, param);
showDepthImg((float*)pBuf, w, h);
}
// 重点声明一下onDepth 函数是SDK中的线程回调上来的这里面不要有耗时等待的操作。
m_iGrabProfileNum = param._iCapturedProfileLineNum;
m_iGrabCallbackCount++;
if (m_testType != TEST_TYPE.TEST_TYPE_PROFILES)
{
// 抓取点云
Common.InvokeFunc(this, () => {
// 不要在SDK的回调线程中直接调用sdk接口,此处切换到ui线程中调用
m_fTemperature = 0;
m_camera.GetTemperature(ref m_fTemperature);
labelMsg.Text = DateTime.Now.ToString("HH:mm:ss.fff") + " 抓取线数:" + m_iGrabProfileNum + " 温度:" + m_fTemperature;
});
}
//if (h > 0)
//{
// saveDepthData((float*)pBuf, (uint)(w * h * 3));
//}
//Common.InvokeFunc(this, () => {
// BufToHImage((float*)pBuf, (byte*)pGrayBuf, w, h);
//});
}
unsafe void showGrayImg( byte* pGrayBuf, int width, int height)
{
int iPntsCount = width * height;
float[] y_pBuf = new float[iPntsCount];
float[] x_pBuf = new float[iPntsCount];
float[] z_pBuf = new float[iPntsCount];
byte[] grayBufff = null;
if (pGrayBuf != null)
{
grayBufff = new byte[iPntsCount];
}
for (int i = 0; i < iPntsCount; i++)
{
if (pGrayBuf != null)
{
grayBufff[i] = pGrayBuf[i];
}
}
//HTuple obj3DHandle;//3D句柄
//HTuple hTupleX = new HTuple(x_pBuf);
//HTuple hTupleY = new HTuple(y_pBuf);
//HTuple hTupleZ = new HTuple(z_pBuf);
//HTuple hTupleGray = new HTuple();
//if (pGrayBuf != null)
//{
// hTupleGray = new HTuple(iPntsCount);
//}
//HOperatorSet.GenObjectModel3dFromPoints(hTupleX, hTupleY, hTupleZ, out obj3DHandle);
//void* p0, p1, p2, p3;
IntPtr ypRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height); //分配非托管内存空间
IntPtr xpRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height);
IntPtr zpRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height);
IntPtr gpRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height);
fixed (void* py = y_pBuf)
{
ypRawData = new IntPtr(py);
}
fixed (void* px = x_pBuf)
{
xpRawData = new IntPtr(px);
}
fixed (void* pz = z_pBuf)
{
zpRawData = new IntPtr(pz);
}
if (pGrayBuf != null)
{
fixed (void* pg = grayBufff)
{
gpRawData = new IntPtr(pg);
}
}
HObject img = null, x = null, y = null, z = null;
img?.Dispose();
HOperatorSet.GenImage3(out img, "real", width, height, xpRawData, ypRawData, zpRawData);
x?.Dispose(); y?.Dispose(); z?.Dispose();
HOperatorSet.Decompose3(img, out x, out y, out z);
HTuple w, h;
HOperatorSet.GetImageSize(z, out w, out h);
HOperatorSet.SetPart(this.hWindowControlHeight.HalconWindow, 0, 0, h - 1, w - 1);
HOperatorSet.DispObj(z, this.hWindowControlHeight.HalconWindow);
if (pGrayBuf != null)
{
HObject gray = null;
gray?.Dispose();
HOperatorSet.GenImage1(out gray, "byte", width, height, gpRawData);
HOperatorSet.GetImageSize(gray, out w, out h);
HOperatorSet.SetPart(this.hWindowControlGray.HalconWindow, 0, 0, h - 1, w - 1);
HOperatorSet.DispObj(gray, this.hWindowControlGray.HalconWindow);
}
}
unsafe void showDepthImg(float* pBuf, int width, int height)
{
int iPntsCount = width * height;
float[] y_pBuf = new float[iPntsCount];
float[] x_pBuf = new float[iPntsCount];
float[] z_pBuf = new float[iPntsCount];
//float/*[*/] a_pBuf = new float[iPntsCount * 3];
for (int i = 0; i < iPntsCount; i++)
{
y_pBuf[i] = pBuf[i * 3];
x_pBuf[i] = pBuf[i * 3 + 1];
z_pBuf[i] = pBuf[i * 3 + 2];
//a_pBuf[i*3] = pBuf[i * 3];
//a_pBuf[i*3+1] = pBuf[i * 3 + 1];
//a_pBuf[i*3+2] = pBuf[i * 3 + 2];
}
//// 定义txt文件的路径
//string filePath = "all_buf.txt";
//// 使用StreamWriter写入文件
//using (StreamWriter writer = new StreamWriter(filePath))
//{
// // 遍历数组并写入每个元素到txt文件
// foreach (float value in a_pBuf)
// {
// writer.WriteLine(value);
// }
//}
//// 定义txt文件的路径
//filePath = "x1.txt";
//// 使用StreamWriter写入文件
//using (StreamWriter writer = new StreamWriter(filePath))
//{
// // 遍历数组并写入每个元素到txt文件
// foreach (float value in x_pBuf)
// {
// writer.WriteLine(value);
// }
//}
//// 定义txt文件的路径
//filePath = "y1.txt";
//// 使用StreamWriter写入文件
//using (StreamWriter writer = new StreamWriter(filePath))
//{
// // 遍历数组并写入每个元素到txt文件
// foreach (float value in y_pBuf)
// {
// writer.WriteLine(value);
// }
//}
//HTuple obj3DHandle;//3D句柄
//HTuple hTupleX = new HTuple(x_pBuf);
//HTuple hTupleY = new HTuple(y_pBuf);
//HTuple hTupleZ = new HTuple(z_pBuf);
//HTuple hTupleGray = new HTuple();
//if (pGrayBuf != null)
//{
// hTupleGray = new HTuple(iPntsCount);
//}
//HOperatorSet.GenObjectModel3dFromPoints(hTupleX, hTupleY, hTupleZ, out obj3DHandle);
//void* p0, p1, p2, p3;
IntPtr ypRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height); //分配非托管内存空间
IntPtr xpRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height);
IntPtr zpRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height);
IntPtr gpRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height);
fixed (void* py = y_pBuf)
{
ypRawData = new IntPtr(py);
}
fixed (void* px = x_pBuf)
{
xpRawData = new IntPtr(px);
}
fixed (void* pz = z_pBuf)
{
zpRawData = new IntPtr(pz);
}
HObject img = null, x = null, y = null, z = null;
img?.Dispose();
HOperatorSet.GenImage3(out img, "real", width, height, xpRawData, ypRawData, zpRawData);
x?.Dispose(); y?.Dispose(); z?.Dispose();
HOperatorSet.Decompose3(img, out x, out y, out z);
HTuple w, h;
HOperatorSet.GetImageSize(z, out w, out h);
HOperatorSet.SetPart(this.hWindowControlHeight.HalconWindow, 0, 0, h - 1, w - 1);
HOperatorSet.DispObj(z, this.hWindowControlHeight.HalconWindow);
}
public byte Rescale(float value, float max, float min)
{
if (value < min)
{
return 0;
}
else if (value > max)
{
return 255;
}
else if (Math.Abs(max - min) < 0.00001)
{
// 同一个平面
return 0;
}
return (byte)(((value - min) / (max - min)) * 255);
}
unsafe void BufToHImage(float* pBuf, byte* pGrayBuf, int width, int height)
{
int iPntsCount = width * height;
float[] y_pBuf = new float[iPntsCount];
float[] x_pBuf = new float[iPntsCount];
float[] z_pBuf = new float[iPntsCount];
byte[] grayBufff = null;
if (pGrayBuf != null)
{
grayBufff = new byte[iPntsCount];
}
for (int i = 0; i < iPntsCount; i++)
{
y_pBuf[i] = pBuf[i * 3];
x_pBuf[i] = pBuf[i * 3 + 1];
z_pBuf[i] = pBuf[i * 3 + 2];
if (pGrayBuf != null)
{
grayBufff[i] = pGrayBuf[i];
}
}
//HTuple obj3DHandle;//3D句柄
//HTuple hTupleX = new HTuple(x_pBuf);
//HTuple hTupleY = new HTuple(y_pBuf);
//HTuple hTupleZ = new HTuple(z_pBuf);
//HTuple hTupleGray = new HTuple();
//if (pGrayBuf != null)
//{
// hTupleGray = new HTuple(iPntsCount);
//}
//HOperatorSet.GenObjectModel3dFromPoints(hTupleX, hTupleY, hTupleZ, out obj3DHandle);
//void* p0, p1, p2, p3;
IntPtr ypRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height); //分配非托管内存空间
IntPtr xpRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height);
IntPtr zpRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height);
IntPtr gpRawData = IntPtr.Zero;// Marshal.AllocHGlobal(width * height);
fixed (void* py = y_pBuf)
{
ypRawData = new IntPtr(py);
}
fixed (void* px = x_pBuf)
{
xpRawData = new IntPtr(px);
}
fixed (void* pz = z_pBuf)
{
zpRawData = new IntPtr(pz);
}
if (pGrayBuf != null)
{
fixed (void* pg = grayBufff)
{
gpRawData = new IntPtr(pg);
}
}
HObject img = null, x = null, y = null, z = null;
img?.Dispose();
HOperatorSet.GenImage3(out img, "real", width, height, xpRawData, ypRawData, zpRawData);
x?.Dispose(); y?.Dispose(); z?.Dispose();
HOperatorSet.Decompose3(img, out x, out y, out z);
HTuple w, h;
HOperatorSet.GetImageSize(z, out w, out h);
HOperatorSet.SetPart(this.hWindowControlHeight.HalconWindow, 0, 0, h - 1, w - 1);
HOperatorSet.DispObj(z, this.hWindowControlHeight.HalconWindow);
if (pGrayBuf != null)
{
HObject gray = null;
gray?.Dispose();
HOperatorSet.GenImage1(out gray, "byte", width, height, gpRawData);
HOperatorSet.GetImageSize(gray, out w, out h);
HOperatorSet.SetPart(this.hWindowControlGray.HalconWindow, 0, 0, h - 1, w - 1);
HOperatorSet.DispObj(gray, this.hWindowControlGray.HalconWindow);
}
}
private void button1_Click(object sender, EventArgs e)
{
CameraWrapper.SGEXPORT_MODE mode = m_mode;
mode._ucCaptureMode = (byte)CameraWrapper.CAPMODE.CAPMODE_AUTO;
mode._uiGrabNumber = (uint)numericUpDownLineNum.Value;
if (checkBoxGray.Checked)
{
mode._ucDataType = (byte)CameraWrapper.DATATYPE.DATATYPE_PROFILE_GRAY;
}
else
{
mode._ucDataType = (byte)CameraWrapper.DATATYPE.DATATYPE_PROFILE;
}
mode._ucUserOperatorMode = (byte)CameraWrapper.DATAMODE.DATAMODE_GRAB;
mode._ucTransMode = (byte)cb_transmode.SelectedIndex; // 对应到 CameraWrapper.TRANSMODE
m_camera.SetCamMode(mode);
}
private void button2_Click(object sender, EventArgs e)
{
CameraWrapper.SGEXPORT_CAMCONFIG cfg = m_cfg;
m_cfg._usFrame = (ushort)nud_frameData.Value;
m_cfg._fYScaling = (ushort)nud_y.Value;
m_cfg._fXScaling = (ushort)nud_x.Value;
var ret = m_camera.SetCamConfig(m_cfg);
}
private void button3_Click(object sender, EventArgs e)
{
m_camera.GetCamConfig(ref m_cfg);
m_cfg._uiExpo = (uint)nud_expo.Value;
m_cfg._usGain = ushort.Parse(cbb_gain.Text);
m_cfg._ucLaserPower = (byte)nup_jiguang.Value;
m_camera.SetCamConfig(m_cfg);
}
private void buttonCapture_Click(object sender, EventArgs e)
{
labelTips.Text = "开始抓取...";
bool ret = m_camera.SendGrabSignalToCameraEx(false);
if (ret == false)
{
labelMsg.Text = "抓取失败";
}
}
private void buttonStart_Click(object sender, EventArgs e)
{
if (m_mode._ucUserOperatorMode != (byte)CameraWrapper.DATAMODE.DATAMODE_GRAB)
{
MessageBox.Show("相机模式不对(非抓取模式)");
return;
}
bool bStart = m_camera.StartCapture();
if (bStart)
{
labelMsg.Text = "准备抓取成功,请使用定时抓取或单步触发功能";
}
else
{
labelMsg.Text = "准备抓起失败";
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
bool bStop = m_camera.StopCapture();
if (bStop)
{
labelMsg.Text = "结束抓取成功";
}
else
{
labelMsg.Text = "结束抓起失败";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
CameraSyn.LibRelease();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,171 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Camera;
using Sunny.UI.Win32;
using static AntdUI.Math3D;
namespace PanSight3DForm.Model
{
public class CameraManger
{
#region
CameraWrapper.SGEXPORT_ROI m_roi = new CameraWrapper.SGEXPORT_ROI();
CameraWrapper.SGEXPORT_MODE m_mode = new CameraWrapper.SGEXPORT_MODE();
CameraWrapper.SGEXPORT_CAMCONFIG m_cfg = new CameraWrapper.SGEXPORT_CAMCONFIG();
CameraWrapper.SGEXPORT_PRODUCTINFO m_prod = new CameraWrapper.SGEXPORT_PRODUCTINFO();
public struct cameras
{
public string sourceIp;
public string targetIp;
public string cameraType;
public string cameraStatus;
}
private CameraSyn m_camera = new CameraSyn();
private List<cameras> camerasList =new List<cameras>();
public bool isConnect = false;
public string localIp;
public string targetIp;
#endregion
public CameraManger()
{
m_camera = new CameraSyn();
m_camera.ImageEvent += M_camera_ImageEvent;
}
private void M_camera_ImageEvent(IntPtr arg1, CameraWrapper.SG_IMGDATA_PARAM arg2, IntPtr arg3)
{
}
public void ScannCamera()
{
IntPtr ptr = CameraWrapper.SgDetectNetCameras();
string sip = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ptr);
Common.DETECTINFO[] devices = Common.analysisIpInfos(sip);
if (devices != null && devices.Length > 0)
{
GetDeviceIPPair(devices).ToList().ForEach((item) =>
{
cameras _camera = new cameras();
_camera.sourceIp = item.Item1;
_camera.targetIp = item.Item2.Split('(')[0];
_camera.cameraType = item.Item2.Split('(')[1].Split(')')[0].Split('=')[0];
_camera.cameraStatus = "未连接";
foreach (var v in camerasList)
{
if (v.cameraType == _camera.cameraType)
{
return;
}
}
camerasList.Add(_camera);
});
}
}
public bool ConnectCamera(string sourceip,string targetIp)
{
m_camera.LocalIp = sourceip;
m_camera.CameraIp = targetIp;
var ret = m_camera.Connect();
string v = "已连接";
if (!ret) v = "未连接";
else
{
localIp = m_camera.LocalIp;
this.targetIp = m_camera.CameraIp;
}
for (int i = 0; i < camerasList.Count; i++)
{
if (m_camera.CameraIp == camerasList[i].targetIp)
{
cameras _cameras = new cameras();
_cameras.sourceIp = camerasList[i].sourceIp;
_cameras.targetIp = camerasList[i].targetIp;
_cameras.cameraStatus = v;
camerasList[i] = _cameras;
}
}
isConnect = ret;
return ret;
}
public bool DisConnectCamera()
{
m_camera.CloseCamera();
for (int i = 0; i < camerasList.Count; i++)
{
if (m_camera.CameraIp == camerasList[i].targetIp)
{
cameras _cameras = new cameras();
_cameras.sourceIp = camerasList[i].sourceIp;
_cameras.targetIp = camerasList[i].targetIp;
_cameras.cameraStatus = "未连接";
camerasList[i] = _cameras;
}
}
isConnect = false;
return true;
}
IEnumerable<Tuple<string, string>> GetDeviceIPPair(Common.DETECTINFO[] infos)
{
foreach (var i in infos)
{
foreach (var j in i._ips)
{
foreach (var k in j._cameraIps)
{
yield return Tuple.Create(j._hostIp, k);
}
}
}
}
public bool SetExpor(uint expo)
{
m_cfg._uiExpo = expo;
if (m_camera.SetCamConfig(m_cfg))
{
return true;
}
return false;
}
public bool SetGain(ushort gain)
{
m_cfg._usGain = gain;
if (m_camera.SetCamConfig(m_cfg))
{
return true;
}
return false;
}
public List<cameras> GetCameras
{
get { return camerasList; }
}
public CameraWrapper.SGEXPORT_CAMCONFIG GetMode()
{
m_camera.GetCamConfig(ref m_cfg);
return m_cfg;
}
public CameraWrapper.SGEXPORT_MODE GetCamMode()
{
m_camera.GetCamMode(ref m_mode);
return m_mode;
}
}
}

View File

@ -12,9 +12,24 @@
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
@ -23,6 +38,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -32,11 +48,36 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<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>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="CameraEx">
<HintPath>..\Dlls\CameraEx.dll</HintPath>
</Reference>
<Reference Include="halcondotnet">
<HintPath>..\Dlls\halcondotnet.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@ -52,11 +93,11 @@
<ItemGroup>
<Compile Include="Common.cs" />
<Compile Include="CursorCtrol.cs" />
<Compile Include="DetectForm.cs">
<Compile Include="DetectForm1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="DetectForm.Designer.cs">
<DependentUpon>DetectForm.cs</DependentUpon>
<Compile Include="DetectForm1.Designer.cs">
<DependentUpon>DetectForm1.cs</DependentUpon>
</Compile>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
@ -64,11 +105,10 @@
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Model\CameraManger.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="DetectForm.resx">
<DependentUpon>DetectForm.cs</DependentUpon>
<EmbeddedResource Include="DetectForm1.resx">
<DependentUpon>DetectForm1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
@ -103,5 +143,17 @@
<Version>3.7.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.8">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.8 %28x86 和 x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -8,13 +8,19 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CB2EBB4D-EDF7-4346-A8A0-928FFA87B38E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB2EBB4D-EDF7-4346-A8A0-928FFA87B38E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB2EBB4D-EDF7-4346-A8A0-928FFA87B38E}.Debug|x64.ActiveCfg = Debug|x64
{CB2EBB4D-EDF7-4346-A8A0-928FFA87B38E}.Debug|x64.Build.0 = Debug|x64
{CB2EBB4D-EDF7-4346-A8A0-928FFA87B38E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB2EBB4D-EDF7-4346-A8A0-928FFA87B38E}.Release|Any CPU.Build.0 = Release|Any CPU
{CB2EBB4D-EDF7-4346-A8A0-928FFA87B38E}.Release|x64.ActiveCfg = Release|x64
{CB2EBB4D-EDF7-4346-A8A0-928FFA87B38E}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE