窗体如图:
本窗体功能为打开相机、相机的实时显示采集、关闭相机。
HALCON代码:
* Image Acquisition 01: Code generated by Image Acquisition 01
* Image Acquisition 01: Attention: The initialization may fail in case parameters need to
* Image Acquisition 01: be set in a specific order (e.g., image resolution vs. offset).
open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '[0] Integrated Webcam', 0, -1, AcqHandle)
set_framegrabber_param (AcqHandle, 'grab_timeout', 2000)
set_framegrabber_param (AcqHandle, 'gamma', 116)
grab_image_start (AcqHandle, -1)
while (true)
grab_image_async (Image, AcqHandle, -1)
* Image Acquisition 01: Do something
get_image_size (Image, Width, Height)
dev_set_part (0, 0, Height, Width)
endwhile
close_framegrabber (AcqHandle)
HALCON导出的C#代码:
//
// File generated by HDevelop for HALCON/.NET (C#) Version 19.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=null;
// Local control variables
HTuple hv_AcqHandle = new HTuple(), hv_Width = new HTuple();
HTuple hv_Height = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Image);
//Image Acquisition 01: Code generated by Image Acquisition 01
//Image Acquisition 01: Attention: The initialization may fail in case parameters need to
//Image Acquisition 01: be set in a specific order (e.g., image resolution vs. offset).
hv_AcqHandle.Dispose();
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[0] Integrated Webcam", 0, -1, out hv_AcqHandle);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "grab_timeout", 2000);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "gamma", 116);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
while ((int)(1) != 0)
{
ho_Image.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
//Image Acquisition 01: Do something
hv_Width.Dispose();hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height, hv_Width);
}
}
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
ho_Image.Dispose();
hv_AcqHandle.Dispose();
hv_Width.Dispose();
hv_Height.Dispose();
}
#endif
}
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
public class HDevelopExportApp
{
static void Main(string[] args)
{
new HDevelopExport();
}
}
#endif
程序源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
namespace open_cam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
HObject ho_Image = null;
HTuple hv_AcqHandle = new HTuple();
HTuple hv_Width = new HTuple();
HTuple hv_Height = new HTuple();
private void button1_Click(object sender, EventArgs e)
{
//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
//Image Acquisition 01: Attention: The initialization may fail in case parameters need to
//Image Acquisition 01: be set in a specific order (e.g., image resolution vs. offset).
hv_AcqHandle.Dispose();
HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
-1, "false", "default", "[0] Integrated Webcam", 0, -1, out hv_AcqHandle);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "grab_timeout", 2000);
HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "gamma", 116);
HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
}
HTuple hv_WindowHandle = new HTuple();
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
HOperatorSet.OpenWindow(0, 0, hWindowControl1.Width, hWindowControl1.Height, hWindowControl1.HalconWindow, "visible", "", out hv_WindowHandle);//open window,将图片的长宽改为控件的长宽
HDevWindowStack.Push(hv_WindowHandle);//入栈
}
private void timer1_Tick(object sender, EventArgs e)
{
ho_Image.Dispose();
//hv_AcqHandle.Dispose();
HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
//Image Acquisition 01: Do something
hv_Width.Dispose(); hv_Height.Dispose();
HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
if (HDevWindowStack.IsOpen())
{
HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height, hv_Width);
}
if (HDevWindowStack.IsOpen())//实时显示
{
HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
}
}
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = false;//一定要关闭timer
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
ho_Image.Dispose();
}
}
}
注意:
在button3的事件中记得关闭timer
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = false;//一定要关闭timer
HOperatorSet.CloseFramegrabber(hv_AcqHandle);
ho_Image.Dispose();
}
图像的显示函数:
if (HDevWindowStack.IsOpen())//实时显示
{
HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
}