C#调用度申工业相机实现双摄像头显示源码
By
admin
at 2023-02-01 • 0人收藏 • 594人看过
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
登录后方可回帖
太长了, 分两截 ,后半部分代码: