电脑连接海康相机并在PictureBox和HWindowControl中分别显示。

展示结果:

下面附上界面中所有控件的Name,只需照着红字设置对应的控件Name即可

下面附上小编主界面的全部代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using MvCameraControl;namespace MVC
{public partial class Form1 : Form{// ch:枚举的相机类型 | en:TLayerType for enumerate devicesreadonly DeviceTLayerType enumTLayerType = DeviceTLayerType.MvGigEDevice | DeviceTLayerType.MvUsbDevice| DeviceTLayerType.MvGenTLGigEDevice | DeviceTLayerType.MvGenTLCXPDevice | DeviceTLayerType.MvGenTLCameraLinkDevice | DeviceTLayerType.MvGenTLXoFDevice;List<IDeviceInfo> deviceInfoList = new List<IDeviceInfo>();IDevice device = null;bool isGrabbing = false; //是否正在取图Thread receiveThread = null;  //接收图像线程Thread receiveImage = null;IntPtr pictureBoxHandle = IntPtr.Zero; //显示图像的控件句柄public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){//初始化SDKSDKSystem.Initialize();//枚举设备RefreshDeviceList();}/// <summary>/// ch:显示错误信息 | en:Show error message/// </summary>private void ShowErrorMsg(string message, int errorCode){string errorMsg;if (errorCode == 0){errorMsg = message;}else{errorMsg = message + ": Error =" + String.Format("{0:X}", errorCode);}switch (errorCode){case MvError.MV_E_HANDLE: errorMsg += " Error or invalid handle "; break;case MvError.MV_E_SUPPORT: errorMsg += " Not supported function "; break;case MvError.MV_E_BUFOVER: errorMsg += " Cache is full "; break;case MvError.MV_E_CALLORDER: errorMsg += " Function calling order error "; break;case MvError.MV_E_PARAMETER: errorMsg += " Incorrect parameter "; break;case MvError.MV_E_RESOURCE: errorMsg += " Applying resource failed "; break;case MvError.MV_E_NODATA: errorMsg += " No data "; break;case MvError.MV_E_PRECONDITION: errorMsg += " Precondition error, or running environment changed "; break;case MvError.MV_E_VERSION: errorMsg += " Version mismatches "; break;case MvError.MV_E_NOENOUGH_BUF: errorMsg += " Insufficient memory "; break;case MvError.MV_E_UNKNOW: errorMsg += " Unknown error "; break;case MvError.MV_E_GC_GENERIC: errorMsg += " General error "; break;case MvError.MV_E_GC_ACCESS: errorMsg += " Node accessing condition error "; break;case MvError.MV_E_ACCESS_DENIED: errorMsg += " No permission "; break;case MvError.MV_E_BUSY: errorMsg += " Device is busy, or network disconnected "; break;case MvError.MV_E_NETER: errorMsg += " Network error "; break;}MessageBox.Show(errorMsg, "PROMPT");}private void RefreshDeviceList(){//创建设备列表cbDeviceList.Items.Clear();int nRet = DeviceEnumerator.EnumDevices(enumTLayerType, out deviceInfoList);if (nRet != MvError.MV_OK){ShowErrorMsg("Enumerate devices fail !", nRet);return;}//在窗体列表中显示设备名for (int i = 0; i < deviceInfoList.Count; i++){IDeviceInfo deviceInfo = deviceInfoList[i];if (deviceInfo.UserDefinedName != ""){cbDeviceList.Items.Add(deviceInfo.TLayerType.ToString() + ": " + deviceInfo.UserDefinedName + " (" + deviceInfo.SerialNumber + ")");}else{cbDeviceList.Items.Add(deviceInfo.TLayerType.ToString() + ": " + deviceInfo.ManufacturerName + " " + deviceInfo.ModelName + " (" + deviceInfo.SerialNumber + ")");}}//选择第一项if (deviceInfoList.Count != 0){cbDeviceList.SelectedIndex = 0;}}private void bnClose_Click(object sender, EventArgs e){//取流标志位清零if (isGrabbing == true){bnStopGrab_Click(sender, e);}//关闭设备if (device != null){device.Close();device.Dispose();}//控件操作SetCtrlWhenClose();}private void SetCtrlWhenClose(){bnOpen.Enabled = true;bnClose.Enabled = false;bnStartGrab.Enabled = false;bnStopGrab.Enabled = false;bnContinuesMode.Enabled = false;bnTriggerMode.Enabled = false;cbSoftTrigger.Enabled = false;bnTriggerExec.Enabled = false;tbExposure.Enabled = false;tbGain.Enabled = false;tbFrameRate.Enabled = false;bnGetParam.Enabled = false;bnSetParam.Enabled = false;}private void bnEnum_Click(object sender, EventArgs e){RefreshDeviceList();}private void bnOpen_Click(object sender, EventArgs e){if (deviceInfoList.Count == 0 || cbDeviceList.SelectedIndex == -1){ShowErrorMsg("No device, please select", 0);return;}//获取选择的设备信息IDeviceInfo deviceInfo = deviceInfoList[cbDeviceList.SelectedIndex];try{//打开设备device = DeviceFactory.CreateDevice(deviceInfo);}catch (Exception ex){MessageBox.Show("Create Device fail !" + ex.Message);return;}int result = device.Open();if (result != MvError.MV_OK){ShowErrorMsg("Open Device fail", result);return;}//判断是否为gige设备if (device is IGigEDevice){//转换为gige设备IGigEDevice gigEDevice = device as IGigEDevice;//探测网络最佳包大小int optionPacketSize;result = gigEDevice.GetOptimalPacketSize(out optionPacketSize);if (result != MvError.MV_OK){ShowErrorMsg("Waring: Get packet Size failed !", result);}else{result = device.Parameters.SetIntValue("GevSCPSPPacketSize", (long)optionPacketSize);if (result != MvError.MV_OK){ShowErrorMsg("Waring: Set Packet Size failed", result);}}}//设置采集卡连续模式device.Parameters.SetEnumValueByString("AcauisitionMode", "Continuous");device.Parameters.SetEnumValueByString("TriggerMode", "Off");//关闭触发模式//控件操作SetCtrlWhenOpen();//获取参数bnGetParam_Click(null, null);}private void SetCtrlWhenOpen(){bnOpen.Enabled = false;bnClose.Enabled = true;bnStartGrab.Enabled = true;bnStopGrab.Enabled = false;bnContinuesMode.Enabled = true;bnContinuesMode.Checked = true;//bnContinuesMode_CheckedChanged(null,null);bnTriggerMode.Enabled = true;cbSoftTrigger.Enabled = false;bnTriggerExec.Enabled = false;tbExposure.Enabled = true;tbGain.Enabled = true;tbFrameRate.Enabled = true;bnGetParam.Enabled = true;bnSetParam.Enabled = true;}private void GetTriggerMode(){IEnumValue enumValue;int result = device.Parameters.GetEnumValue("TriggerMode", out enumValue);if (result == MvError.MV_OK){if (enumValue.CurEnumEntry.Symbolic == "On"){bnTriggerMode.Checked = true;bnContinuesMode.Checked = false;result = device.Parameters.GetEnumValue("TriggerMode", out enumValue);if (result == MvError.MV_OK){if (enumValue.CurEnumEntry.Symbolic == "TriggerSoftware"){cbSoftTrigger.Enabled = true;cbSoftTrigger.Checked = true;if (isGrabbing){bnTriggerExec.Enabled = true;}}}}else{bnContinuesMode.Checked = true;bnTriggerMode.Checked = false;}}}private void bnGetParam_Click(object sender, EventArgs e){GetTriggerMode();IFloatValue floatValue;int result = device.Parameters.GetFloatValue("ExposureTime", out floatValue);if (result == MvError.MV_OK){tbExposure.Text = floatValue.CurValue.ToString("F1");}result = device.Parameters.GetFloatValue("Gain", out floatValue);if (result == MvError.MV_OK){tbGain.Text = floatValue.CurValue.ToString("F1");}result = device.Parameters.GetFloatValue("ResultingFrameRate", out floatValue);if (result == MvError.MV_OK){tbFrameRate.Text = floatValue.CurValue.ToString("F1");}IEnumValue enumValue;result = device.Parameters.GetEnumValue("PixelFormat", out enumValue);if (result == MvError.MV_OK){tbPixelFormat.Text = enumValue.CurEnumEntry.Symbolic;}}private void bnSetParam_Click(object sender, EventArgs e){try{float.Parse(tbExposure.Text);float.Parse(tbGain.Text);float.Parse(tbFrameRate.Text);}catch{ShowErrorMsg("Please enter correct type !", 0);return;}device.Parameters.SetEnumValue("ExposureAuto", 0);int result = device.Parameters.SetFloatValue("ExposureTime", float.Parse(tbExposure.Text));if (result != MvError.MV_OK){ShowErrorMsg("Set Gain Fail", result);}device.Parameters.SetEnumValue("GainAuto", 0);result = device.Parameters.SetFloatValue("Gain", float.Parse(tbGain.Text));if (result != MvError.MV_OK){ShowErrorMsg("Set Gain Fail !", result);}result = device.Parameters.SetBoolValue("AcquisitionFrameRateEnable", true);if (result != MvError.MV_OK){ShowErrorMsg("Set AcquisitionFrameRateEnable Fail !", result);}else{result = device.Parameters.SetFloatValue("AcquisitionFrameRate", float.Parse(tbFrameRate.Text));if (result != MvError.MV_OK){ShowErrorMsg("Set Frame Rate Fail !", result);}}}private void bnStopGrab_Click(object sender, EventArgs e){//标志位设为falseisGrabbing = false;receiveThread.Join();//停止采集int result = device.StreamGrabber.StopGrabbing();if (result != MvError.MV_OK){ShowErrorMsg("Stop Grabbing Fail !", result);}//控件操作SetCtrlWhenStopGrab();}private void SetCtrlWhenStopGrab(){bnStartGrab.Enabled = true;bnStopGrab.Enabled = false;cbSoftTrigger.Enabled = false;bnTriggerExec.Enabled = false;}private void bnTriggerExec_Click(object sender, EventArgs e){//触发命令int result = device.Parameters.SetCommandValue("TriggerSoftware");if (result != MvError.MV_OK){ShowErrorMsg("Trigger Software Fail !", result);}}private void cbSoftTrigger_CheckedChanged(object sender, EventArgs e){//触发源设为软触发if (cbSoftTrigger.Checked){device.Parameters.SetEnumValueByString("TriggerSource", "Software");if (isGrabbing){bnTriggerExec.Enabled = true;}}if (!cbSoftTrigger.Checked){device.Parameters.SetEnumValueByString("TriggerSource", "Line0");bnTriggerExec.Enabled = false;}}private void Form1_FormClosing(object sender, FormClosingEventArgs e){bnClose_Click(null, null);//反初始化SDKSystem.Finalize();}private void bnContinuesMode_CheckedChanged(object sender, EventArgs e){if (bnContinuesMode.Checked){device.Parameters.SetEnumValueByString("TriggerMode", "Off");cbSoftTrigger.Enabled = false;bnTriggerExec.Enabled = false;}}private void bnTriggerMode_CheckedChanged(object sender, EventArgs e){if (bnTriggerMode.Checked){//打开触发模式device.Parameters.SetEnumValueByString("TriggerMode", "On");if ((bool)cbSoftTrigger.Checked){device.Parameters.SetEnumValueByString("TriggerSource", "Software");if (isGrabbing){bnTriggerExec.Enabled = true;}}else{device.Parameters.SetEnumValueByString("TriggerSource", "Line0");}cbSoftTrigger.Enabled = true;cbSoftTrigger.Checked = true;}}public void ReciveThreadProcess(){IFrameOut frameOut = null;int result = MvError.MV_OK;while (isGrabbing){result = device.StreamGrabber.GetImageBuffer(1000, out frameOut);if (result == MvError.MV_OK){device.ImageRender.DisplayOneFrame(pictureBoxHandle, frameOut.Image);device.StreamGrabber.FreeImageBuffer(frameOut);}}}public void ReciveThreadImage(){IFrameOut frameOut = null;int result = MvError.MV_OK;int windowWidth = hWindowControl1.Width;int windowHeight = hWindowControl1.Height;double windowAspect = (double)windowWidth / windowHeight;while (isGrabbing){result = device.StreamGrabber.GetImageBuffer(1000, out frameOut);if (result == MvError.MV_OK){IImage image = frameOut.Image;int width = (int)image.Width;int height = (int)image.Height;double imageAspect = (double)width / height;int row1, col1, row2, col2;if (imageAspect > windowAspect){col1 = 0;col2 = width - 1;int newHeight = (int)(width / windowAspect);row1 = (width - newHeight) / 2;row2 = row1 + newHeight - 1;}else{row1 = 0;row2 = height - 1;int newWidth = (int)(height * windowAspect);col1 = (width - newWidth)/2;col2 = col1 + newWidth - 1;}hWindowControl1.HalconWindow.SetPart(row1, col1, row2, col2);IntPtr imagePtr = image.PixelDataPtr;Console.WriteLine(image.PixelType);if (image.PixelType == MvGvspPixelType.PixelType_Gvsp_BayerBG8){HImage hImage = new HImage("byte",width,height,imagePtr);HObject rgbImage;HOperatorSet.CfaToRgb(hImage, out rgbImage, "bayer_bg", "bilinear");HOperatorSet.DispObj(rgbImage, hWindowControl1.HalconWindow);hImage.Dispose();rgbImage.Dispose();}}}}private void bnStartGrab_Click(object sender, EventArgs e){try{isGrabbing = true;pictureBoxHandle = displayArea.Handle;receiveThread = new Thread(ReciveThreadProcess);receiveThread.Start();receiveImage = new Thread(ReciveThreadImage);receiveImage.Start();}catch (Exception ex){MessageBox.Show("Start thread failed !" + ex.Message);return;}//开始采集int result = device.StreamGrabber.StartGrabbing();if (result != MvError.MV_OK){isGrabbing = false;receiveThread.Join();ShowErrorMsg("Start Grabbing Fail !", result);return;}SetCtrlWhenStartGrab();}private void SetCtrlWhenStartGrab(){bnStartGrab.Enabled = false;bnStopGrab.Enabled = true;if ((bool)bnTriggerMode.Checked){cbSoftTrigger.Enabled = true;if ((bool)cbSoftTrigger.Checked){bnTriggerExec.Enabled = true;}}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/56929.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【算法练习】最小生成树

题意&#xff1a;【模板】最小生成树 方法1&#xff1a;Prim算法(稠密边用优&#xff09; #include <bits/stdc.h> using namespace std; int n,m,u,v,d,ans; bool f[5001]; vector<pair<int,int>> a[5001];//用结构体和重载比直接定义小根堆似乎还快一点点…

利用 Google AI 工具提升应用智能化:ML Kit、TensorFlowLite、Cloud Vision、AutoML、Gemini

在code应用开发中&#xff0c;机器学习和人工智能正逐渐成为提升用户体验和应用智能化的重要手段。Google 提供了多种强大的 AI 工具&#xff0c;可以帮助开发者快速集成各种机器学习功能。本文将详细介绍五个关键工具&#xff1a; Firebase ML Kit、TensorFlow Lite、Google …

kafka 的高可用机制是什么?

大家好&#xff0c;我是锋哥。今天分享关于【kafka 的高可用机制是什么&#xff1f;】面试题&#xff1f;希望对大家有帮助&#xff1b; kafka 的高可用机制是什么&#xff1f; 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 Apache Kafka 是一个分布式消息系统&am…

Peach-9B-8k-Roleplay模型部署指南

一、模型介绍 Peach-9B-8k-Roleplay 是一种聊天大型语言模型&#xff0c;它是通过我们的数据合成方法创建的超过 100K 的对话中微调 01-ai/Yi-1.5-9B 模型而获得的。 也许是 34B 以下参数最好的 LLM。 二、部署过程 1、更新环境 apt update2、安装Miniconda wget https://…

标准日志插件项目【C/C++】

博客主页&#xff1a;花果山~程序猿-CSDN博客 文章分栏&#xff1a;项目日记_花果山~程序猿的博客-CSDN博客 关注我一起学习&#xff0c;一起进步&#xff0c;一起探索编程的无限可能吧&#xff01;让我们一起努力&#xff0c;一起成长&#xff01; 目录 一&#xff0c;项目介…

二、Spring的执行流程

文章目录 1. spring的初始化过程1.1 ClassPathXmlApplicationContext的构造方法1.2 refresh方法&#xff08;核心流程&#xff09;1.2.1 prepareRefresh() 方法1.2.2 obtainFreshBeanFactory() 方法1.2.3 prepareBeanFactory() 方法1.2.4 invokeBeanFactoryPostProcessors() 方…

2024年9月电子学会Scratch图形化编程等级考试三级真题试卷

2024年9月Scratch图形化编程等级考试三级真题试卷 一、选择题 第 1 题 单选题 scratch运行下列程序后&#xff0c;变量“和”的取值范围是&#xff1f;&#xff08; &#xff09; A.0~12 B.0~11 C.2~11 D.2~12 第 2 题 单选题 默认角色小猫&#xff0c;scratch运行下列…

深度学习:Matplotlib篇

一、简介 1.1 什么是 Matplotlib&#xff1f; Matplotlib 是一个广泛使用的 2D 绘图库&#xff0c;它可以用来在 Python 中创建各种静态、动态和交互式的图表。无论是科学计算、数据可视化&#xff0c;还是深度学习模型的训练与评估&#xff0c;Matplotlib 都能提供强大的图形…

【Python】if选择判断结构详解:逻辑分支与条件判断

目录 &#x1f354; if选择判断结构作用 1.1 if选择判断结构的基本语法 1.2 if选择结构案例 1.3 if...else...结构 1.4 if...elif...else多条件判断结构 1.5 if嵌套结构 &#x1f354; 综合案例&#xff1a;石头剪刀布 2.1 需求分析 2.2 代码实现 2.3 随机出拳 &…

【数据结构】快速排序(三种实现方式)

目录 一、基本思想 二、动图演示&#xff08;hoare版&#xff09; 三、思路分析&#xff08;图文&#xff09; 四、代码实现&#xff08;hoare版&#xff09; 五、易错提醒 六、相遇场景分析 6.1 ❥ 相遇位置一定比key要小的原因 6.2 ❥ 右边为key&#xff0c;左边先走 …

redis详细教程(2.List教程)

List是一种可以存储多个有序字符串的数据类型&#xff0c;其中的元素按照顺序排列&#xff08;可以重复出现&#xff09;&#xff0c;可以通过数字索引来访问列表中的元素&#xff0c;索引可以从左到右或者从右到左。 Redis 列表可以通过两种方式实现&#xff1a;压缩列表&…

PVE 一键安装WIKI.js

Wiki.js 一个轻量的知识库管理工具 在PVE 的shell 下执行如下代码&#xff08;国内访问需自行调整&#xff09;&#xff0c;一键安装&#xff0c;默认使用了sqlLite 作为数据库&#xff1a; bash -c "$(wget -qLO - https://github.com/tteck/Proxmox/raw/main/ct/wikijs…

电脑维修指南

1.输入法切换 1.右键悬浮窗 2.选择全拼 2.换壁纸 壁纸给你准备好了 https://wwyz.lanzoul.com/b00g2g2vyd 密码:da72浏览器下载解压, 然后就有了 右键, 挑选 3.清理垃圾 浏览器输入这个地址 https://wwyz.lanzoul.com/ijMin2di41ih普通下载 找一个喜欢的地方 右键, 解压 …

[SWPUCTF 2022 新生赛]py1的write up

开启靶场&#xff0c;下载附件&#xff0c;解压后得到&#xff1a; 双击exe文件&#xff0c;出现弹窗&#xff1a; 问的是异或&#xff0c;写个python文件来计算结果&#xff1a; # 获取用户输入的两个整数 num1 int(input("Enter the first number: ")) num2 int…

排序算法(冒泡,插入),希尔排序(插入升级),希尔排序和插入排序时间比较!

&#x1f381;个人主页&#xff1a;我们的五年 &#x1f50d;系列专栏&#xff1a;排序算法 &#x1f389;欢迎大家点赞&#x1f44d;评论&#x1f4dd;收藏⭐文章 一.冒泡排序&#xff1a; 时间复杂度&#xff1a;O&#xff08;N^2&#xff09;。 &#x1f3c4;‍♂️思路…

【Nas】X-DOC:搞机之PVE部署All In One(黑群晖NAS 软路由OpenWrt Docker Win10远程桌面)

【Nas】X-DOC&#xff1a;搞机之PVE部署All In One&#xff08;黑群晖NAS & 软路由OpenWrt & Docker & Win10远程桌面&#xff09; 1、原硬件配置清单&#xff1a;2、改AIO后增加配置清单&#xff1a;3、虚拟化平台PVE&#xff1a;4、搭建的关键服务&#xff1a; 1…

Web高级开发实验:EL基本运算符与数据访问

一、实验目的 掌握EL的定义&#xff0c;即Expression Language&#xff0c;用于提高编程效率。学习和掌握在开发环境中创建Java文件&#xff0c;并在jsp文件中使用EL表达式去调用其中的方法与属性等。 二、实验所用方法 上机实操 三、实验步骤及截图 1、创建javaweb项目&a…

Springboot项目中常用注解

文章目录 Springboot相关注解EnableAspectJAutoProxy(exposeProxy true)内部实现机制 EnableTransactionManagementServletComponentScanMapperScan(basePackages {"com.xxx.mapper"})ComponentScan(basePackages{"*"})lombok Data注解Controller中的相关…

jvm虚拟机介绍

Java虚拟机&#xff08;JVM&#xff09;是Java语言的运行环境&#xff0c;它基于栈式架构&#xff0c;通过加载、验证、准备、解析、初始化等类加载过程&#xff0c;将Java类文件转换成平台无关的字节码&#xff0c;并在运行时动态地将其翻译成特定平台的机器码执行。 JVM的核心…

基于SSM农业信息管理系统的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;用户管理&#xff0c;农业技术管理&#xff0c;种植户管理&#xff0c;农产品类型管理&#xff0c;农资订单管理&#xff0c;系统管理 种植户账号功能包括&#xff1a;系统首页&#xff0c;个人中心&a…