大恒工业相机C#语言winform平台开发例程

大恒工业相机winform快速开发

  • 例程一、连续采集,返回bitmap格式图像
  • 例程二、软触发采集,返回bitmap格式图像
  • 例程三、硬触发采集,返回bitmap格式图像
  • 例程四、作为TCP客户端接收字符串“1”触发采集

链接:pan.baidu.com/s/1xq-xNpY6rWxpIddX345PYw?
提取码:dhxa
本例程大恒水星系列(MER、MER2、ME2P),火星(MARS)均可适用

大恒工业相机C#语言winform平台开发例程_第1张图片

这四个例程为自己二次分装,非自带例程,而且非常简洁
采集到的图像返回为bitmap格式图像,显示在picturebox中

首先新建一个winform项目,调用Galaxy Viewer中的C#dll
大恒工业相机C#语言winform平台开发例程_第2张图片
再进行引用

using GxIAPINET;

例程一、连续采集,返回bitmap格式图像

大恒工业相机C#语言winform平台开发例程_第3张图片
自由采集例程直接调用本例程中的DHcamera文件,改为自己项目的命名空间,可以直接调用大恒工业相机C#语言winform平台开发例程_第4张图片

        int m_CamNum = 0;
        DhCamera m_Cam = new DhCamera();                           //声明一个相机类对象数组
        private void Open_Click(object sender, EventArgs e)
        {
            if(m_CamNum>0)
            if (!m_Cam.CamOpen())
                m_Cam.OpenCameraFunc(0, ShowImage);                      
        }
        private void Close_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.CloseCameraFunc();                        
        }

        private void Start_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.StartAcqFunc();
        }
        private void Stop_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.StopAcqFunc();
        }
        public void ShowImage(int index, Bitmap objdata)
        {
            if (!m_Cam.m_bIsColor)
            {
                //添加调色板
                ColorPalette palette;
                palette = objdata.Palette;
                int i = 0;
                for (i = 0; i <= 255; i++)
                {
                    palette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
                }
                objdata.Palette = palette;

            }
            //BitmapImage objdataImage = BitmapToBitmapImage(objdata);       
            Action action1 = () =>
            {
                pictureBox1.Image = objdata;
            };
            pictureBox1.BeginInvoke(action1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DhCamera.initlib();
            m_CamNum = DhCamera.GetCamNumFunc(); //获取识别到的相机数量
            if (m_CamNum > 0)
                textBox1.Text = m_Cam.GetInfo(0); //第一个相机的id
            else
                textBox1.Text = "未识别到相机";
        }

例程二、软触发采集,返回bitmap格式图像

大恒工业相机C#语言winform平台开发例程_第5张图片
同样直接调用本例程中的DHcamera文件,改为自己项目的命名空间,可以直接调用

		int m_CamNum = 0;
        DhCamera m_Cam = new DhCamera();                           //声明一个相机类对象数组
        public Form1()
        {
            InitializeComponent();
        }
        private void Open_Click(object sender, EventArgs e)
        {
            if (m_CamNum > 0)
                if (!m_Cam.CamOpen())
                    m_Cam.OpenCameraFunc(0, ShowImage);

        }
        private void Close_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.CloseCameraFunc();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.StartAcqFunc();
        }
        private void Stop_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.StopAcqFunc();
        }
        public void ShowImage(int index, Bitmap objdata)
        {
            if (!m_Cam.m_bIsColor)
            {
                //添加调色板
                ColorPalette palette;
                palette = objdata.Palette;
                int i = 0;
                for (i = 0; i <= 255; i++)
                {
                    palette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
                }
                objdata.Palette = palette;

            }
            //BitmapImage objdataImage = BitmapToBitmapImage(objdata);       
            Action action1 = () =>
            {
                pictureBox1.Image = objdata;
            };
            pictureBox1.BeginInvoke(action1);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            DhCamera.initlib();
            m_CamNum = DhCamera.GetCamNumFunc(); //获取识别到的相机数量
            if (m_CamNum > 0)
                textBox1.Text = m_Cam.GetInfo(0); //第一个相机的id
            else
                textBox1.Text = "未识别到相机";
        }
        private void Trigger_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.SoftTriggerCommand();
        }

例程三、硬触发采集,返回bitmap格式图像

同样直接调用本例程中的DHcamera文件,改为自己项目的命名空间,可以直接调用
需要注意的是,大恒水星系列相机和火星系列相机可以作为触发的IO有3组,分别是line0、line2、line3
如果想更改触发接口需要更改DHcamera.cs文件中的146行参数,将line0改为line2或者line3。
如果想通过相机输出GPIO信号,参考这个大恒水星火星工业相机GPIO输入输出

//打开触发模式
 m_objIGXFeatureControl.GetEnumFeature("TriggerMode").SetValue("On");
  //也可以使用Line2,Line3
 m_objIGXFeatureControl.GetEnumFeature("TriggerSource").SetValue("Line0");
 		int m_CamNum = 0;
        DhCamera m_Cam = new DhCamera();                           //声明一个相机类对象数组
        private void Open_Click(object sender, EventArgs e)
        {
            if(m_CamNum>0)
            if (!m_Cam.CamOpen())
                m_Cam.OpenCameraFunc(0, ShowImage);                     
        }
        private void Close_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.CloseCameraFunc();                        
        }

        private void Start_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.StartAcqFunc();
        }
        private void Stop_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.StopAcqFunc();
        }
        public void ShowImage(int index, Bitmap objdata)
        {
            if (!m_Cam.m_bIsColor)
            {
                //添加调色板
                ColorPalette palette;
                palette = objdata.Palette;
                int i = 0;
                for (i = 0; i <= 255; i++)
                {
                    palette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
                }
                objdata.Palette = palette;

            }
            //BitmapImage objdataImage = BitmapToBitmapImage(objdata);       
            Action action1 = () =>
            {
                pictureBox1.Image = objdata;
            };
            pictureBox1.BeginInvoke(action1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DhCamera.initlib();
            m_CamNum = DhCamera.GetCamNumFunc(); //获取识别到的相机数量
            if (m_CamNum > 0)
                textBox1.Text = m_Cam.GetInfo(0); //第一个相机的id
            else
                textBox1.Text = "未识别到相机";
        }

例程四、作为TCP客户端接收字符串“1”触发采集

大恒工业相机C#语言winform平台开发例程_第6张图片

字符串可以根据自己需要的协议进行更改

 		private void SetText(string text)
        {
            if(text == "1")//根据需要将“1”改为其他接收的字符串
                if (m_Cam.CamOpen())
                    m_Cam.SoftTriggerCommand();
        }
		IPAddress HostIP;
        IPEndPoint point;
        Socket socket;
        bool flag = true;
        string IP = "192.168.1.105";
        string IPort = "50123";
        int i;

        int m_CamNum = 0;
        DhCamera m_Cam = new DhCamera();                           //声明一个相机类对象数组
        private void Open_Click(object sender, EventArgs e)
        {
            if (m_CamNum > 0)
                if (!m_Cam.CamOpen())
                    m_Cam.OpenCameraFunc(0, ShowImage);
        }
        private void Close_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.CloseCameraFunc();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.StartAcqFunc();
        }
        private void Stop_Click(object sender, EventArgs e)
        {
            if (m_Cam.CamOpen())
                m_Cam.StopAcqFunc();
        }
        public void ShowImage(int index, Bitmap objdata)
        {
            if (!m_Cam.m_bIsColor)
            {
                //添加调色板
                ColorPalette palette;
                palette = objdata.Palette;
                int i = 0;
                for (i = 0; i <= 255; i++)
                {
                    palette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
                }
                objdata.Palette = palette;
            }
            //BitmapImage objdataImage = BitmapToBitmapImage(objdata);       
            Action action1 = () =>
            {
                pictureBox1.Image = objdata;
            };
            pictureBox1.BeginInvoke(action1);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            DhCamera.initlib();
            m_CamNum = DhCamera.GetCamNumFunc(); //获取识别到的相机数量
            if (m_CamNum > 0)
                textBox1.Text = m_Cam.GetInfo(0); //第一个相机的id
            else
                textBox1.Text = "未识别到相机";
        }
        private void Trigger_Click(object sender, EventArgs e)
        {
            try
            {
                HostIP = IPAddress.Parse(IP);              //获取Ip
                point = new IPEndPoint(HostIP, Int32.Parse(IPort));   //将网络端点表示为IP地址和端口号
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //使用Socket的构造函数实例化一个Socket对象,以遍连接远程主机
                socket.Connect(point);   //调用Connect方法连接远程主机
                Thread thread = new Thread(new ThreadStart(Proccess));
                thread.Start();
            }
            catch
            {      
            }
        }
        private void SetText(string text)
        {
            if(text == "1")
                if (m_Cam.CamOpen())
                    m_Cam.SoftTriggerCommand();
        }
        private void Proccess()
        {
            if (socket.Connected)
            {
                while (flag)
                {
                    byte[] receiveByte = new byte[64];
                    int r = socket.Receive(receiveByte, receiveByte.Length, 0);
                    string strInfo = Encoding.UTF8.GetString(receiveByte,0,r);
                    this.Invoke(new SetTextCallback(SetText), new object[] { strInfo });
                }
            }
        }
        delegate void SetTextCallback(string text);

你可能感兴趣的:(大恒工业相机,c#,开发语言)