Vpro&C#混合编程,Basler相机加载显示实时图像

******注意添加引用   PylonC.NET.dll

目前手头没有相机,故出现没有找到相机提示窗口,点击确定窗口自动关闭。

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;
using Cognex.VisionPro;
using PylonC.NET;
using System.Runtime.InteropServices;
using System.Threading;

namespace Basler
{
     
    public partial class Form1 : Form
    {
        private Thread ThreadObject;                //线程
        private bool ThreadStop = false;
        public Form1()
        {
            InitializeComponent();
            //线程对象实例化
            ThreadObject = new Thread(new ThreadStart(ThreadFunction));

            //打开相机
            OpenCameraSoftTrigger();
        }

        private void button1_Click(object sender, EventArgs e)
        {
              if (ThreadObject.ThreadState == System.Threading.ThreadState.Unstarted)
            {
                ThreadObject.Start();

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
        ThreadStop = true;

            //停止采集图像
            Pylon.DeviceClose(hDev);
            Pylon.DestroyDevice(hDev);
        }
         //线程回调函数
        public void ThreadFunction()
        {
            int ImageWidth = 1280;
            int ImageHeight = 1024;
            CogImage8Grey Image = new CogImage8Grey();
            var cogRoot = new CogImage8Root();
            IntPtr ImageBufferPtr = Marshal.AllocHGlobal(ImageWidth * ImageHeight);
            byte[] ImageBuffer = new byte[ImageWidth * ImageHeight];
            while (!ThreadStop)
            {
                //采集单张图像
                SnapAcquisitionSoftTrigger(ref ImageBuffer);

                //将图像数据从托管区拷贝到非托管区
                Marshal.Copy(ImageBuffer, 0, ImageBufferPtr, ImageWidth * ImageHeight);

                //初始化
                cogRoot.Initialize(ImageWidth, ImageHeight, ImageBufferPtr, ImageWidth, null);

                //指定Image图像数据为cogRoot
                Image.SetRoot(cogRoot);

                //将图像数据传给cogRecordDisplay1控件
                cogRecordDisplay1.Image = Image as CogImage8Grey;

                //显示图像
                cogRecordDisplay1.Fit(true);

            }
        }


        ///////////////////////////////////////////////////balser  sdk/////////////////////////////////////////////////////
        private PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE();   /* Handle for the pylon device. */
        private uint numDevices;                                        /* Number of available devices. */
        private const int numGrabs = 1000;                              /* Number of images to grab. */
        private PylonBuffer<Byte> imgBuf = null;                        /* Buffer used for grabbing. */

        //打开相机
        public void OpenCameraSoftTrigger()
        {
            bool isAvail;

            Pylon.Initialize();

            /* Enumerate all camera devices. You must call PylonEnumerateDevices() before creating a device. */
            numDevices = Pylon.EnumerateDevices();

            if (0 == numDevices)
            {
                MessageBox.Show("没有找到相机");
             
                System.Environment.Exit(0);
            }
            else
            {
                /* Get a handle for the first device found.  */
                hDev = Pylon.CreateDeviceByIndex(0);
            }

            /* Before using the device, it must be opened. Open it for configuring parameters and for grabbing images. */
            Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream);

            /* Set the pixel format to Mono8, where gray values will be output as 8 bit values for each pixel. */
            /* ... Check first to see if the device supports the Mono8 format. */
            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono8");

            if (!isAvail)
            {
                /* Feature is not available. */
                MessageBox.Show("设备不支持8位灰度图像");
            }

            /* ... Set the pixel format to Mono8. */
            Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8");

            /* Disable acquisition start trigger if available. */
            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart");
            if (isAvail)
            {
                Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart");
                Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
            }


            /* Disable frame burst start trigger if available */
            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart");
            if (isAvail)
            {
                Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart");
                Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
            }

            /* Disable frame start trigger if available */
            isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart");
            if (isAvail)
            {
                Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart");
                Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off");
            }

            /* For GigE cameras, we recommend increasing the packet size for better
            performance. If the network adapter supports jumbo frames, set the packet
            size to a value > 1500, e.g., to 8192. In this sample, we only set the packet size
            to 1500. */
            /* ... Check first to see if the GigE camera packet size parameter is supported and if it is writable. */
            isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize");

            if (isAvail)
            {
                /* ... The device supports the packet size feature. Set a value. */
                Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", 1500);
            }

        }


        //采集单张图像
        public void SnapAcquisitionSoftTrigger(ref byte[] ImageBufferPtr)
        {

            PylonGrabResult_t grabResult;

            /* Grab one single frame from stream channel 0. The
            camera is set to "single frame" acquisition mode.
            Wait up to 500 ms for the image to be grabbed.
            If imgBuf is null a buffer is automatically created with the right size.*/
            Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 500);
            IntPtr dataAddress = imgBuf.Pointer;

            /* Check to see if the image was grabbed successfully. */
            if (grabResult.Status == EPylonGrabStatus.Grabbed)
            {
                Marshal.Copy(dataAddress, ImageBufferPtr, 0, 1280 * 1024 - 1);

            }
            else
            {
                Console.WriteLine("图像抓取失败!n");
            }

        }
    }
}

猜你喜欢

转载自blog.csdn.net/Qhj_Miracle/article/details/82808982