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
...@@ -33,27 +33,27 @@ ...@@ -33,27 +33,27 @@
this.btnSelImage = new System.Windows.Forms.Button(); this.btnSelImage = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.txtResult = new System.Windows.Forms.TextBox(); this.txtResult = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button(); this.btnErZhi = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button(); this.btnGray = new System.Windows.Forms.Button();
this.hWindowControl1 = new HalconDotNet.HWindowControl(); this.hWindowControl1 = new HalconDotNet.HWindowControl();
this.btnbarCode = new System.Windows.Forms.Button(); this.btnbarCode = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button(); this.btnLearn = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button(); this.btnDCode = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button(); this.btnClearLog = new System.Windows.Forms.Button();
this.cmbCount = new System.Windows.Forms.ComboBox(); this.cmbCount = new System.Windows.Forms.ComboBox();
this.lblCount = new System.Windows.Forms.Label(); this.lblCount = new System.Windows.Forms.Label();
this.button8 = new System.Windows.Forms.Button(); this.btnCameraImage = new System.Windows.Forms.Button();
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.cmbCamera = new System.Windows.Forms.ComboBox(); this.cmbCamera = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.cmbCodeType = new System.Windows.Forms.ComboBox(); this.cmbCodeType = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button(); this.btnAn = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.chbUseParam = new System.Windows.Forms.CheckBox(); this.chbUseParam = new System.Windows.Forms.CheckBox();
this.txtParamPath = new System.Windows.Forms.TextBox(); this.txtParamPath = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label();
this.btnLight = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
...@@ -97,27 +97,27 @@ ...@@ -97,27 +97,27 @@
this.txtResult.Size = new System.Drawing.Size(450, 290); this.txtResult.Size = new System.Drawing.Size(450, 290);
this.txtResult.TabIndex = 3; this.txtResult.TabIndex = 3;
// //
// button2 // btnErZhi
// //
this.button2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnErZhi.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button2.Location = new System.Drawing.Point(524, 13); this.btnErZhi.Location = new System.Drawing.Point(524, 13);
this.button2.Name = "button2"; this.btnErZhi.Name = "btnErZhi";
this.button2.Size = new System.Drawing.Size(73, 33); this.btnErZhi.Size = new System.Drawing.Size(73, 33);
this.button2.TabIndex = 5; this.btnErZhi.TabIndex = 5;
this.button2.Text = "二值化"; this.btnErZhi.Text = "二值化";
this.button2.UseVisualStyleBackColor = true; this.btnErZhi.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click); this.btnErZhi.Click += new System.EventHandler(this.btnErZhi_Click);
// //
// button3 // btnGray
// //
this.button3.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnGray.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button3.Location = new System.Drawing.Point(524, 51); this.btnGray.Location = new System.Drawing.Point(524, 51);
this.button3.Name = "button3"; this.btnGray.Name = "btnGray";
this.button3.Size = new System.Drawing.Size(73, 33); this.btnGray.Size = new System.Drawing.Size(73, 33);
this.button3.TabIndex = 6; this.btnGray.TabIndex = 6;
this.button3.Text = "图像转灰"; this.btnGray.Text = "图像转灰";
this.button3.UseVisualStyleBackColor = true; this.btnGray.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click); this.btnGray.Click += new System.EventHandler(this.btnGray_Click);
// //
// hWindowControl1 // hWindowControl1
// //
...@@ -142,41 +142,41 @@ ...@@ -142,41 +142,41 @@
this.btnbarCode.TabIndex = 9; this.btnbarCode.TabIndex = 9;
this.btnbarCode.Text = "一维码识别"; this.btnbarCode.Text = "一维码识别";
this.btnbarCode.UseVisualStyleBackColor = true; this.btnbarCode.UseVisualStyleBackColor = true;
this.btnbarCode.Click += new System.EventHandler(this.btnHalconP_Click); this.btnbarCode.Click += new System.EventHandler(this.btnbarCode_Click);
// //
// button5 // btnLearn
// //
this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnLearn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button5.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnLearn.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button5.Location = new System.Drawing.Point(966, 13); this.btnLearn.Location = new System.Drawing.Point(966, 13);
this.button5.Name = "button5"; this.btnLearn.Name = "btnLearn";
this.button5.Size = new System.Drawing.Size(109, 33); this.btnLearn.Size = new System.Drawing.Size(109, 33);
this.button5.TabIndex = 10; this.btnLearn.TabIndex = 10;
this.button5.Text = "学习"; this.btnLearn.Text = "学习";
this.button5.UseVisualStyleBackColor = true; this.btnLearn.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click); this.btnLearn.Click += new System.EventHandler(this.btnLearn_Click);
// //
// button6 // btnDCode
// //
this.button6.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnDCode.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button6.Location = new System.Drawing.Point(795, 12); this.btnDCode.Location = new System.Drawing.Point(795, 12);
this.button6.Name = "button6"; this.btnDCode.Name = "btnDCode";
this.button6.Size = new System.Drawing.Size(112, 33); this.btnDCode.Size = new System.Drawing.Size(112, 33);
this.button6.TabIndex = 11; this.btnDCode.TabIndex = 11;
this.button6.Text = "条码识别"; this.btnDCode.Text = "二维码识别";
this.button6.UseVisualStyleBackColor = true; this.btnDCode.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click); this.btnDCode.Click += new System.EventHandler(this.btnDCode_Click);
// //
// button7 // btnClearLog
// //
this.button7.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.button7.Location = new System.Drawing.Point(683, 51); this.btnClearLog.Location = new System.Drawing.Point(683, 51);
this.button7.Name = "button7"; this.btnClearLog.Name = "btnClearLog";
this.button7.Size = new System.Drawing.Size(84, 33); this.btnClearLog.Size = new System.Drawing.Size(84, 33);
this.button7.TabIndex = 12; this.btnClearLog.TabIndex = 12;
this.button7.Text = "清理日志"; this.btnClearLog.Text = "清理日志";
this.button7.UseVisualStyleBackColor = true; this.btnClearLog.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click); this.btnClearLog.Click += new System.EventHandler(this.btnClearLog_Click);
// //
// cmbCount // cmbCount
// //
...@@ -219,16 +219,16 @@ ...@@ -219,16 +219,16 @@
this.lblCount.TabIndex = 15; this.lblCount.TabIndex = 15;
this.lblCount.Text = "条码数量:"; this.lblCount.Text = "条码数量:";
// //
// button8 // btnCameraImage
// //
this.button8.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnCameraImage.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button8.Location = new System.Drawing.Point(10, 51); this.btnCameraImage.Location = new System.Drawing.Point(10, 51);
this.button8.Name = "button8"; this.btnCameraImage.Name = "btnCameraImage";
this.button8.Size = new System.Drawing.Size(117, 33); this.btnCameraImage.Size = new System.Drawing.Size(117, 33);
this.button8.TabIndex = 17; this.btnCameraImage.TabIndex = 17;
this.button8.Text = "摄像机获取图片"; this.btnCameraImage.Text = "相机获取图片";
this.button8.UseVisualStyleBackColor = true; this.btnCameraImage.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button8_Click); this.btnCameraImage.Click += new System.EventHandler(this.btnCameraImage_Click);
// //
// btnExit // btnExit
// //
...@@ -294,27 +294,16 @@ ...@@ -294,27 +294,16 @@
this.label3.TabIndex = 22; this.label3.TabIndex = 22;
this.label3.Text = "条码类型:"; this.label3.Text = "条码类型:";
// //
// button1 // btnAn
// //
this.button1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnAn.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(601, 13); this.btnAn.Location = new System.Drawing.Point(601, 51);
this.button1.Name = "button1"; this.btnAn.Name = "btnAn";
this.button1.Size = new System.Drawing.Size(73, 33); this.btnAn.Size = new System.Drawing.Size(73, 33);
this.button1.TabIndex = 24; this.btnAn.TabIndex = 25;
this.button1.Text = "提亮"; this.btnAn.Text = "变暗";
this.button1.UseVisualStyleBackColor = true; this.btnAn.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click); this.btnAn.Click += new System.EventHandler(this.btnAn_Click);
//
// button4
//
this.button4.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button4.Location = new System.Drawing.Point(601, 51);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(73, 33);
this.button4.TabIndex = 25;
this.button4.Text = "变暗";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
// //
// chbUseParam // chbUseParam
// //
...@@ -349,6 +338,17 @@ ...@@ -349,6 +338,17 @@
this.label4.TabIndex = 27; this.label4.TabIndex = 27;
this.label4.Text = "参数路径"; this.label4.Text = "参数路径";
// //
// btnLight
//
this.btnLight.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnLight.Location = new System.Drawing.Point(601, 13);
this.btnLight.Name = "btnLight";
this.btnLight.Size = new System.Drawing.Size(73, 33);
this.btnLight.TabIndex = 24;
this.btnLight.Text = "提亮";
this.btnLight.UseVisualStyleBackColor = true;
this.btnLight.Click += new System.EventHandler(this.btnLigth_Click);
//
// FrmCodeDecode // FrmCodeDecode
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
...@@ -357,23 +357,23 @@ ...@@ -357,23 +357,23 @@
this.Controls.Add(this.txtParamPath); this.Controls.Add(this.txtParamPath);
this.Controls.Add(this.label4); this.Controls.Add(this.label4);
this.Controls.Add(this.chbUseParam); this.Controls.Add(this.chbUseParam);
this.Controls.Add(this.button4); this.Controls.Add(this.btnAn);
this.Controls.Add(this.button1); this.Controls.Add(this.btnLight);
this.Controls.Add(this.cmbCodeType); this.Controls.Add(this.cmbCodeType);
this.Controls.Add(this.label3); this.Controls.Add(this.label3);
this.Controls.Add(this.cmbCamera); this.Controls.Add(this.cmbCamera);
this.Controls.Add(this.label2); this.Controls.Add(this.label2);
this.Controls.Add(this.btnExit); this.Controls.Add(this.btnExit);
this.Controls.Add(this.button8); this.Controls.Add(this.btnCameraImage);
this.Controls.Add(this.cmbCount); this.Controls.Add(this.cmbCount);
this.Controls.Add(this.lblCount); this.Controls.Add(this.lblCount);
this.Controls.Add(this.button7); this.Controls.Add(this.btnClearLog);
this.Controls.Add(this.button6); this.Controls.Add(this.btnDCode);
this.Controls.Add(this.button5); this.Controls.Add(this.btnLearn);
this.Controls.Add(this.btnbarCode); this.Controls.Add(this.btnbarCode);
this.Controls.Add(this.hWindowControl1); this.Controls.Add(this.hWindowControl1);
this.Controls.Add(this.button3); this.Controls.Add(this.btnGray);
this.Controls.Add(this.button2); this.Controls.Add(this.btnErZhi);
this.Controls.Add(this.txtResult); this.Controls.Add(this.txtResult);
this.Controls.Add(this.pictureBox1); this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.btnSelImage); this.Controls.Add(this.btnSelImage);
...@@ -397,27 +397,27 @@ ...@@ -397,27 +397,27 @@
private System.Windows.Forms.Button btnSelImage; private System.Windows.Forms.Button btnSelImage;
private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.TextBox txtResult; private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Button button2; private System.Windows.Forms.Button btnErZhi;
private System.Windows.Forms.Button button3; private System.Windows.Forms.Button btnGray;
private HalconDotNet.HWindowControl hWindowControl1; private HalconDotNet.HWindowControl hWindowControl1;
private System.Windows.Forms.Button btnbarCode; private System.Windows.Forms.Button btnbarCode;
private System.Windows.Forms.Button button5; private System.Windows.Forms.Button btnLearn;
private System.Windows.Forms.Button button6; private System.Windows.Forms.Button btnDCode;
private System.Windows.Forms.Button button7; private System.Windows.Forms.Button btnClearLog;
private System.Windows.Forms.ComboBox cmbCount; private System.Windows.Forms.ComboBox cmbCount;
private System.Windows.Forms.Label lblCount; private System.Windows.Forms.Label lblCount;
private System.Windows.Forms.Button button8; private System.Windows.Forms.Button btnCameraImage;
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 cmbCamera; private System.Windows.Forms.ComboBox cmbCamera;
private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cmbCodeType; private System.Windows.Forms.ComboBox cmbCodeType;
private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button1; private System.Windows.Forms.Button btnAn;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.CheckBox chbUseParam; private System.Windows.Forms.CheckBox chbUseParam;
private System.Windows.Forms.TextBox txtParamPath; private System.Windows.Forms.TextBox txtParamPath;
private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button btnLight;
} }
} }
...@@ -19,8 +19,10 @@ namespace CodeLibrary ...@@ -19,8 +19,10 @@ 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)
{ {
pictureBox1.Image = bit; 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;
}
} }
camera.Close(); private void btnCameraImage_Click(object sender, EventArgs e)
{
Bitmap bitmap = GetCameraBitmap();
if (bitmap != null)
{
HDLogUtil.info("从相机【" + cmbCamera.Text + "】获取到一张图片");
pictureBox1.Image = bitmap;
HObject hoImage = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
HDCodeLearnHelper.DefaultImage = hoImage;
}
} }
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;
...@@ -266,7 +269,6 @@ ...@@ -266,7 +269,6 @@
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
...@@ -16,30 +16,77 @@ namespace CodeLibrary ...@@ -16,30 +16,77 @@ 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 (chbHalcon.Checked)
{
cameraName = cmbHalconCamera.Text;
if (cameraName.Equals("")) if (cameraName.Equals(""))
{ {
MessageBox.Show("请选择摄像机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("请先选择相机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
} }
else else
{ {
cameraName = ""; Bitmap bitmap = GetCameraBitmap();
if (pictureBox1.Image == null) if (bitmap != null)
{
HDLogUtil.info("从相机【" + cmbCamera.Text + "】获取到一张图片");
pictureBox1.Image = bitmap;
HObject hoImage = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
HDCodeLearnHelper.DefaultImage = hoImage;
}
else
{
return;
}
}
}
else
{ {
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();
} }
...@@ -168,21 +237,16 @@ namespace CodeLibrary ...@@ -168,21 +237,16 @@ namespace CodeLibrary
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
......
//引用MvCameraControl.Net.dll
using System;
using MvCamCtrl.NET;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace CodeLibrary
{
/// <summary>
/// 海康相机
/// </summary>
public class HIKCamera
{
public static HIKCamera Instance = new HIKCamera();
/// <summary>
/// 当前相机
/// </summary>
private MyCamera cameraCurr;
/// <summary>
/// 所有相机列表
/// </summary>
private MyCamera.MV_CC_DEVICE_INFO_LIST cameraAll;
/// <summary>
/// 所有相机的名称
/// </summary>
private List<string> cameraName = new List<string>();
/// <summary>
/// 海康相机
/// </summary>
private HIKCamera()
{
cameraAll = new MyCamera.MV_CC_DEVICE_INFO_LIST();
Load();
}
/// <summary>
/// 错误信息
/// </summary>
public string ErrInfo { set; get; }
/// <summary>
/// 相机总数
/// </summary>
public int Count
{
get { return (int)cameraAll.nDeviceNum; }
}
/// <summary>
/// 相机名称,ModelName,SerialNumber
/// </summary>
public string[] CameraName
{
get { return cameraName.ToArray(); }
}
/// <summary>
/// 当前相机是否打开
/// </summary>
public bool IsOpen
{
get
{
if (cameraCurr == null)
return false;
else
return true;
}
}
/// <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()
{
int rtn = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref cameraAll);
if (rtn != MyCamera.MV_OK) return;
cameraName.Clear();
string s = "";
for (int i = 0; i < cameraAll.nDeviceNum; i++)
{
MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(cameraAll.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));
if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
s = "GigE:" + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")";
}
else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)
{
IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));
s = "USB:" + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")";
}
cameraName.Add(s);
}
}
/// <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 >= Count) return false;
if (cameraCurr != null) Close();
try
{
MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(cameraAll.pDeviceInfo[idx], typeof(MyCamera.MV_CC_DEVICE_INFO));
cameraCurr = new MyCamera();
if (cameraCurr == null) return false;
int nRet = cameraCurr.MV_CC_CreateDevice_NET(ref device);
if (nRet != MyCamera.MV_OK) return false;
nRet = cameraCurr.MV_CC_OpenDevice_NET();
if (nRet != MyCamera.MV_OK)
{
cameraCurr.MV_CC_DestroyDevice_NET();
return false;
}
if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)
{
int nPacketSize = cameraCurr.MV_CC_GetOptimalPacketSize_NET();
if (nPacketSize > 0) nRet = cameraCurr.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
}
cameraCurr.MV_CC_SetEnumValue_NET("AcquisitionMode", 2); //工作在连续模式
cameraCurr.MV_CC_SetEnumValue_NET("TriggerMode", 0); //连续模式
MyCamera.MVCC_INTVALUE pstValue = new MyCamera.MVCC_INTVALUE();
nRet = cameraCurr.MV_CC_GetWidth_NET(ref pstValue);
Width = (int)pstValue.nCurValue;
nRet = cameraCurr.MV_CC_GetHeight_NET(ref pstValue);
Height = (int)pstValue.nCurValue;
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
/// <summary>
/// 关闭当前相机
/// </summary>
public void Close()
{
if (cameraCurr != null)
{
cameraCurr.MV_CC_CloseDevice_NET();
cameraCurr.MV_CC_DestroyDevice_NET();
cameraCurr = null;
}
}
/// <summary>
/// 停止抓取数据
/// </summary>
public void Stop()
{
if (cameraCurr == null) return;
int rtn = cameraCurr.MV_CC_StopGrabbing_NET();
if (rtn != MyCamera.MV_OK) return;
}
/// <summary>
/// 抓取一张图像
/// </summary>
public void GrabOne()
{
int rtn = cameraCurr.MV_CC_StartGrabbing_NET();
if (rtn != MyCamera.MV_OK) return;
MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
rtn = cameraCurr.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
if (rtn != MyCamera.MV_OK) return;
uint dataSize = stParam.nCurValue;
byte[] dataArr = new byte[dataSize];
uint buffSize = dataSize * 3 + 2048;
byte[] buffArr = new byte[buffSize];
IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(dataArr, 0);
MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
rtn = cameraCurr.MV_CC_GetOneFrameTimeout_NET(pData, dataSize, ref stFrameInfo, 100000);
if (rtn != MyCamera.MV_OK) return;
MyCamera.MvGvspPixelType enDstPixelType = stFrameInfo.enPixelType;
switch (stFrameInfo.enPixelType)
{
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12_Packed:
enDstPixelType = stFrameInfo.enPixelType; break;
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG8:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_YUYV_Packed:
case MyCamera.MvGvspPixelType.PixelType_Gvsp_YCBCR411_8_CBYYCRYY:
enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed; break;
}
IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(buffArr, 0);
MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
stConverPixelParam.nWidth = stFrameInfo.nWidth;
stConverPixelParam.nHeight = stFrameInfo.nHeight;
stConverPixelParam.pSrcData = pData;
stConverPixelParam.nSrcDataLen = stFrameInfo.nFrameLen;
stConverPixelParam.enSrcPixelType = stFrameInfo.enPixelType;
stConverPixelParam.enDstPixelType = enDstPixelType;
stConverPixelParam.pDstBuffer = pImage;
stConverPixelParam.nDstBufferSize = buffSize;
rtn = cameraCurr.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
if (rtn != MyCamera.MV_OK) return;
if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
{
Image = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage);
ColorPalette cp = Image.Palette;
for (int i = 0; i < 256; i++)
cp.Entries[i] = Color.FromArgb(i, i, i);
Image.Palette = cp;
int picSize = Image.Width * Image.Height;
Buffer = new byte[picSize];
Array.Copy(buffArr, Buffer, picSize);
//Rectangle rect = new Rectangle(0, 0, Image.Width, Image.Height);
//BitmapData bmpData = Image.LockBits(rect, ImageLockMode.ReadWrite, Image.PixelFormat);
//IntPtr iPtr = bmpData.Scan0;
//int picSize = Image.Width * Image.Height;
//Buffer = new byte[picSize];
//Marshal.Copy(iPtr, Buffer, 0, picSize);
//Image.UnlockBits(bmpData);
}
else
{
for (int i = 0; i < stFrameInfo.nHeight; i++)
{
for (int j = 0; j < stFrameInfo.nWidth; j++)
{
byte chRed = buffArr[i * stFrameInfo.nWidth * 3 + j * 3];
buffArr[i * stFrameInfo.nWidth * 3 + j * 3] = buffArr[i * stFrameInfo.nWidth * 3 + j * 3 + 2];
buffArr[i * stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
}
}
Image = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
int picSize = Image.Width * Image.Height * 3;
Buffer = new byte[picSize];
Array.Copy(buffArr, Buffer, picSize);
}
rtn = cameraCurr.MV_CC_StopGrabbing_NET();
if (rtn != MyCamera.MV_OK) return;
}
/// <summary>
/// 抓取连续图像,触发GrabImage事件
/// </summary>
/// <param name="hWnd"></param>
public void GrabContinuous(IntPtr hWnd)
{
int rtn = cameraCurr.MV_CC_StartGrabbing_NET();
if (rtn != MyCamera.MV_OK) return;
rtn = cameraCurr.MV_CC_Display_NET(hWnd);
if (rtn != MyCamera.MV_OK) return;
}
}
}
\ No newline at end of file \ No newline at end of file
20181123
增加相机本身获取图片的代码,后续扫码都直接从相机中获取图片,然后扫码
此文件太大,无法显示。
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!