
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>();
//枚举同一网段下的相机 无ALL
IGXFactory.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//黑白
{
//如果是黑白相机,直接转为bmp
Bitmap 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//黑白
{
//如果是黑白相机,直接转为bmp
Bitmap 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;
}
}
}
打开相机 连续采集