HALCON day1 C# 打开相机

在HALCON里面打开图像采集助手

HALCON day1 C# 打开相机_第1张图片

会自动识别大相机类型

HALCON day1 C# 打开相机_第2张图片

在连接选项卡中,可以看到使用的接口库和设备名称,点击下面的连接按钮,尝试采集和实时图像,看在显示窗口是否可以采集到图像

HALCON day1 C# 打开相机_第3张图片

图像窗口有图像输出,说明相机连接正常

HALCON day1 C# 打开相机_第4张图片

代码生成窗口,选择采集单幅图片,点击插入代码。

HALCON day1 C# 打开相机_第5张图片

HALCON的程序窗口将显示生成的程序 

* Image Acquisition 01: Code generated by Image Acquisition 01
open_framegrabber ('USB3Vision', 1, 1, 0, 0, 0, 0, 'progressive', 12, 'gray', -1, 'false', 'default', '1E1000DCD06E_PointGreyResearch_Grasshopper3GS3U391S6M', 0, -1, AcqHandle)
grab_image_start (AcqHandle, -1)
grab_image_async (Image, AcqHandle, -1)
* Image Acquisition 01: Do something
close_framegrabber (AcqHandle)

 

在文件中选择导出语言

HALCON day1 C# 打开相机_第6张图片

选择导出语言的类型,这里选择了C#

HALCON day1 C# 打开相机_第7张图片

//
// File generated by HDevelop for HALCON/.NET (C#) Version 20.11.0.0
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936).
// 
// Please note that non-ASCII characters in string constants are exported
// as octal codes in order to guarantee that the strings are correctly
// created on all systems, independent on any compiler settings.
// 
// Source files with different encoding should not be mixed in one project.
//

using HalconDotNet;

public partial class HDevelopExport
{
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
  public HDevelopExport()
  {
    // Default settings used in HDevelop
    HOperatorSet.SetSystem("width", 512);
    HOperatorSet.SetSystem("height", 512);
    if (HalconAPI.isWindows)
      HOperatorSet.SetSystem("use_window_thread","true");
    action();
  }
#endif

#if !NO_EXPORT_MAIN
  // Main procedure 
  private void action()
  {


    // Local iconic variables 

    HObject ho_Image;

    // Local control variables 

    HTuple hv_AcqHandle = new HTuple();
    // Initialize local and output iconic variables 
    HOperatorSet.GenEmptyObj(out ho_Image);
    //Image Acquisition 01: Code generated by Image Acquisition 01
    hv_AcqHandle.Dispose();
    HOperatorSet.OpenFramegrabber("USB3Vision", 1, 1, 0, 0, 0, 0, "progressive", 
        12, "gray", -1, "false", "default", "1E1000DCD06E_PointGreyResearch_Grasshopper3GS3U391S6M", 
        0, -1, out hv_AcqHandle);
    HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
    ho_Image.Dispose();
    HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
    //Image Acquisition 01: Do something
    HOperatorSet.CloseFramegrabber(hv_AcqHandle);

    ho_Image.Dispose();

    hv_AcqHandle.Dispose();

  }

#endif


}
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
public class HDevelopExportApp
{
  static void Main(string[] args)
  {
    new HDevelopExport();
  }
}
#endif

上面程序中Action中的代码就是执行打开相机操作的代码。

打开VisualStudio开发环境,新建一个WinFormApp

HALCON day1 C# 打开相机_第8张图片

命名为halconOpenCamera

HALCON day1 C# 打开相机_第9张图片

拷贝Halcon安装目录下的

HALCON day1 C# 打开相机_第10张图片

HALCON day1 C# 打开相机_第11张图片

到C#程序下的bin目录中

HALCON day1 C# 打开相机_第12张图片

添加项目引用

HALCON day1 C# 打开相机_第13张图片

HALCON day1 C# 打开相机_第14张图片

在Form设计中加入picturebox和按钮控件

HALCON day1 C# 打开相机_第15张图片

在form1.cs中使用 HalconDotNet命名空间

using HalconDotNet;

复制Halcon生成.cs程序中Action函数中的语句到button1_Click()函数下,这时候并没有Halcon窗体,所以没法显示图片。

这时候要创建一个Halcon窗体

 public void CreateHalconWindow()
        {   
            HTuple FartherWindow = this.pictureBox1.Handle;
            HOperatorSet.SetWindowAttr("background_color", "blue");
            HOperatorSet.OpenWindow(0, 0, this.pictureBox1.Width, this.pictureBox1.Height, FartherWindow, "visible","", out WindowID);
        }

把CreateHalconWindow()函数放到初始化组件之后。

把下面几行代码插入到button1_Click()函数中获得图像之后。

 HTuple width = null, height = null;
 HOperatorSet.GetImageSize(ho_Image, out width, out height);
 HOperatorSet.SetColor(WindowID, "yellow");
 HOperatorSet.SetPart(WindowID, 0, 0, height, width);
 HOperatorSet.DispObj(ho_Image, WindowID);

所以整个程序是这样的。

using HalconDotNet;
namespace halconOpenCamera
{
    public partial class Form1 : Form
    {
        private HTuple WindowID;
        
        public Form1()
        {
            InitializeComponent();
            CreateHalconWindow();
        }

        public void CreateHalconWindow()
        {
            HTuple FartherWindow = this.pictureBox1.Handle;
            HOperatorSet.SetWindowAttr("background_color", "black");
            HOperatorSet.OpenWindow(0, 0, this.pictureBox1.Width, this.pictureBox1.Height, FartherWindow, "visible", "", out WindowID);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            HObject ho_Image;

            // Local control variables 

            HTuple hv_AcqHandle = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            //Image Acquisition 01: Code generated by Image Acquisition 01
            hv_AcqHandle.Dispose();
            HOperatorSet.OpenFramegrabber("USB3Vision", 1, 1, 0, 0, 0, 0, "progressive",
                12, "gray", -1, "false", "default", "1E1000DCD06E_PointGreyResearch_Grasshopper3GS3U391S6M",
                0, -1, out hv_AcqHandle);
            HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
            ho_Image.Dispose();
            HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
            //Image Acquisition 01: Do something
            HOperatorSet.CloseFramegrabber(hv_AcqHandle);
            HTuple width = null, height = null;
            HOperatorSet.GetImageSize(ho_Image, out width, out height);
            HOperatorSet.SetColor(WindowID, "yellow");
            HOperatorSet.SetPart(WindowID, 0, 0, height, width);
            HOperatorSet.DispObj(ho_Image, WindowID);
            ho_Image.Dispose();

            hv_AcqHandle.Dispose();
        }
    }
}

运行程序

HALCON day1 C# 打开相机_第16张图片

打开相机成功,正常获取图像。

你可能感兴趣的:(c#)