C#调用度申工业相机实现双摄像头显示源码

By admin at 2023-02-01 • 0人收藏 • 597人看过

image.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DVPCameraType;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;


namespace MultipleCamera
{
    public partial class MultipleCamera : Form
    {
        public int m_n_dev_count_0;
        uint m_handle_0 = 0;

        public int m_n_dev_count_1;
        uint m_handle_1 = 0;

        public static IntPtr m_ptr_wnd0 = new IntPtr();
        public static IntPtr m_ptr0 = new IntPtr();
        public static IntPtr m_ptr_wnd1 = new IntPtr();
        public static IntPtr m_ptr1 = new IntPtr();

        dvpCameraInfo[] m_info0 = new dvpCameraInfo[16];
        dvpCameraInfo[] m_info1 = new dvpCameraInfo[16];


        // Display param
        public static Stopwatch m_Stopwatch0 = new Stopwatch();
        public static Stopwatch m_Stopwatch1 = new Stopwatch();

        public static Double[] m_dfDisplayCount = new Double[4];


        public MultipleCamera()
        {
            Form.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();

            m_ptr_wnd0 = pictureBox.Handle;
            m_ptr_wnd1 = pictureBox1.Handle;

            InitDevList_0();
            InitDevList_1();
        }


        public void InitDevList_0()
        {
            dvpStatus status;
            uint i, n = 0;

            // "n" represents the number of cameras that is enumerated successfully, the drop-down list contains each camera's FriendlyName.
            DevNameCombo.Items.Clear();

            // Get the number of cameras that has been connected to a computer.
            status = DVPCamera.dvpRefresh(ref n);
            Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
            m_n_dev_count_0 = (int)n;
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                if (n > 16)
                    n = 16;

                for (i = 0; i < n; i++)
                {
                    // Acquire each camera's information one by one.
                    status = DVPCamera.dvpEnum(i, ref m_info0[i]);
                    Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                    if (status == dvpStatus.DVP_STATUS_OK)
                    {
                        // GUI need UNICODE,but the information acquired from camera is ANSI,so convert the character set from ANSI to UNICODE.
                        int item = DevNameCombo.Items.Add(m_info0[i].FriendlyName);
                    }
                }
                if (n > 0)
                {
                    DevNameCombo.SelectedIndex = 0;
                }
            }

            if (n == 0)
            {
                OpenDev.Enabled = false;
            }
            else
            {
                OpenDev.Enabled = true;
            }

            InitCtrls_0();
        }

        public void InitDevList_1()
        {
            dvpStatus status;
            uint i, n = 0;

            // "n" represents the number of cameras that is enumerated successfully,the drop-down list contains each camera's FriendlyName.
            DevNameCombo1.Items.Clear();

            // Get the number of cameras that has been connected to a computer.
            status = DVPCamera.dvpRefresh(ref n);
            m_n_dev_count_1 = (int)n;
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                if (n > 16)
                    n = 16;
                for (i = 0; i < n; i++)
                {
                    // Acquire each camera's information one by one.
                    status = DVPCamera.dvpEnum(i, ref m_info1[i]);
                    Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                    if (status == dvpStatus.DVP_STATUS_OK)
                    {
                        // GUI need UNICODE,but the information acquired from camera is ANSI,so convert the character set from ANSI to UNICODE.
                        int item = DevNameCombo1.Items.Add(m_info1[i].FriendlyName);
                    }
                }
                if (n > 1)
                {
                    DevNameCombo1.SelectedIndex = 1;
                }
            }

            if (n == 0)
            {
                OpenDev1.Enabled = false;
            }
            else
            {
                OpenDev1.Enabled = true;
            }

            InitCtrls_1();
        }

  
        public bool IsValidHandle(uint handle)
        {
            bool bValidHandle = false;
            dvpStatus status = DVPCamera.dvpIsValid(handle, ref bValidHandle);
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                return bValidHandle;
            }

            return false;
        }

        // Initialize the first camera.
        public void InitCtrls_0()
        {
            dvpStatus status;
            if (IsValidHandle(m_handle_0))
            {
                // The device has been opened at this time.
                dvpCameraInfo info = new dvpCameraInfo();
                dvpStreamState state = new dvpStreamState();

                status = DVPCamera.dvpGetStreamState(m_handle_0, ref state);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                // Set the basic controls.
                OpenDev.Text = "Close";
                StartPlay.Text = state == dvpStreamState.STATE_STARTED ? ("Stop") : ("Start");
                StartPlay.Enabled = true;

                // Set the enable status of the related controls.
                PropertSet.Enabled = true;


                // Get the camera's information.
                status = DVPCamera.dvpGetCameraInfo(m_handle_0, ref info);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

            }
            else
            {
                // No device is opened at this time.
                // Set the basic controls.
                OpenDev.Text = "Open";
                StartPlay.Text = "Start";
                StartPlay.Enabled = false;

                // Set the enable status of the related controls.
                PropertSet.Enabled = false;
            }
        }

        public void InitCtrls_1()
        {
            dvpStatus status;
            if (IsValidHandle(m_handle_1))
            {
                // The device has been opened at this time.
                dvpCameraInfo info = new dvpCameraInfo();
                dvpStreamState state = new dvpStreamState();
                status = DVPCamera.dvpGetStreamState(m_handle_1, ref state);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                // Set the basic controls.
                OpenDev1.Text = "Close";
                StartPlay1.Text = state == dvpStreamState.STATE_STARTED ? ("Stop") : ("Start");
                StartPlay1.Enabled = true;

                // Set the enable status of the related controls.
                PropertSet1.Enabled = true;

                // Get the camera's information.
                status = DVPCamera.dvpGetCameraInfo(m_handle_1, ref info);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
            }
            else
            {
                // No device is opened at this time.
                // Set the basic controls.
                OpenDev1.Text = "Open";
                StartPlay1.Text = "Start";
                StartPlay1.Enabled = false;

                // Set the enable status of the related controls.
                PropertSet1.Enabled = false;
            }
        }

        private void ScanDev_Click(object sender, EventArgs e)
        {
            InitDevList_0();
        }

        private void ScanDev1_Click(object sender, EventArgs e)
        {
            InitDevList_1();
        }


        private DVPCamera.dvpStreamCallback _proc0;
        private DVPCamera.dvpStreamCallback _proc1;

        // The callback function that used for receiving data.
        public static int _dvpStreamCallback0(/*dvpHandle*/uint handle, dvpStreamEvent _event, /*void **/IntPtr pContext, ref dvpFrame refFrame, /*void **/IntPtr pBuffer)
        {
            bool bDisplay = false;

            if (m_dfDisplayCount[0] == 0)
            {
                m_Stopwatch0.Restart();
                bDisplay = true;
            }
            else
            {
                if (m_Stopwatch0.ElapsedMilliseconds - (long)(m_dfDisplayCount[0] * 33.3f) >= 33)
                {
                    bDisplay = true;
                }
            }

            if (bDisplay)
            {
                m_dfDisplayCount[0]++;

                // It demonstrates the usual video drawing,and it is not recommended to take a longer time operation in the callback function,
                // in order to avoid affecting the frame rate and the real-time of acquiring images.
                // The acquired image data buffer is valid only before the function returns,so the buffer pointer should not be passed out, 
                // however, the user can malloc memory and copy image data.
                dvpStatus status = DVPCamera.dvpDrawPicture(ref refFrame, pBuffer,
                    m_ptr_wnd0, (IntPtr)0, (IntPtr)0);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
            }
            return 1;
        }
        public static int _dvpStreamCallback1(/*dvpHandle*/uint handle, dvpStreamEvent _event, /*void **/IntPtr pContext, ref dvpFrame refFrame, /*void **/IntPtr pBuffer)
        {
            bool bDisplay = false;

            if (m_dfDisplayCount[1] == 0)
            {
                m_Stopwatch1.Restart();
                bDisplay = true;
            }
            else
            {
                if (m_Stopwatch1.ElapsedMilliseconds - (long)(m_dfDisplayCount[1] * 33.3f) >= 33)
                {
                    bDisplay = true;
                }
            }

            if (bDisplay)
            {
                m_dfDisplayCount[1]++;

                // It demonstrates the usual video drawing,and it is not recommended to take a longer time operation in the callback function,
                // in order to avoid affecting the frame rate and the real-time of acquiring images.
                // The acquired image data buffer is valid only before the function returns,so the buffer pointer should not be passed out, 
                // however, the user can malloc memory and copy image data.
                dvpStatus status = DVPCamera.dvpDrawPicture(ref refFrame, pBuffer,
                    m_ptr_wnd1, (IntPtr)0, (IntPtr)0);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
            }

            return 1;
        }


1 个回复 | 最后更新于 2023-02-01
2023-02-01   #1

太长了, 分两截 ,后半部分代码:

        private void OpenDev_Click(object sender, EventArgs e)
        {
            dvpStatus status;
            string strName;
            if (!IsValidHandle(m_handle_0))
            {
                strName = DevNameCombo.Text;
                if (strName != "")
                {
                    // Open the pointed device by the selected FriendlyName.
                    status = DVPCamera.dvpOpenByName(strName, dvpOpenMode.OPEN_NORMAL, ref m_handle_0);
                    if (status != dvpStatus.DVP_STATUS_OK)
                    {
                        DevNameCombo.Enabled = true;
                        MessageBox.Show("Open the device failed!");
                    }
                    else
                    {
                        DevNameCombo.Enabled = false;

                        // If it needs to display images,the user should register a callback function and finish the operation of drawing pictures in the registered callback function.
                        // Note: Drawing pictures in the callback function maybe generate some delays for acquiring image data by the use of "dvpGetFrame".
                        _proc0 = _dvpStreamCallback0;
                        using (Process curProcess = Process.GetCurrentProcess())
                        using (ProcessModule curModule = curProcess.MainModule)
                        {
                            status = DVPCamera.dvpRegisterStreamCallback(m_handle_0, _proc0, dvpStreamEvent.STREAM_EVENT_PROCESSED, m_ptr0);
                            Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                        }
                    }
                }
            }
            else
            {
                // check camear
                dvpStreamState StreamState = new dvpStreamState();
                status = DVPCamera.dvpGetStreamState(m_handle_0, ref StreamState);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                if (StreamState == dvpStreamState.STATE_STARTED)
                {
                    // init display count is 0
                    m_dfDisplayCount[0] = 0;

                    // stop camera
                    status = DVPCamera.dvpStop(m_handle_0);
                    Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                }

                status = DVPCamera.dvpClose(m_handle_0);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                m_handle_0 = 0;
                DevNameCombo.Enabled = true;
                pictureBox.Refresh();
            }

            InitCtrls_0();
        }

        private void OpenDev1_Click(object sender, EventArgs e)
        {
            dvpStatus status;
            string strName;

            if (!IsValidHandle(m_handle_1))
            {
                strName = DevNameCombo1.Text;
                if (strName != "")
                {
                    // Open the pointed device by the selected FriendlyName.
                    status = DVPCamera.dvpOpenByName(strName, dvpOpenMode.OPEN_NORMAL, ref m_handle_1);
                    if (status != dvpStatus.DVP_STATUS_OK)
                    {
                        DevNameCombo1.Enabled = true;
                        MessageBox.Show("Open the device failed!");
                    }
                    else
                    {
                        DevNameCombo1.Enabled = false;

                        // If it needs to display images,the user should register a callback function and finish the operation of drawing pictures in the registered callback function.
                        // Note: Drawing pictures in the callback function maybe generate some delays for acquiring image data by the use of "dvpGetFrame".
                        _proc1 = _dvpStreamCallback1;
                        using (Process curProcess = Process.GetCurrentProcess())
                        using (ProcessModule curModule = curProcess.MainModule)
                        {
                            status = DVPCamera.dvpRegisterStreamCallback(m_handle_1, _proc1, dvpStreamEvent.STREAM_EVENT_PROCESSED, m_ptr1);
                            Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                        }
                    }
                }
            }

            else
            {
                // check camear
                dvpStreamState StreamState = new dvpStreamState();
                status = DVPCamera.dvpGetStreamState(m_handle_1, ref StreamState);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                if (StreamState == dvpStreamState.STATE_STARTED)
                {
                    // init display count is 0
                    m_dfDisplayCount[1] = 0;

                    // stop camera
                    status = DVPCamera.dvpStop(m_handle_1);
                    Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                }

                status = DVPCamera.dvpClose(m_handle_1);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                m_handle_1 = 0;
                DevNameCombo1.Enabled = true;
                pictureBox1.Refresh();
            }

            InitCtrls_1();
        }

        private void StartPlay_Click(object sender, EventArgs e)
        {
            // init display count is 0
            m_dfDisplayCount[0] = 0;

            if (IsValidHandle(m_handle_0))
            {
                dvpStreamState state = new dvpStreamState();
                dvpStatus status = new dvpStatus();

                // Implement a button to start and stop according to the current video's status.
                status = DVPCamera.dvpGetStreamState(m_handle_0, ref state);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                if (state == dvpStreamState.STATE_STARTED)
                {
                    status = DVPCamera.dvpStop(m_handle_0);
                }
                else
                {
                    status = DVPCamera.dvpStart(m_handle_0);
                }

                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                InitCtrls_0();
            }
        }

        private void StartPlay1_Click(object sender, EventArgs e)
        {
            // init display count is 0
            m_dfDisplayCount[1] = 0;

            if (IsValidHandle(m_handle_1))
            {
                dvpStreamState state = new dvpStreamState();
                dvpStatus status = new dvpStatus();

                // Implement a button to start and stop according to the current video's status.
                status = DVPCamera.dvpGetStreamState(m_handle_1, ref state);
                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                if (state == dvpStreamState.STATE_STARTED)
                {
                    status = DVPCamera.dvpStop(m_handle_1);
                }
                else
                {
                    status = DVPCamera.dvpStart(m_handle_1);
                }

                Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                InitCtrls_1();
            }
        }

        private void PropertSet_Click(object sender, EventArgs e)
        {
            if (IsValidHandle(m_handle_0))
            {
                dvpStatus status = DVPCamera.dvpShowPropertyModalDialog(m_handle_0, this.Handle);
            }
        }

        private void PropertSet1_Click(object sender, EventArgs e)
        {
            if (IsValidHandle(m_handle_1))
            {
                dvpStatus status = DVPCamera.dvpShowPropertyModalDialog(m_handle_1, this.Handle);
            }
        }


        public bool OpenByUserId(StringBuilder UserId, ref uint pHandle)
        {
            string str_UserId = UserId.ToString();

            if (UserId.Length < 1)
            {
                return false;
            }

            dvpStatus status = new dvpStatus(); ;

            // user dvpOpenByUserId open the camear
            status = DVPCamera.dvpOpenByUserId(str_UserId, dvpOpenMode.OPEN_NORMAL, ref pHandle);
            // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
            if (status == dvpStatus.DVP_STATUS_OK)
            {
                return true;
            }
            else
            {
                // If open the device unsuccessfully, Get a corresponding referenced string according to the status code.
                // Note: This string may change according to the language and version.
                MessageBox.Show("Open the device failed!");
                return false;
            }
        }

        private void MultipleCamera_FormClosing(object sender, FormClosingEventArgs e)
        {
            dvpStatus status;
            dvpStreamState state = new dvpStreamState();

            if (IsValidHandle(m_handle_0))
            {
                status = DVPCamera.dvpGetStreamState(m_handle_0, ref state);
                // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                if (state == dvpStreamState.STATE_STARTED)
                {
                    status = DVPCamera.dvpStop(m_handle_0);
                    // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                }
                status = DVPCamera.dvpClose(m_handle_0);
                // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);

                m_handle_0 = 0;
            }
            if (IsValidHandle(m_handle_1))
            {
                status = DVPCamera.dvpGetStreamState(m_handle_1, ref state);
                // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                if (state == dvpStreamState.STATE_STARTED)
                {
                    status = DVPCamera.dvpStop(m_handle_1);
                    // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                }
                status = DVPCamera.dvpClose(m_handle_1);
                // Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
                m_handle_1 = 0;
            }
          
        }

    }
}


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...