Halcon 联合C# 实时采集相机图片

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;
using System.Threading;

namespace ImageShow
{
    public partial class Form1 : Form
    {
        //定义全局变量
        HTuple hv_Windowld;
        HTuple hv_AcqHandle;
        HObject ho_Image;
        HTuple hv_Width, hv_Height;
        bool is_GetImage = false;
        string path = @"G:\SoftwareLearning\halcon\ImageProcess\image\";
        string _str = "";
        public Form1()
        {
            InitializeComponent();
        }

        Thread thGetImage;
        private void GetImage_Click(object sender, EventArgs e)
        {
            if (is_GetImage == false)
            {
                is_GetImage = true;
                thGetImage = new Thread(ThreadGetImage);
                thGetImage.Start();
                thGetImage.IsBackground = true;
                GetImage.Text = "停止";
                
            }
            else
            {
                is_GetImage = false;
                thGetImage.Abort();
                GetImage.Text = "实时";
            }
            //保存图像
            HOperatorSet.WriteImage(ho_Image, "png",0, path + ".png");
            _str = "图片保存完成";
            MessageBox.Show(_str);
        }
        //定义实时采图的线程
        private void ThreadGetImage()
        {
            while (true)
            {
                //先销毁对象,避免内存泄漏
                ho_Image.Dispose();
                //采图显示
                HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle);
                HOperatorSet.DispObj(ho_Image, hv_Windowld);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //打开窗口
            HOperatorSet.OpenWindow(0, 0, pictureBoxShow.Width, pictureBoxShow.Height, pictureBoxShow.Handle, "", "", out hv_Windowld);
            //连接相机
            HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", -1, "false", "default", "[0] ", 0, -1, out hv_AcqHandle);
            //采集图像
            HOperatorSet.GrabImage(out  ho_Image, hv_AcqHandle);
            //获取图像大小
            HOperatorSet.GetImageSize(ho_Image,out hv_Width, out hv_Height);
            //设置在窗口中显示图像(不是实时显示)
            HOperatorSet.SetPart(hv_Windowld,0,0, hv_Height, hv_Width);
            HOperatorSet.DispObj(ho_Image, hv_Windowld);
        }
    }
}


在这里插入图片描述

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值