Hello Mat

 找回密码
 立即注册
查看: 7115|回复: 1

采用halcon和C#驱动相机拍照和实时预览

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2018-10-24 22:51:46 | 显示全部楼层 |阅读模式
采用halcon和C#驱动相机拍照和实时预览
百度网盘链接:链接:http://pan.baidu.com/s/1c1BIKLi

具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1

代码如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11. using HalconDotNet;

  12. namespace Csharp_CameraSnapShot
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         HObject ho_Image = null;
  17.         HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
  18.         HTuple hv_AcqHandle = null;
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }

  23.         //定义一个线程
  24.         Thread HPcamera;

  25.         private void RealTimeSnap_Click(object sender, EventArgs e)
  26.         {
  27.             // 图像实时采集
  28.             HPcamera = new Thread(getCameraImage);
  29.             HPcamera.Start();
  30.         }

  31.         void getCameraImage()
  32.         {
  33.             HOperatorSet.GenEmptyObj(out ho_Image);
  34.             //Image Acquisition 01: Code generated by Image Acquisition 01
  35.             HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
  36.                 -1, "false", "default", "[0] HP TrueVision HD", 0, -1, out hv_AcqHandle);
  37.             HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
  38.             while (true)
  39.             {
  40.                 ho_Image.Dispose();
  41.                 HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
  42.                 HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
  43.                 //Image Acquisition 01: Do something
  44.                 HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_Height - 1, hv_Width - 1);
  45.                 HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
  46.             }
  47.         }

  48.         private void StopSnap_Click(object sender, EventArgs e)
  49.         {
  50.             if (ho_Image != null)
  51.             {
  52.                 ho_Image.Dispose();
  53.             }
  54.             ho_Image = null;
  55.             // 停止
  56.             HPcamera.Abort();
  57.             HOperatorSet.CloseFramegrabber(hv_AcqHandle);
  58.         }

  59.         private void SnapSingleImage_Click(object sender, EventArgs e)
  60.         {
  61.             if (ho_Image == null)
  62.             {
  63.                 MessageBox.Show("图像为空,请确认相机是否打开!!!");
  64.                 return;
  65.             }
  66.             // 此步表示已经获取到了图像
  67.             HOperatorSet.WriteImage(ho_Image, "bmp", 0, "1.bmp");
  68.             try
  69.             {
  70.                 // 可以对图像进行分析
  71.             }
  72.             catch(Exception)
  73.             {

  74.             }
  75.         }

  76.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  77.         {
  78.             if (ho_Image != null)
  79.             {
  80.                 ho_Image.Dispose();
  81.             }
  82.             if (HPcamera != null)
  83.             {
  84.                 HPcamera.Abort(); //结束这个线程
  85.             }
  86.         }


  87.     }
  88. }
复制代码

参考:
https://blog.csdn.net/liyuqian199695/article/details/65447395






算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-24 07:25 , Processed in 0.241056 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表