C#和Visionpro混合编程实现工业相机实时图像采集

C s h a r p 和 V i s i o n p r o 混 合 编 程 实 现 工 业 相 机 实 时 图 像 采 集 Csharp和Visionpro混合编程实现工业相机实时图像采集 CsharpVisionpro

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

namespace Demo
{
    public partial class Form1 : Form
    {
        private CogImageFileTool m_ImageFileTool;
        private Thread ThreadObject;                //线程
        private bool   ThreadStop = false;

        public Form1()
        {
            InitializeComponent();

            //线程对象实例化
            ThreadObject = new Thread(new ThreadStart(ThreadFunction));

            //打开相机
            OpenCameraSoftTrigger();


        }

        private void DisplayImage(object sender, EventArgs e)
        {

            if (ThreadObject.ThreadState == System.Threading.ThreadState.Unstarted)
            {
                ThreadObject.Start();

            }

        }


        private void StopPlay_button_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("没有找到相机");
                return;
            }
            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");
            }

        }

    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值