当前位置: 首页 > news >正文

【05】大恒相机SDK C#开发 —— Winform中采集图像并显示

文章目录

  • 1 程序过程出现的问题及注意事项
  • 2 防呆 措施
  • 3 完整代码
  • 4 视频教程

在这里插入图片描述

1 程序过程出现的问题及注意事项

回调函数 只能使用 静态的全局变量,
bool colorCam = false;是Form1的非静态变量
在这里插入图片描述

pictureBox 也是非静态变量 ,报错,

在这里插入图片描述
回调函数只能使用静态变量,pictureBox1是非静态变量,可以用用户自定义变量 将PictureBox传进来

//回调函数只能使用静态变量,pictureBox1是非静态变量,可以用用户自定义变量 将PictureBox传进来
PictureBox pictureBox = userParam as PictureBox;

在这里插入图片描述
//显示图像

//由于回调函数相当于开了一个线程,但pictureBox主界面中的对象;
//因此需要使用托管的方式对picturebox进行操作

//显示图像//由于回调函数相当于开了一个线程,但pictureBox主界面中的对象;
//因此需要使用托管的方式对picturebox进行操作
Action action = () =>
{pictureBox.Image = bitmap;
};
pictureBox.BeginInvoke(action);

2 防呆 措施

//打开相机之前,其他操作系统都会报错;各个按钮的Enable 属性默认设为flase;

//打开相机后,除了打开按钮外,其余按钮均为可用状态;

            //打开相机后,相应操作按钮,变为可用状态;btOpenCam.Enabled = false;btCloseCam.Enabled = true;btGrabContinue.Enabled = true;btGrabOnce.Enabled = true;btGrabStop.Enabled = true;

//关闭相机后除了打开相机外,其余按钮均不可用

            //关闭相机后除了打开相机外,其余按钮均不可用btOpenCam.Enabled = true;btCloseCam.Enabled = false;btGrabContinue.Enabled = false;btGrabOnce.Enabled = false;btGrabStop.Enabled = false;

//单帧采集时,禁用连续采集;停止采集后才可以启用其他采集;

            //单帧采集时,禁用连续采集;停止采集后才可以启用其他采集;btGrabContinue.Enabled = false;btGrabOnce.Enabled = false;btGrabStop.Enabled = true;

//连续采集时,禁用单帧采集;停止采集后才可以启用其他采集;

			//连续采集时,禁用单帧采集;停止采集后才可以启用其他采集;btGrabContinue.Enabled = false;btGrabOnce.Enabled = false;btGrabStop.Enabled = true;

//停止采集后可单帧采集、连续采集、关闭相机操作

            //停止采集后可单帧采集、连续采集、关闭相机操作btGrabOnce.Enabled = true;btGrabContinue.Enabled = true;btCloseCam.Enabled = true;btOpenCam.Enabled = false;

3 完整代码

using GxIAPINET;
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;namespace GrabAndShowImg
{public partial class Form1 : Form{IGXStream gCam_stream;//通道流IGXFeatureControl cam_remote_control;//远端控制器IGXDevice cam;bool colorCam = false;//是否彩色相机static bool colorFlag = false;// 回调函数的 静态变量,是否彩色相机public Form1(){InitializeComponent();}//打开相机private void btOpenCam_Click(object sender, EventArgs e){//首先,找到相机//第一步,对相机资源进行初始化IGXFactory.GetInstance().Init();//第二部,枚举相机//先定义一个设备列表,用来存放枚举到的设备List<IGXDeviceInfo> iGXDeviceInfos = new List<IGXDeviceInfo>();//枚举同一网段下的相机 无ALLIGXFactory.GetInstance().UpdateDeviceList(1000, iGXDeviceInfos);//枚举同一网络下所有相机 有ALL//IGXFactory.GetInstance().UpdateAllDeviceList(1000, iGXDeviceInfos);//第三步,获取相机信息,如IP、ID、SN等//枚举到相机后,就可以获取到相机的一些设备信息string IP = iGXDeviceInfos[0].GetIP();string ID = iGXDeviceInfos[0].GetDeviceID();string SN = iGXDeviceInfos[0].GetSN();listBox1.Items.Add("第一个设备的IP:" + IP);listBox1.Items.Add("第一个设备的ID:" + ID);listBox1.Items.Add("第一个设备的SN:" + SN);//第四步,打开相机,可以通过IP、SN、MAC、ID等唯一标识符打开相机//这里通过SN打开相机,打开相机的方式有三种: 只读、控制、独占cam = IGXFactory.GetInstance().OpenDeviceBySN(SN, GX_ACCESS_MODE.GX_ACCESS_CONTROL);//控制方式打开//判读是否彩色相机__IsSupportColor(ref colorCam,cam);__IsSupportColor(ref colorFlag, cam);//获取远端属性控制器cam_remote_control = cam.GetRemoteFeatureControl();//第五步,打开相机后,准备开始采集图像//首先打开流通道//uint cam_num = cam.GetStreamCount();gCam_stream = cam.OpenStream(0);//默认打开第一个流通道   //防呆//打开相机之前,其他操作系统都会报错;各个按钮的Enable 属性默认设为flase;//打开相机后,相应操作按钮,变为可用状态;btOpenCam.Enabled = false;btCloseCam.Enabled = true;btGrabContinue.Enabled = true;btGrabOnce.Enabled = true;btGrabStop.Enabled = true;}private void btGrabOnce_Click(object sender, EventArgs e){//流通道开始采集gCam_stream.StartGrab();//发送开采命令,设备参数字符串可以去文档里查看cam_remote_control.GetCommandFeature("AcquisitionStart").Execute();//采集一帧图像IImageData imgData = gCam_stream.GetImage(1000);//采集超时时间ms//获取并打印图像宽高int imgHeight = (int)imgData.GetHeight();int imgWidth  = (int)imgData.GetWidth();//将图像显示在 PictureBox中//判断是黑板还是彩色相机if (colorCam)//彩色{   listBox1.Items.Add("图像高:" + imgHeight.ToString() + "宽 :" + imgWidth.ToString());//获取图像buffer IntPtr buffer = imgData.ConvertToRGB24(GX_VALID_BIT_LIST.GX_BIT_0_7,GX_BAYER_CONVERT_TYPE_LIST.GX_RAW2RGB_NEIGHBOUR,false);//将图像转成BMP格式Bitmap bitmap = new Bitmap(imgWidth,imgHeight,imgWidth *3,System.Drawing.Imaging.PixelFormat.Format24bppRgb,buffer);//显示图像pictureBox1.Image = bitmap;        }else//黑白{//如果是黑白相机,直接转为bmpBitmap bitmap = new Bitmap(imgWidth, imgHeight, imgWidth, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, imgData.GetBuffer());//需要使用调色盘进行像素翻转System.Drawing.Imaging.ColorPalette colorPalette = bitmap.Palette;for (int i = 0; i < 255; i++){colorPalette.Entries[i] = System.Drawing.Color.FromArgb(i,i,i);}bitmap.Palette = colorPalette;pictureBox1.Image = bitmap;}//单帧采集时,禁用连续采集;停止采集后才可以启用其他采集;btGrabContinue.Enabled = false;btGrabOnce.Enabled = false;btGrabStop.Enabled = true;}/// <param name="bIsColorFilter">是否支持彩色</param>private void __IsSupportColor(ref bool bIsColorFilter,IGXDevice cam){bool bIsImplemented = false;bool bIsMono = false;string strPixelFormat = "";strPixelFormat = cam.GetRemoteFeatureControl().GetEnumFeature("PixelFormat").GetValue();if (0 == string.Compare(strPixelFormat, 0, "Mono", 0, 4)){bIsMono = true;}else{bIsMono = false;}bIsImplemented = cam.GetRemoteFeatureControl().IsImplemented("PixelColorFilter");// 若当前为非黑白且支持PixelColorFilter则为彩色if ((!bIsMono) && (bIsImplemented)){bIsColorFilter = true;}else{bIsColorFilter = false;}}//采用回调函数的方式 连续采集private void btGrabContinue_Click(object sender, EventArgs e){//注册回调函数//RegisterCaptureCallback(,)自定义参数可以是任意参数,调第二个参数则为刚才创建的回调函数;gCam_stream.RegisterCaptureCallback(pictureBox1, _CallCaptureBack);//流通道开始采集gCam_stream.StartGrab();//发送开采命令,设备参数字符串可以去文档里查看cam_remote_control.GetCommandFeature("AcquisitionStart").Execute();//连续采集时,禁用单帧采集;停止采集后才可以启用其他采集;btGrabContinue.Enabled = false;btGrabOnce.Enabled = false;btGrabStop.Enabled = true;}//创建回调函数//其中回调函数中包含一个自定义变量和一个图像数据public static void _CallCaptureBack(object userParam, IFrameData imgData){PictureBox pictureBox = userParam as PictureBox;//回调函数只能使用静态变量,pictureBox1是非静态变量,可以用用户自定义变量 将PictureBox传进来//获取并打印图像宽高int imgHeight = (int)imgData.GetHeight();int imgWidth =  (int)imgData.GetWidth();//将图像显示在 PictureBox中//判断是黑板还是彩色相机if (colorFlag)//彩色{//获取图像buffer IntPtr buffer = imgData.ConvertToRGB24(GX_VALID_BIT_LIST.GX_BIT_0_7, GX_BAYER_CONVERT_TYPE_LIST.GX_RAW2RGB_NEIGHBOUR, false);//将图像转成BMP格式Bitmap bitmap = new Bitmap(imgWidth, imgHeight, imgWidth * 3, System.Drawing.Imaging.PixelFormat.Format24bppRgb, buffer);//显示图像//由于回调函数相当于开了一个线程,但pictureBox主界面中的对象;//因此需要使用托管的方式对picturebox进行操作Action action = () =>{pictureBox.Image = bitmap;};pictureBox.BeginInvoke(action);}else//黑白{//如果是黑白相机,直接转为bmpBitmap bitmap = new Bitmap(imgWidth, imgHeight, imgWidth, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, imgData.GetBuffer());//需要使用调色盘进行像素翻转System.Drawing.Imaging.ColorPalette colorPalette = bitmap.Palette;for (int i = 0; i < 255; i++){colorPalette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);}bitmap.Palette = colorPalette;//显示图像//由于回调函数相当于开了一个线程,但pictureBox主界面中的对象;//因此需要使用托管的方式对picturebox进行操作Action action = () =>{pictureBox.Image = bitmap;};pictureBox.BeginInvoke(action);}}private void btCloseCam_Click(object sender, EventArgs e){//停止采集和关闭相机//关闭相机首先需要先停止流采集;再注销前面所注册的所有回调函数;//关闭采集命令cam_remote_control.GetCommandFeature("AcquisitionStop").Execute();//关闭流通道gCam_stream.StopGrab();//关闭回调函数gCam_stream.UnregisterCaptureCallback();//关闭相机cam.Close();//最后通过反初始化释放所有资源;IGXFactory.GetInstance().Uninit();//关闭相机后除了打开相机外,其余按钮均不可用btOpenCam.Enabled = true;btCloseCam.Enabled = false;btGrabContinue.Enabled = false;btGrabOnce.Enabled = false;btGrabStop.Enabled = false;}private void btGrabStop_Click(object sender, EventArgs e){//停止采集后,可选择 单帧采集、连续采集等操作//关闭流通道gCam_stream.StopGrab();//关闭采集命令cam_remote_control.GetCommandFeature("AcquisitionStop").Execute();btGrabOnce.Enabled = true;btGrabContinue.Enabled = true;}}
}

在这里插入图片描述
打开相机 连续采集
在这里插入图片描述

4 视频教程

【大恒工业相机SDK开发C#版】Winform中采集图像并显示

http://www.lryc.cn/news/606342.html

相关文章:

  • 提示词增强工程(Prompt Enhancement Engineering)白皮书草稿
  • 【大模型理论篇】混合思考之自适应思维链
  • uv使用教程
  • FastMCP本地构建Server和Clinet交互
  • 用Python绘制SM2国密算法椭圆曲线:一场数学与视觉的盛宴
  • 时间戳 + 签名机制
  • 学习日志23 python
  • 因为想开发新项目了~~要给老Python项目整个虚拟环境
  • HTML基础复习:全面回顾核心概念
  • 谷歌V3插件热更新
  • 【0基础PS】Photoshop (PS) 理论知识
  • 【刷题】东方博宜oj 1412-快速幂(零基础,简单易懂)
  • Mysql-视图,函数,存储过程,触发器
  • 【Kiro Code】Chat 聊天功能
  • 某讯视频风控参数逆向分析
  • Docker部署的PostgreSQL慢查询日志配置指南
  • pytorch的自定义 CUDA 扩展怎么学习
  • pytorch程序语句固定开销分析
  • 排序算法-选择排序(选择排序、堆排序)(动图演示)
  • Next实习项目总结串联讲解(一)
  • 基于京东评论的文本挖掘与分析,使用LSTM情感分析算法以及网络语义分析
  • 正则化都是放在模型的哪个位置呢?
  • 案例开发 - 日程管理 - 第四期
  • 【C语言学习】scanf函数
  • 【源力觉醒 创作者计划】文心一言与deepseek集成springboot开发哪个更方便
  • 3.Linux 系统文件类型与文件权限
  • AI与AGI:从狭义智能到通用智能
  • 上证50期权2400是什么意思?
  • 性能测试篇 :Jmeter监控服务器性能
  • virtualbox+UBuntu20.04+内存磁盘扩容