Hello Mat

 找回密码
 立即注册
查看: 6660|回复: 0

15-Halcon和C#驱动相机拍照

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2019-3-31 22:35:39 | 显示全部楼层 |阅读模式
15-Halcon和C#驱动相机拍照
百度网盘链接:https://pan.baidu.com/s/1O8t7b3t5uj6MGFNkQOCFuw  提取码:opv1
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
代码如下:
  1. using HalconDotNet;

  2. namespace Csharp_CameraSnapShot
  3. {
  4.     public partial class Form1 : Form
  5.     {
  6.         HObject ho_Image = null;
  7.         HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
  8.         HTuple hv_AcqHandle = null;
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.         }

  13.         //定义一个线程
  14.         Thread HPcamera;

  15.         private void RealTimeSnap_Click(object sender, EventArgs e)
  16.         {
  17.             // 图像实时采集
  18.             HPcamera = new Thread(getCameraImage);
  19.             HPcamera.Start();
  20.         }

  21.         void getCameraImage()
  22.         {
  23.             HOperatorSet.GenEmptyObj(out ho_Image);
  24.             //Image Acquisition 01: Code generated by Image Acquisition 01
  25.             HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
  26.                 -1, "false", "default", "[0] HP TrueVision HD", 0, -1, out hv_AcqHandle);
  27.             HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
  28.             while (true)
  29.             {
  30.                 ho_Image.Dispose();
  31.                 HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
  32.                 HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
  33.                 //Image Acquisition 01: Do something
  34.                 HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, hv_Height - 1, hv_Width - 1);
  35.                 HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);
  36.             }
  37.         }

  38.        private void StopSnap_Click(object sender, EventArgs e)
  39.         {
  40.             if (ho_Image != null)
  41.             {
  42.                 ho_Image.Dispose();
  43.             }
  44.             ho_Image = null;
  45.             // 停止
  46.             HPcamera.Abort();
  47.             HOperatorSet.CloseFramegrabber(hv_AcqHandle);
  48.         }

  49.         private void SnapSingleImage_Click(object sender, EventArgs e)
  50.         {
  51.             if (ho_Image == null)
  52.             {
  53.                 MessageBox.Show("图像为空,请确认相机是否打开!!!");
  54.                 return;
  55.             }
  56.             // 此步表示已经获取到了图像
  57.             HOperatorSet.WriteImage(ho_Image, "bmp", 0, "1.bmp");
  58.             try
  59.             {
  60.                 // 可以对图像进行分析
  61.             }
  62.             catch(Exception)
  63.             {

  64.             }
  65.         }

  66.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  67.         {
  68.             if (ho_Image != null)
  69.             {
  70.                 ho_Image.Dispose();
  71.             }
  72.             if (HPcamera != null)
  73.             {
  74.                 HPcamera.Abort(); //结束这个线程
  75.             }
  76.         }


  77.     }
  78. }
复制代码








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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 02:07 , Processed in 0.207712 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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