Commit e90230ff 几米阳光

增加海康相机。学习二维码时可以从相机获取图片

1 个父辈 1cce736b
using Basler.Pylon;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeLibrary
{
public class BaslerCamera
{
public static BaslerCamera Instance= new BaslerCamera();
/// <summary>
/// 当前相机
/// </summary>
private Camera cameraCur = null;
/// <summary>
/// 所有相机列表
/// </summary>
private List<ICameraInfo> cameraAll;
/// <summary>
/// 所有相机的名称
/// </summary>
private List<string> cameraName;
/// <summary>
/// 获取连续图像
/// </summary>
public delegate void GrabImageEvent();
/// <summary>
/// 获取连续图像事件,需要跨线程操作
/// </summary>
public event GrabImageEvent GrabImage;
private BaslerCamera()
{
Load();
}
/// <summary>
/// 错误信息
/// </summary>
public string ErrInfo { set; get; }
/// <summary>
/// 相机总数
/// </summary>
public int Count
{
get { return cameraAll == null ? 0 : cameraAll.Count; }
}
/// <summary>
/// 相机名称,ModelName,SerialNumber
/// </summary>
public string[] CameraName
{
get { return cameraName.ToArray(); }
}
/// <summary>
/// 当前相机是否打开
/// </summary>
public bool IsOpen
{
get
{
if (cameraCur == null)
return false;
else
return cameraCur.IsOpen;
}
}
/// <summary>
/// 相机图像宽度
/// </summary>
public int Width { set; get; }
/// <summary>
/// 相机图像高度
/// </summary>
public int Height { set; get; }
/// <summary>
/// 相机32位缓存
/// </summary>
public byte[] Buffer { get; private set; }
/// <summary>
/// 相机32位图像
/// </summary>
public Bitmap Image { get; private set; }
/// <summary>
/// 加载相机
/// </summary>
public void Load()
{
cameraAll = CameraFinder.Enumerate();
cameraName = new List<string>();
foreach (ICameraInfo info in cameraAll)
cameraName.Add(info[CameraInfoKey.ModelName].ToString() + " (" + info[CameraInfoKey.SerialNumber].ToString() + ")");
}
/// <summary>
/// 打开指定相机
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool Open(string name)
{
int n = cameraName.FindIndex(s => s == name);
if (n == -1)
return false;
else
return Open(n);
}
/// <summary>
/// 打开指定相机
/// </summary>
/// <param name="idx">索引</param>
/// <returns></returns>
public bool Open(int idx)
{
if (idx < 0 || idx >= cameraAll.Count) return false;
if (cameraCur != null) Close();
try
{
cameraCur = new Camera(cameraAll[idx]);
//cameraCur.ConnectionLost += OnConnectionLost;
//cameraCur.CameraOpened += OnCameraOpened;
//cameraCur.CameraClosed += OnCameraClosed;
//cameraCur.StreamGrabber.GrabStarted += OnGrabStarted;
cameraCur.StreamGrabber.ImageGrabbed += OnImageGrabbed;
//cameraCur.StreamGrabber.GrabStopped += OnGrabStopped;
cameraCur.Open();
Width = Convert.ToInt32(cameraCur.Parameters[PLCamera.Width].GetValue());
Height = Convert.ToInt32(cameraCur.Parameters[PLCamera.Height].GetValue());
cameraCur.Parameters[PLCamera.UserSetSelector].SetValue(PLCamera.UserSetSelector.UserSet1); //加载用户设置1
bool bln = cameraCur.Parameters[PLCamera.UserSetLoad].TryExecute(); //执行设置
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
/// <summary>
/// 关闭当前相机
/// </summary>
public void Close()
{
if (cameraCur != null)
{
cameraCur.Close();
cameraCur.Dispose();
cameraCur = null;
}
}
/// <summary>
/// 停止抓取数据
/// </summary>
public void Stop()
{
if (cameraCur != null)
cameraCur.StreamGrabber.Stop();
}
/// <summary>
/// 抓取一张图像
/// </summary>
public void GrabOne()
{
cameraCur.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.SingleFrame);
//cameraCur.StreamGrabber.Start();
//IGrabResult grabResult = cameraCur.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);
IGrabResult grabResult = cameraCur.StreamGrabber.GrabOne(5000);
if (!grabResult.IsValid) return;
Image = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
BitmapData bmpData = Image.LockBits(new Rectangle(0, 0, grabResult.Width, grabResult.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
IntPtr ptrBmp = bmpData.Scan0;
int picSize = bmpData.Stride * grabResult.Height;
PixelDataConverter conv = new PixelDataConverter();
conv.OutputPixelFormat = PixelType.BGRA8packed;
conv.Convert(ptrBmp, picSize, grabResult);
Buffer = new byte[picSize];
System.Runtime.InteropServices.Marshal.Copy(ptrBmp, Buffer, 0, picSize);
Image.UnlockBits(bmpData);
//cameraCur.StreamGrabber.Stop();
}
/// <summary>
/// 抓取连续图像,触发GrabImage事件
/// </summary>
public void GrabContinuous()
{
cameraCur.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
cameraCur.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
}
private void OnImageGrabbed(object sender, ImageGrabbedEventArgs e)
{
try
{
IGrabResult grabResult = e.GrabResult;
if (!grabResult.IsValid) return;
Image = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
BitmapData bmpData = Image.LockBits(new Rectangle(0, 0, grabResult.Width, grabResult.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
IntPtr ptrBmp = bmpData.Scan0;
int picSize = bmpData.Stride * grabResult.Height;
PixelDataConverter conv = new PixelDataConverter();
conv.OutputPixelFormat = PixelType.BGRA8packed;
conv.Convert(ptrBmp, picSize, grabResult);
Buffer = new byte[picSize];
System.Runtime.InteropServices.Marshal.Copy(ptrBmp, Buffer, 0, picSize);
Image.UnlockBits(bmpData);
GrabImage?.Invoke();
}
catch (Exception ex)
{
ErrInfo = ex.Message;
}
finally
{
e.DisposeGrabResultIfClone();
}
}
}
}
using Basler.Pylon;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeLibrary
{
public class PylonCamera
{
private Camera cam = null;
private List<ICameraInfo> allCameras;
private PixelDataConverter conv = new PixelDataConverter();
private Stopwatch stopWatch = new Stopwatch();
private System.Windows.Forms.PictureBox pic;
public PylonCamera()
{
allCameras = CameraFinder.Enumerate();
}
~PylonCamera()
{
Close();
}
public int Index { set; get; }
public string ErrInfo { set; get; }
public int CameraCount
{
get { return allCameras.Count; }
}
public bool IsOpen
{
get { return cam != null; }
}
public string[] GetName()
{
string[] s = new string[allCameras.Count];
for (int i = 0; i < s.Length; i++)
s[i] = allCameras[i][CameraInfoKey.FullName].ToString();
return s;
}
public void Close()
{
if (cam != null)
{
cam.Close();
cam.Dispose();
cam = null;
}
}
public bool Open()
{
return Open(Index);
}
public bool Open(int idx)
{
if (idx < 0 || idx >= allCameras.Count) return false;
if (cam != null) Close();
try
{
Index = idx;
cam = new Camera(allCameras[idx]);
cam.ConnectionLost += OnConnectionLost;
cam.CameraOpened += OnCameraOpened;
cam.CameraClosed += OnCameraClosed;
cam.StreamGrabber.GrabStarted += OnGrabStarted;
cam.StreamGrabber.ImageGrabbed += OnImageGrabbed;
cam.StreamGrabber.GrabStopped += OnGrabStopped;
cam.Open();
//加载用户设置1
cam.Parameters[PLCamera.UserSetSelector].SetValue(PLCamera.UserSetSelector.UserSet1);
//执行设置
bool bln = cam.Parameters[PLCamera.UserSetLoad].TryExecute();
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
public void Stop()
{
if (cam != null)
{
cam.StreamGrabber.Stop();
}
}
public Bitmap syncShot()
{
cam.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.SingleFrame);
cam.StreamGrabber.Start();
IGrabResult grabResult = cam.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);
if (!grabResult.IsValid)
{
return null;
}
Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
cam.StreamGrabber.Stop();
return bitmap;
}
public void asynShot( )
{
try
{
cam.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.SingleFrame);
cam.StreamGrabber.Start(1, GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
}
catch (Exception ex)
{
ErrInfo = ex.Message;
}
}
public void ContinuousShot( )
{
try
{
cam.Parameters[PLCamera.AcquisitionMode].SetValue(PLCamera.AcquisitionMode.Continuous);
cam.StreamGrabber.Start(GrabStrategy.OneByOne, GrabLoop.ProvidedByStreamGrabber);
}
catch (Exception ex)
{
ErrInfo = ex.Message;
}
}
private void OnConnectionLost(Object sender, EventArgs e)
{
Close();
}
private void OnCameraOpened(Object sender, EventArgs e)
{
}
private void OnCameraClosed(Object sender, EventArgs e)
{
}
private void OnGrabStarted(Object sender, EventArgs e)
{
stopWatch.Reset();
}
private void OnGrabStopped(Object sender, GrabStopEventArgs e)
{
stopWatch.Reset();
}
private void OnImageGrabbed(Object sender, ImageGrabbedEventArgs e)
{
IGrabResult grabResult = e.GrabResult;
if (!grabResult.IsValid)
{ return; }
if (!stopWatch.IsRunning || stopWatch.ElapsedMilliseconds > 33)
{
stopWatch.Restart();
}
Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
}
}
}
...@@ -42,6 +42,9 @@ ...@@ -42,6 +42,9 @@
<Reference Include="log4net"> <Reference Include="log4net">
<HintPath>..\dll\log4net.dll</HintPath> <HintPath>..\dll\log4net.dll</HintPath>
</Reference> </Reference>
<Reference Include="MvCameraControl.Net">
<HintPath>..\dll\MvCameraControl.Net.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
...@@ -52,12 +55,10 @@ ...@@ -52,12 +55,10 @@
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="zxing">
<HintPath>..\dll\zxing.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Camera.cs" /> <Compile Include="HIKCamera.cs" />
<Compile Include="BaslerCamera.cs" />
<Compile Include="FrmCodeLearn.cs"> <Compile Include="FrmCodeLearn.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>
...@@ -84,5 +85,8 @@ ...@@ -84,5 +85,8 @@
<DependentUpon>FrmCodeDecode.cs</DependentUpon> <DependentUpon>FrmCodeDecode.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Include="记录.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>
\ No newline at end of file \ No newline at end of file
...@@ -18,9 +18,11 @@ using System.IO; ...@@ -18,9 +18,11 @@ using System.IO;
namespace CodeLibrary namespace CodeLibrary
{ {
public partial class FrmCodeDecode : Form public partial class FrmCodeDecode : Form
{ {
private PylonCamera camera = new PylonCamera();
private Stopwatch stopwatch = new Stopwatch(); private Stopwatch stopwatch = new Stopwatch();
private List<string> baslerNameList = new List<string>();
private List<string> hikNameList = new List<string>();
public FrmCodeDecode() public FrmCodeDecode()
{ {
InitializeComponent(); InitializeComponent();
...@@ -30,16 +32,8 @@ namespace CodeLibrary ...@@ -30,16 +32,8 @@ namespace CodeLibrary
{ {
cmbCount.SelectedIndex = 0; cmbCount.SelectedIndex = 0;
string[] camerName = camera.GetName(); LoadCamera();
cmbCamera.Items.Clear();
foreach (string str in camerName)
{
cmbCamera.Items.Add(str);
}
if (camerName.Length > 0)
{
cmbCamera.SelectedIndex = 0;
}
cmbCodeType.DataSource = HDCodeLearnHelper.codeTypeList; cmbCodeType.DataSource = HDCodeLearnHelper.codeTypeList;
if (HDCodeLearnHelper.codeTypeList.Count > 0) if (HDCodeLearnHelper.codeTypeList.Count > 0)
{ {
...@@ -51,6 +45,27 @@ namespace CodeLibrary ...@@ -51,6 +45,27 @@ namespace CodeLibrary
cmbCodeType.SelectedIndex = 0; cmbCodeType.SelectedIndex = 0;
} }
} }
private void LoadCamera()
{
string[] camerName = BaslerCamera.Instance.CameraName;
baslerNameList.AddRange(camerName);
cmbCamera.Items.Clear();
foreach (string str in camerName)
{
cmbCamera.Items.Add(str);
}
camerName = HIKCamera.Instance.CameraName;
hikNameList.AddRange(camerName);
foreach (string str in camerName)
{
cmbCamera.Items.Add(str);
}
if (camerName.Length > 0)
{
cmbCamera.SelectedIndex = 0;
}
}
private void btnSelImage_Click(object sender, EventArgs e) private void btnSelImage_Click(object sender, EventArgs e)
{ {
System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog(); System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
...@@ -74,7 +89,7 @@ namespace CodeLibrary ...@@ -74,7 +89,7 @@ namespace CodeLibrary
pictureBox1.Image = img; pictureBox1.Image = img;
} }
private void button2_Click(object sender, EventArgs e) private void btnErZhi_Click(object sender, EventArgs e)
{ {
if (pictureBox1.Image == null || txtPath.Text.Equals("")) if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{ {
...@@ -85,7 +100,7 @@ namespace CodeLibrary ...@@ -85,7 +100,7 @@ namespace CodeLibrary
Bitmap newMap = ImageHelper.ConvertTo1Bpp1(map); Bitmap newMap = ImageHelper.ConvertTo1Bpp1(map);
pictureBox1.Image = newMap; pictureBox1.Image = newMap;
} }
private void button3_Click(object sender, EventArgs e) private void btnGray_Click(object sender, EventArgs e)
{ {
if (pictureBox1.Image == null || txtPath.Text.Equals("")) if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{ {
...@@ -114,7 +129,7 @@ namespace CodeLibrary ...@@ -114,7 +129,7 @@ namespace CodeLibrary
} }
} }
private void btnHalconP_Click(object sender, EventArgs e) private void btnbarCode_Click(object sender, EventArgs e)
{ {
if (pictureBox1.Image == null || txtPath.Text.Equals("")) if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{ {
...@@ -141,13 +156,13 @@ namespace CodeLibrary ...@@ -141,13 +156,13 @@ namespace CodeLibrary
HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow); HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
} }
private void button5_Click(object sender, EventArgs e) private void btnLearn_Click(object sender, EventArgs e)
{ {
FrmCodeLearn frm = new FrmCodeLearn(); FrmCodeLearn frm = new FrmCodeLearn();
frm.ShowDialog(); frm.ShowDialog();
} }
private void button6_Click(object sender, EventArgs e) private void btnDCode_Click(object sender, EventArgs e)
{ {
if (pictureBox1.Image == null) if (pictureBox1.Image == null)
{ {
...@@ -178,26 +193,48 @@ namespace CodeLibrary ...@@ -178,26 +193,48 @@ namespace CodeLibrary
txtResult.Text += "\r\n扫码结束耗时:" + stopwatch.Elapsed.ToString(); txtResult.Text += "\r\n扫码结束耗时:" + stopwatch.Elapsed.ToString();
} }
private void button7_Click(object sender, EventArgs e) private void btnClearLog_Click(object sender, EventArgs e)
{ {
HDLogUtil.ClearLog();
txtResult.Text = ""; txtResult.Text = "";
} }
private Bitmap GetCameraBitmap()
private void button8_Click(object sender, EventArgs e)
{ {
int index = cmbCamera.SelectedIndex; int index = cmbCamera.SelectedIndex;
string camerName = cmbCamera.Text;
if (index < 0) if (index < 0)
{ {
MessageBox.Show("请先选择相机"); MessageBox.Show("请先选择相机");
return; return null;
} }
camera.Open(index); if (baslerNameList.Contains(camerName))
Bitmap bit = camera.syncShot(); {
if (bit != null) BaslerCamera.Instance.Open(camerName);
BaslerCamera.Instance.GrabOne();
Bitmap bitmap = BaslerCamera.Instance.Image;
BaslerCamera.Instance.Close();
return bitmap;
}
else
{
HIKCamera.Instance.Open(camerName);
HIKCamera.Instance.GrabOne();
Bitmap bitmap = HIKCamera.Instance.Image;
HIKCamera.Instance.Close();
return bitmap;
}
}
private void btnCameraImage_Click(object sender, EventArgs e)
{
Bitmap bitmap = GetCameraBitmap();
if (bitmap != null)
{ {
pictureBox1.Image = bit; HDLogUtil.info("从相机【" + cmbCamera.Text + "】获取到一张图片");
pictureBox1.Image = bitmap;
HObject hoImage = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
HDCodeLearnHelper.DefaultImage = hoImage;
} }
camera.Close();
} }
private void btnExit_Click(object sender, EventArgs e) private void btnExit_Click(object sender, EventArgs e)
...@@ -205,7 +242,7 @@ namespace CodeLibrary ...@@ -205,7 +242,7 @@ namespace CodeLibrary
this.Close(); this.Close();
} }
private void button1_Click(object sender, EventArgs e) private void btnLigth_Click(object sender, EventArgs e)
{ {
if (pictureBox1.Image == null || txtPath.Text.Equals("")) if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{ {
...@@ -217,7 +254,7 @@ namespace CodeLibrary ...@@ -217,7 +254,7 @@ namespace CodeLibrary
pictureBox1.Image = newMap; pictureBox1.Image = newMap;
} }
private void button4_Click(object sender, EventArgs e) private void btnAn_Click(object sender, EventArgs e)
{ {
if (pictureBox1.Image == null || txtPath.Text.Equals("")) if (pictureBox1.Image == null || txtPath.Text.Equals(""))
{ {
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
this.hWindowControl1 = new HalconDotNet.HWindowControl(); this.hWindowControl1 = new HalconDotNet.HWindowControl();
this.btnExit = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label();
this.cmbCameraList = new System.Windows.Forms.ComboBox(); this.cmbHalconCamera = new System.Windows.Forms.ComboBox();
this.cmbCodeType = new System.Windows.Forms.ComboBox(); this.cmbCodeType = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label();
...@@ -52,6 +52,8 @@ ...@@ -52,6 +52,8 @@
this.chbUseCamera = new System.Windows.Forms.CheckBox(); this.chbUseCamera = new System.Windows.Forms.CheckBox();
this.btnDelOld = new System.Windows.Forms.Button(); this.btnDelOld = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label();
this.chbHalcon = new System.Windows.Forms.CheckBox();
this.cmbCamera = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
...@@ -102,7 +104,7 @@ ...@@ -102,7 +104,7 @@
// //
this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnExit.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnExit.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnExit.Location = new System.Drawing.Point(898, 13); this.btnExit.Location = new System.Drawing.Point(896, 51);
this.btnExit.Name = "btnExit"; this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(110, 35); this.btnExit.Size = new System.Drawing.Size(110, 35);
this.btnExit.TabIndex = 6; this.btnExit.TabIndex = 6;
...@@ -114,30 +116,31 @@ ...@@ -114,30 +116,31 @@
// //
this.label1.AutoSize = true; this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(45, 20); this.label1.Location = new System.Drawing.Point(17, 20);
this.label1.Name = "label1"; this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(51, 20); this.label1.Size = new System.Drawing.Size(51, 20);
this.label1.TabIndex = 7; this.label1.TabIndex = 7;
this.label1.Text = "相机:"; this.label1.Text = "相机:";
// //
// cmbCameraList // cmbHalconCamera
// //
this.cmbCameraList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbHalconCamera.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbCameraList.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.cmbHalconCamera.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.cmbCameraList.FormattingEnabled = true; this.cmbHalconCamera.FormattingEnabled = true;
this.cmbCameraList.Location = new System.Drawing.Point(90, 16); this.cmbHalconCamera.Location = new System.Drawing.Point(63, 16);
this.cmbCameraList.Name = "cmbCameraList"; this.cmbHalconCamera.Name = "cmbHalconCamera";
this.cmbCameraList.Size = new System.Drawing.Size(200, 28); this.cmbHalconCamera.Size = new System.Drawing.Size(230, 28);
this.cmbCameraList.TabIndex = 8; this.cmbHalconCamera.TabIndex = 8;
this.cmbHalconCamera.Visible = false;
// //
// cmbCodeType // cmbCodeType
// //
this.cmbCodeType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbCodeType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbCodeType.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.cmbCodeType.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.cmbCodeType.FormattingEnabled = true; this.cmbCodeType.FormattingEnabled = true;
this.cmbCodeType.Location = new System.Drawing.Point(90, 54); this.cmbCodeType.Location = new System.Drawing.Point(63, 53);
this.cmbCodeType.Name = "cmbCodeType"; this.cmbCodeType.Name = "cmbCodeType";
this.cmbCodeType.Size = new System.Drawing.Size(200, 28); this.cmbCodeType.Size = new System.Drawing.Size(230, 28);
this.cmbCodeType.TabIndex = 10; this.cmbCodeType.TabIndex = 10;
this.cmbCodeType.SelectedIndexChanged += new System.EventHandler(this.cmbCodeType_SelectedIndexChanged); this.cmbCodeType.SelectedIndexChanged += new System.EventHandler(this.cmbCodeType_SelectedIndexChanged);
// //
...@@ -147,9 +150,9 @@ ...@@ -147,9 +150,9 @@
this.label2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(17, 58); this.label2.Location = new System.Drawing.Point(17, 58);
this.label2.Name = "label2"; this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(79, 20); this.label2.Size = new System.Drawing.Size(51, 20);
this.label2.TabIndex = 9; this.label2.TabIndex = 9;
this.label2.Text = "条码类型:"; this.label2.Text = "类型:";
// //
// label3 // label3
// //
...@@ -225,7 +228,7 @@ ...@@ -225,7 +228,7 @@
// //
this.btnClearLog.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnClearLog.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnClearLog.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnClearLog.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnClearLog.Location = new System.Drawing.Point(898, 51); this.btnClearLog.Location = new System.Drawing.Point(896, 13);
this.btnClearLog.Name = "btnClearLog"; this.btnClearLog.Name = "btnClearLog";
this.btnClearLog.Size = new System.Drawing.Size(110, 35); this.btnClearLog.Size = new System.Drawing.Size(110, 35);
this.btnClearLog.TabIndex = 16; this.btnClearLog.TabIndex = 16;
...@@ -265,8 +268,7 @@ ...@@ -265,8 +268,7 @@
this.txtPath.Location = new System.Drawing.Point(606, 101); this.txtPath.Location = new System.Drawing.Point(606, 101);
this.txtPath.Name = "txtPath"; this.txtPath.Name = "txtPath";
this.txtPath.Size = new System.Drawing.Size(404, 21); this.txtPath.Size = new System.Drawing.Size(404, 21);
this.txtPath.TabIndex = 18; this.txtPath.TabIndex = 18;
this.txtPath.TextChanged += new System.EventHandler(this.txtPath_TextChanged);
// //
// pictureBox1 // pictureBox1
// //
...@@ -286,9 +288,9 @@ ...@@ -286,9 +288,9 @@
this.chbUseCamera.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.chbUseCamera.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.chbUseCamera.Location = new System.Drawing.Point(457, 18); this.chbUseCamera.Location = new System.Drawing.Point(457, 18);
this.chbUseCamera.Name = "chbUseCamera"; this.chbUseCamera.Name = "chbUseCamera";
this.chbUseCamera.Size = new System.Drawing.Size(154, 24); this.chbUseCamera.Size = new System.Drawing.Size(140, 24);
this.chbUseCamera.TabIndex = 21; this.chbUseCamera.TabIndex = 21;
this.chbUseCamera.Text = "摄像机实时获取图片"; this.chbUseCamera.Text = "相机获取实时图片";
this.chbUseCamera.UseVisualStyleBackColor = true; this.chbUseCamera.UseVisualStyleBackColor = true;
this.chbUseCamera.CheckedChanged += new System.EventHandler(this.chbUseCamera_CheckedChanged); this.chbUseCamera.CheckedChanged += new System.EventHandler(this.chbUseCamera_CheckedChanged);
// //
...@@ -314,10 +316,34 @@ ...@@ -314,10 +316,34 @@
this.label4.TabIndex = 23; this.label4.TabIndex = 23;
this.label4.Text = "图片路径"; this.label4.Text = "图片路径";
// //
// chbHalcon
//
this.chbHalcon.AutoSize = true;
this.chbHalcon.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.chbHalcon.Location = new System.Drawing.Point(303, 18);
this.chbHalcon.Name = "chbHalcon";
this.chbHalcon.Size = new System.Drawing.Size(132, 24);
this.chbHalcon.TabIndex = 24;
this.chbHalcon.Text = "Halcon获取图片";
this.chbHalcon.UseVisualStyleBackColor = true;
this.chbHalcon.CheckedChanged += new System.EventHandler(this.chbHalcon_CheckedChanged);
//
// cmbCamera
//
this.cmbCamera.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbCamera.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.cmbCamera.FormattingEnabled = true;
this.cmbCamera.Location = new System.Drawing.Point(63, 17);
this.cmbCamera.Name = "cmbCamera";
this.cmbCamera.Size = new System.Drawing.Size(230, 28);
this.cmbCamera.TabIndex = 25;
//
// FrmCodeLearn // FrmCodeLearn
// //
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(1018, 729); this.ClientSize = new System.Drawing.Size(1018, 729);
this.Controls.Add(this.cmbCamera);
this.Controls.Add(this.chbHalcon);
this.Controls.Add(this.label4); this.Controls.Add(this.label4);
this.Controls.Add(this.btnDelOld); this.Controls.Add(this.btnDelOld);
this.Controls.Add(this.chbUseCamera); this.Controls.Add(this.chbUseCamera);
...@@ -333,7 +359,7 @@ ...@@ -333,7 +359,7 @@
this.Controls.Add(this.label3); this.Controls.Add(this.label3);
this.Controls.Add(this.cmbCodeType); this.Controls.Add(this.cmbCodeType);
this.Controls.Add(this.label2); this.Controls.Add(this.label2);
this.Controls.Add(this.cmbCameraList); this.Controls.Add(this.cmbHalconCamera);
this.Controls.Add(this.label1); this.Controls.Add(this.label1);
this.Controls.Add(this.btnExit); this.Controls.Add(this.btnExit);
this.Controls.Add(this.hWindowControl1); this.Controls.Add(this.hWindowControl1);
...@@ -358,7 +384,7 @@ ...@@ -358,7 +384,7 @@
private HalconDotNet.HWindowControl hWindowControl1; private HalconDotNet.HWindowControl hWindowControl1;
private System.Windows.Forms.Button btnExit; private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox cmbCameraList; private System.Windows.Forms.ComboBox cmbHalconCamera;
private System.Windows.Forms.ComboBox cmbCodeType; private System.Windows.Forms.ComboBox cmbCodeType;
private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label3;
...@@ -374,5 +400,7 @@ ...@@ -374,5 +400,7 @@
private System.Windows.Forms.CheckBox chbUseCamera; private System.Windows.Forms.CheckBox chbUseCamera;
private System.Windows.Forms.Button btnDelOld; private System.Windows.Forms.Button btnDelOld;
private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label4;
private System.Windows.Forms.CheckBox chbHalcon;
private System.Windows.Forms.ComboBox cmbCamera;
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -15,31 +15,78 @@ namespace CodeLibrary ...@@ -15,31 +15,78 @@ namespace CodeLibrary
{ {
public partial class FrmCodeLearn : Form public partial class FrmCodeLearn : Form
{ {
private List<string> baslerNameList = new List<string>();
private List<string> hikNameList = new List<string>();
public FrmCodeLearn() public FrmCodeLearn()
{ {
InitializeComponent(); InitializeComponent();
} }
private Bitmap GetCameraBitmap()
{
Bitmap bitmap = null;
int index = cmbCamera.SelectedIndex;
string camerName = cmbCamera.Text;
if (index < 0)
{
MessageBox.Show("请先选择相机");
return null;
}
if (baslerNameList.Contains(camerName))
{
BaslerCamera.Instance.Open(camerName);
BaslerCamera.Instance.GrabOne();
bitmap = BaslerCamera.Instance.Image;
BaslerCamera.Instance.Close();
}
else
{
HIKCamera.Instance.Open(camerName);
HIKCamera.Instance.GrabOne();
bitmap = HIKCamera.Instance.Image;
HIKCamera.Instance.Close();
}
return bitmap;
}
private void btnOpen_Click(object sender, EventArgs e) private void btnOpen_Click(object sender, EventArgs e)
{ {
string filePath = txtParamPath.Text; string filePath = txtParamPath.Text;
string cameraName = cmbCameraList.Text; string cameraName = "";
string codeType = this.cmbCodeType.Text; string codeType = this.cmbCodeType.Text;
int count = cmbCount.SelectedIndex + 1; int count = cmbCount.SelectedIndex + 1;
if (chbUseCamera.Checked) if (chbUseCamera.Checked)
{ {
if (cameraName.Equals("")) if (chbHalcon.Checked)
{ {
MessageBox.Show("请选择摄像机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); cameraName = cmbHalconCamera.Text;
return; if (cameraName.Equals(""))
{
MessageBox.Show("请先选择相机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
else
{
Bitmap bitmap = GetCameraBitmap();
if (bitmap != null)
{
HDLogUtil.info("从相机【" + cmbCamera.Text + "】获取到一张图片");
pictureBox1.Image = bitmap;
HObject hoImage = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
HDCodeLearnHelper.DefaultImage = hoImage;
}
else
{
return;
}
} }
} }
else else
{ {
cameraName = "";
if (pictureBox1.Image == null) if (pictureBox1.Image == null)
{ {
MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("请先选择图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
...@@ -51,9 +98,29 @@ namespace CodeLibrary ...@@ -51,9 +98,29 @@ namespace CodeLibrary
{ {
HDCodeLearnHelper.StartLearn(this.hWindowControl1.HalconWindow, cameraName, codeType, filePath, count,5000); HDCodeLearnHelper.StartLearn(this.hWindowControl1.HalconWindow, cameraName, codeType, filePath, count,5000);
}); });
FormStatus(true); FormStatus(true);
} }
private void LoadCamera()
{
string[] camerName = BaslerCamera.Instance.CameraName;
baslerNameList.AddRange(camerName);
cmbCamera.Items.Clear();
foreach (string str in camerName)
{
cmbCamera.Items.Add(str);
}
camerName = HIKCamera.Instance.CameraName;
hikNameList.AddRange(camerName);
foreach (string str in camerName)
{
cmbCamera.Items.Add(str);
}
if (camerName.Length > 0)
{
cmbCamera.SelectedIndex = 0;
}
}
private void FormStatus(bool open) private void FormStatus(bool open)
{ {
btnOpen.Enabled = !open; btnOpen.Enabled = !open;
...@@ -75,11 +142,12 @@ namespace CodeLibrary ...@@ -75,11 +142,12 @@ namespace CodeLibrary
{ {
HDCodeLearnHelper.LoadConfig("", ""); HDCodeLearnHelper.LoadConfig("", "");
} }
LoadCamera();
FormStatus(false); FormStatus(false);
cmbCameraList.DataSource = HDCodeLearnHelper.cameraNameList; cmbHalconCamera.DataSource = HDCodeLearnHelper.cameraNameList;
if (HDCodeLearnHelper.cameraNameList.Count > 0) if (HDCodeLearnHelper.cameraNameList.Count > 0)
{ {
cmbCameraList.SelectedIndex = 0; cmbHalconCamera.SelectedIndex = 0;
} }
cmbCodeType.DataSource = HDCodeLearnHelper.codeTypeList; cmbCodeType.DataSource = HDCodeLearnHelper.codeTypeList;
if (HDCodeLearnHelper.codeTypeList.Count > 0) if (HDCodeLearnHelper.codeTypeList.Count > 0)
...@@ -90,6 +158,7 @@ namespace CodeLibrary ...@@ -90,6 +158,7 @@ namespace CodeLibrary
string filePath =HDCodeHelper.GetCodeParamFilePath(cmbCodeType.Text); string filePath =HDCodeHelper.GetCodeParamFilePath(cmbCodeType.Text);
txtParamPath.Text = filePath; txtParamPath.Text = filePath;
cmbCount.SelectedIndex = 0; cmbCount.SelectedIndex = 0;
chbUseCamera.Checked = true;
timer1.Start(); timer1.Start();
} }
...@@ -167,22 +236,17 @@ namespace CodeLibrary ...@@ -167,22 +236,17 @@ namespace CodeLibrary
Image img = (Image)Image.FromFile(filename).Clone(); Image img = (Image)Image.FromFile(filename).Clone();
pictureBox1.Image = img; pictureBox1.Image = img;
} }
private void txtPath_TextChanged(object sender, EventArgs e)
{
}
private void chbUseCamera_CheckedChanged(object sender, EventArgs e) private void chbUseCamera_CheckedChanged(object sender, EventArgs e)
{ {
if (chbUseCamera.Checked) if (chbUseCamera.Checked)
{ {
cmbCameraList.Enabled = true ; cmbHalconCamera.Enabled = true ;
btnSelImage.Enabled = false; btnSelImage.Enabled = false;
} }
else else
{ {
cmbCameraList.Enabled = false ; cmbHalconCamera.Enabled = false ;
btnSelImage.Enabled = true ; btnSelImage.Enabled = true ;
} }
} }
...@@ -202,5 +266,19 @@ namespace CodeLibrary ...@@ -202,5 +266,19 @@ namespace CodeLibrary
File.Delete(path); File.Delete(path);
} }
} }
private void chbHalcon_CheckedChanged(object sender, EventArgs e)
{
if (chbHalcon.Checked)
{
cmbHalconCamera.Visible = true;
cmbCamera.Visible = false;
}
else
{
cmbHalconCamera.Visible = false ;
cmbCamera.Visible = true ;
}
}
} }
} }
...@@ -8,8 +8,6 @@ using System.Linq; ...@@ -8,8 +8,6 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using ZXing;
namespace CodeLibrary namespace CodeLibrary
{ {
public class HDCodeHelper public class HDCodeHelper
......
20181123
增加相机本身获取图片的代码,后续扫码都直接从相机中获取图片,然后扫码
此文件太大,无法显示。
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!