Commit b63041d8 LN

增加相机获取图片代码

1 个父辈 c00b7345
...@@ -17,7 +17,11 @@ namespace AOIProject ...@@ -17,7 +17,11 @@ namespace AOIProject
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmAoiSetting()); string path = "E:\\VSSource\\TSAV-智能组装机\\Line-TSAV-AOI\\TSA-V\\bin\\Debug\\config\\AOIConfig\\";
string filename = "E:\\VSSource\\TSAV-智能组装机\\Line-TSAV-AOI\\TSA-V\\bin\\Debug\\config\\AOIConfig\\aaaaa.data";
AccAOI.camera.CameraManager.LoadCamera();
Application.Run(new FrmAoiSetting(filename, null,path));
AccAOI.camera.CameraManager.CloseCamera();
} }
} }
} }
...@@ -38,6 +38,9 @@ ...@@ -38,6 +38,9 @@
<Reference Include="Asa.Theme"> <Reference Include="Asa.Theme">
<HintPath>..\dll\Asa.Theme.dll</HintPath> <HintPath>..\dll\Asa.Theme.dll</HintPath>
</Reference> </Reference>
<Reference Include="MvCameraControl.Net">
<HintPath>..\dll\MvCameraControl.Net.dll</HintPath>
</Reference>
<Reference Include="OpenCvSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL"> <Reference Include="OpenCvSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.dll</HintPath> <HintPath>..\packages\OpenCvSharp3-AnyCPU.4.0.0.20181129\lib\net40\OpenCvSharp.dll</HintPath>
...@@ -67,6 +70,7 @@ ...@@ -67,6 +70,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AOIFormUtil.cs" /> <Compile Include="AOIFormUtil.cs" />
<Compile Include="camera\CameraManager.cs" />
<Compile Include="ControlUtil.cs" /> <Compile Include="ControlUtil.cs" />
<Compile Include="control\AioTempMatchControl.cs"> <Compile Include="control\AioTempMatchControl.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
...@@ -104,6 +108,7 @@ ...@@ -104,6 +108,7 @@
<Compile Include="FrmAoiSetting.Designer.cs"> <Compile Include="FrmAoiSetting.Designer.cs">
<DependentUpon>FrmAoiSetting.cs</DependentUpon> <DependentUpon>FrmAoiSetting.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="camera\HIKCamera.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="control\AioTempMatchControl.resx"> <EmbeddedResource Include="control\AioTempMatchControl.resx">
......
...@@ -105,7 +105,7 @@ namespace AccAOI ...@@ -105,7 +105,7 @@ namespace AccAOI
// //
this.btnOpenImage.ImageSize = new System.Drawing.Size(0, 0); this.btnOpenImage.ImageSize = new System.Drawing.Size(0, 0);
this.btnOpenImage.Inside = false; this.btnOpenImage.Inside = false;
this.btnOpenImage.Location = new System.Drawing.Point(1032, 40); this.btnOpenImage.Location = new System.Drawing.Point(1091, 40);
this.btnOpenImage.Name = "btnOpenImage"; this.btnOpenImage.Name = "btnOpenImage";
this.btnOpenImage.Size = new System.Drawing.Size(109, 30); this.btnOpenImage.Size = new System.Drawing.Size(109, 30);
this.btnOpenImage.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30))))); this.btnOpenImage.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
...@@ -129,14 +129,14 @@ namespace AccAOI ...@@ -129,14 +129,14 @@ namespace AccAOI
this.cmbCameraList.Inside = false; this.cmbCameraList.Inside = false;
this.cmbCameraList.Location = new System.Drawing.Point(730, 40); this.cmbCameraList.Location = new System.Drawing.Point(730, 40);
this.cmbCameraList.Name = "cmbCameraList"; this.cmbCameraList.Name = "cmbCameraList";
this.cmbCameraList.Size = new System.Drawing.Size(158, 30); this.cmbCameraList.Size = new System.Drawing.Size(217, 30);
this.cmbCameraList.TabIndex = 12; this.cmbCameraList.TabIndex = 12;
// //
// btnGetCameraImg // btnGetCameraImg
// //
this.btnGetCameraImg.ImageSize = new System.Drawing.Size(0, 0); this.btnGetCameraImg.ImageSize = new System.Drawing.Size(0, 0);
this.btnGetCameraImg.Inside = false; this.btnGetCameraImg.Inside = false;
this.btnGetCameraImg.Location = new System.Drawing.Point(908, 40); this.btnGetCameraImg.Location = new System.Drawing.Point(967, 40);
this.btnGetCameraImg.Name = "btnGetCameraImg"; this.btnGetCameraImg.Name = "btnGetCameraImg";
this.btnGetCameraImg.Size = new System.Drawing.Size(109, 30); this.btnGetCameraImg.Size = new System.Drawing.Size(109, 30);
this.btnGetCameraImg.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30))))); this.btnGetCameraImg.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
...@@ -206,7 +206,7 @@ namespace AccAOI ...@@ -206,7 +206,7 @@ namespace AccAOI
// //
this.btnImageChange.ImageSize = new System.Drawing.Size(0, 0); this.btnImageChange.ImageSize = new System.Drawing.Size(0, 0);
this.btnImageChange.Inside = false; this.btnImageChange.Inside = false;
this.btnImageChange.Location = new System.Drawing.Point(779, 76); this.btnImageChange.Location = new System.Drawing.Point(838, 76);
this.btnImageChange.Name = "btnImageChange"; this.btnImageChange.Name = "btnImageChange";
this.btnImageChange.Size = new System.Drawing.Size(109, 30); this.btnImageChange.Size = new System.Drawing.Size(109, 30);
this.btnImageChange.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30))))); this.btnImageChange.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
...@@ -218,7 +218,7 @@ namespace AccAOI ...@@ -218,7 +218,7 @@ namespace AccAOI
// //
this.flatButton1.ImageSize = new System.Drawing.Size(0, 0); this.flatButton1.ImageSize = new System.Drawing.Size(0, 0);
this.flatButton1.Inside = false; this.flatButton1.Inside = false;
this.flatButton1.Location = new System.Drawing.Point(908, 76); this.flatButton1.Location = new System.Drawing.Point(967, 76);
this.flatButton1.Name = "flatButton1"; this.flatButton1.Name = "flatButton1";
this.flatButton1.Size = new System.Drawing.Size(109, 30); this.flatButton1.Size = new System.Drawing.Size(109, 30);
this.flatButton1.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30))))); this.flatButton1.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
...@@ -230,7 +230,7 @@ namespace AccAOI ...@@ -230,7 +230,7 @@ namespace AccAOI
// //
this.flatButton2.ImageSize = new System.Drawing.Size(0, 0); this.flatButton2.ImageSize = new System.Drawing.Size(0, 0);
this.flatButton2.Inside = false; this.flatButton2.Inside = false;
this.flatButton2.Location = new System.Drawing.Point(1032, 76); this.flatButton2.Location = new System.Drawing.Point(1091, 76);
this.flatButton2.Name = "flatButton2"; this.flatButton2.Name = "flatButton2";
this.flatButton2.Size = new System.Drawing.Size(109, 30); this.flatButton2.Size = new System.Drawing.Size(109, 30);
this.flatButton2.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30))))); this.flatButton2.StateColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
......
using AOI; using AccAOI.camera;
using AOI;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -20,6 +22,7 @@ namespace AccAOI ...@@ -20,6 +22,7 @@ namespace AccAOI
private AoiProject Project = null; private AoiProject Project = null;
private bool CanSel = true; private bool CanSel = true;
private string DefaultPath = ""; private string DefaultPath = "";
private string currProjectFileName = "";
public FrmAoiSetting() public FrmAoiSetting()
{ {
InitializeComponent(); InitializeComponent();
...@@ -29,9 +32,9 @@ namespace AccAOI ...@@ -29,9 +32,9 @@ namespace AccAOI
{ {
InitializeComponent(); InitializeComponent();
this.WindowState = FormWindowState.Maximized; this.WindowState = FormWindowState.Maximized;
if (!programPath.Equals("") && (image != null)) if (!programPath.Equals(""))
{ {
BaseImg = image; // BaseImg = image;
// Project = new AoiProject(image); // Project = new AoiProject(image);
string result = ""; string result = "";
Project= AoiProject.Load(programPath,out result); Project= AoiProject.Load(programPath,out result);
...@@ -39,10 +42,20 @@ namespace AccAOI ...@@ -39,10 +42,20 @@ namespace AccAOI
{ {
MessageBox.Show("加载项目" + programPath + "失败:\r\n" + result); MessageBox.Show("加载项目" + programPath + "失败:\r\n" + result);
} }
else
{
currProjectFileName= programPath;
}
}
if (image != null)
{
if (Project != null)
{
Project.standardImage = image;
}
} }
DefaultPath = defaultPath; DefaultPath = defaultPath;
ShowPorject();
} }
private void FrmAoiSetting_Load(object sender, EventArgs e) private void FrmAoiSetting_Load(object sender, EventArgs e)
{ {
...@@ -58,6 +71,16 @@ namespace AccAOI ...@@ -58,6 +71,16 @@ namespace AccAOI
BaseImg = (Image)Image.FromFile(defaultImg).Clone(); BaseImg = (Image)Image.FromFile(defaultImg).Clone();
imageBox1.Image = BaseImg; imageBox1.Image = BaseImg;
} }
foreach (string str in CameraManager.hikNameList)
{
cmbCameraList.ItemAdd(str);
}
if (Project != null)
{
ShowPorject();
}
} }
private void btnOpenImage_Click(object sender, EventArgs e) private void btnOpenImage_Click(object sender, EventArgs e)
...@@ -96,6 +119,21 @@ namespace AccAOI ...@@ -96,6 +119,21 @@ namespace AccAOI
MessageBox.Show("清先选择相机"); MessageBox.Show("清先选择相机");
return; return;
} }
Bitmap img = CameraManager.GetCamerImage(camera);
if (img != null)
{
//读取图片内容
BaseImg = (Image)img.Clone();
if (Project != null)
{
Project.standardImage = BaseImg;
}
imageBox1.Image = BaseImg;
if (imageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
} }
private void btnOpenPro_Click(object sender, EventArgs e) private void btnOpenPro_Click(object sender, EventArgs e)
...@@ -121,6 +159,10 @@ namespace AccAOI ...@@ -121,6 +159,10 @@ namespace AccAOI
{ {
MessageBox.Show("加载项目" + fileName + "失败:\r\n" + msg); MessageBox.Show("加载项目" + fileName + "失败:\r\n" + msg);
} }
else
{
currProjectFileName = fileName;
}
ShowPorject(); ShowPorject();
} }
...@@ -135,7 +177,13 @@ namespace AccAOI ...@@ -135,7 +177,13 @@ namespace AccAOI
System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog(); System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
saveFileDialog.Title = "保存项目"; saveFileDialog.Title = "保存项目";
saveFileDialog.Filter = "(*.data)|*.data|(*.*)|*.*"; saveFileDialog.Filter = "(*.data)|*.data|(*.*)|*.*";
if (!String.IsNullOrEmpty(DefaultPath))
if (!String.IsNullOrEmpty(currProjectFileName))
{
saveFileDialog.InitialDirectory = Path.GetFullPath(currProjectFileName);
saveFileDialog.FileName = Path.GetFileName(currProjectFileName);
}
else if (!String.IsNullOrEmpty(DefaultPath))
{ {
saveFileDialog.InitialDirectory = DefaultPath; saveFileDialog.InitialDirectory = DefaultPath;
} }
...@@ -245,6 +293,7 @@ namespace AccAOI ...@@ -245,6 +293,7 @@ namespace AccAOI
{ {
CloseCurrAoi(); CloseCurrAoi();
aoiList.ItemClear(); aoiList.ItemClear();
BaseImg = Project.standardImage;
imageBox1.Image = Project.standardImage; imageBox1.Image = Project.standardImage;
imageBox1.SelectionRegion = new GraphicsPath(); imageBox1.SelectionRegion = new GraphicsPath();
...@@ -418,7 +467,23 @@ namespace AccAOI ...@@ -418,7 +467,23 @@ namespace AccAOI
private void flatButton1_Click(object sender, EventArgs e) private void flatButton1_Click(object sender, EventArgs e)
{ {
string camera = cmbCameraList.Text;
if (camera.Equals(""))
{
MessageBox.Show("清先选择相机");
return;
}
Bitmap img = CameraManager.GetCamerImage(camera);
if (img != null)
{
//读取图片内容
TestImage = (Image)img.Clone();
testImageBox1.Image = TestImage;
if (testImageBox1.Visible.Equals(false))
{
btnImageChange_Click(null, null);
}
}
} }
} }
} }
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
namespace AccAOI.camera
{
public class CameraManager
{
public static List<string> hikNameList = new List<string>();
private static char spiltChar = '#';
public static string ErrorMsg = "";
public static void LoadCamera(bool isReLoad = false)
{
if (isReLoad)
{
HIKCamera.Instance.Load();
}
string[] names = HIKCamera.Instance.CameraName;
hikNameList.AddRange(names);
}
public static void CloseCamera()
{
HIKCamera.Instance.Close();
}
[HandleProcessCorruptedStateExceptions]
public static Bitmap GetCamerImage(string cameraName)
{
Bitmap bitm = null;
try
{
if (hikNameList.Contains(cameraName))
{
bool result = HIKCamera.Instance.IsOpen;
if (!result)
{
result = HIKCamera.Instance.Open(cameraName);
}
if (result)
{
HIKCamera.Instance.GrabOne();
if (HIKCamera.Instance.Image != null)
{
bitm = (Bitmap)HIKCamera.Instance.Image;
//HIKCamera.Instance.Close();
}
}
else
{
ErrorMsg = ("相机【" + cameraName + "】打开失败:" + HIKCamera.Instance.ErrInfo);
}
}
else
{
ErrorMsg = ("未找到相机【" + cameraName + "】无法获取图片");
}
}
catch (Exception ex)
{
ErrorMsg = ("从相机【" + cameraName + "】获取图片出错:" + ex.ToString());
}
return bitm;
}
}
}
此文件类型无法预览
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!