|
采用halcon和C#驱动相机拍照和实时预览
百度网盘链接:链接:http://pan.baidu.com/s/1c1BIKLi
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
代码如下:- 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 System.Threading;
- using HalconDotNet;
- namespace Csharp_CameraSnapShot
- {
- public partial class Form1 : Form
- {
- HObject ho_Image = null;
- HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
- HTuple hv_AcqHandle = null;
- public Form1()
- {
- InitializeComponent();
- }
- //定义一个线程
- Thread HPcamera;
- private void RealTimeSnap_Click(object sender, EventArgs e)
- {
- // 图像实时采集
- HPcamera = new Thread(getCameraImage);
- HPcamera.Start();
- }
- void getCameraImage()
- {
- HOperatorSet.GenEmptyObj(out ho_Image);
- //Image Acquisition 01: Code generated by Image Acquisition 01
- HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
- -1, "false", "default", "[0] HP TrueVision HD", 0, -1, out hv_AcqHandle);
- HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
- while (true)
- {
- ho_Image.Dispose();
- HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
- HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
- //Image Acquisition 01: Do something
- HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_Height - 1, hv_Width - 1);
- HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
- }
- }
- private void StopSnap_Click(object sender, EventArgs e)
- {
- if (ho_Image != null)
- {
- ho_Image.Dispose();
- }
- ho_Image = null;
- // 停止
- HPcamera.Abort();
- HOperatorSet.CloseFramegrabber(hv_AcqHandle);
- }
- private void SnapSingleImage_Click(object sender, EventArgs e)
- {
- if (ho_Image == null)
- {
- MessageBox.Show("图像为空,请确认相机是否打开!!!");
- return;
- }
- // 此步表示已经获取到了图像
- HOperatorSet.WriteImage(ho_Image, "bmp", 0, "1.bmp");
- try
- {
- // 可以对图像进行分析
- }
- catch(Exception)
- {
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (ho_Image != null)
- {
- ho_Image.Dispose();
- }
- if (HPcamera != null)
- {
- HPcamera.Abort(); //结束这个线程
- }
- }
- }
- }
复制代码
参考:
https://blog.csdn.net/liyuqian199695/article/details/65447395
|
|