[MIL]MdigProcess C#例程开发

1、开发环境
 VS2019+C#+Windows10x64
2、引用包含
 Matrox.MatroxImagingLibrary.dll
3、代码
 参考官方目录下的MdigProcess C#例程
 关键成员变量
 private const int BUFFERING_SIZE_MAX = 20;//buffer队列的大小
 public MIL_ID MilDigitizer;
 public MIL_ID MilImageDisp;
 public int ProcessedImageCount;
 public string FilePathName = "D:\\1";//自动存图的位置
4、初始化相机

using Matrox.MatroxImagingLibrary;
using System;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    class Program
    {
        // Number of images in the buffering grab queue.
        // Generally, increasing this number gives a better real-time grab.
        private const int BUFFERING_SIZE_MAX = 20;//buffer队列的大小

        // User's processing function hook data object.
        public class HookDataStruct
        {
            public MIL_ID MilDigitizer;
            public MIL_ID MilImageDisp;
            public int ProcessedImageCount;
            public string FilePathName = "D:\\1";//自动存图的位置

        };

        // Main function.
        static void Main(string[] args)
        {
            MIL_ID MilApplication = MIL.M_NULL;
            MIL_ID MilSystem = MIL.M_NULL;
            MIL_ID MilDigitizer = MIL.M_NULL;
            MIL_ID MilDisplay = MIL.M_NULL;
            MIL_ID MilImageDisp = MIL.M_NULL;
            MIL_ID[] MilGrabBufferList = new MIL_ID[BUFFERING_SIZE_MAX];
            int MilGrabBufferListSize = 0;
            
            HookDataStruct UserHookData = new HookDataStruct();

            // Allocate defaults.
            // 使用MappAllocDefault默认分配app、system等资源,也可以使用MappAlloc、MsysAlloc等函数,单独分配
            MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay,ref MilDigitizer, MIL.M_NULL);

            // Allocate a monochrome display buffer. 
            //申请一块黑白的8位图像缓存
            MIL.MbufAlloc2d(MilSystem,MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X, MIL.M_NULL),MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y, MIL.M_NULL),
                8 + MIL.M_UNSIGNED,MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC + MIL.M_DISP,ref MilImageDisp);
            MIL.MbufClear(MilImageDisp, MIL.M_COLOR_BLACK);

            // Display the image buffer. 
            //锁定显示窗口,使用mil的显示窗
            MIL.MdispSelect(MilDisplay, MilImageDisp);

            // Print a message.打印抬头
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Press <Enter> to start processing.");
            Console.WriteLine();

            // Allocate the grab buffers and clear them.
            MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_DISABLE);//关闭弹窗打印报错信息

            //申请一串buffer,用于回调取流,跟MilImageDisp类似,主要去看BUFFERING_SIZE_MAX,默认申请了20个buffer缓存,避免回调阻塞
            for (MilGrabBufferListSize = 0; MilGrabBufferListSize < BUFFERING_SIZE_MAX; MilGrabBufferListSize++)
            {
                MIL.MbufAlloc2d(MilSystem,
                                MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X, MIL.M_NULL),
                                MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y, MIL.M_NULL),
                                8 + MIL.M_UNSIGNED,
                                MIL.M_IMAGE + MIL.M_GRAB + MIL.M_PROC,
                                ref MilGrabBufferList[MilGrabBufferListSize]);

                if (MilGrabBufferList[MilGrabBufferListSize] != MIL.M_NULL)
                {
                    MIL.MbufClear(MilGrabBufferList[MilGrabBufferListSize], 0xFF);
                }
                else
                {
                    break;
                }
            }

            MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_ENABLE);//开启弹窗打印报错信息

            // Initialize the user's processing function data structure.
            //回调里面自定义的一些变量初始化,用户也可以自己加一下变量,初始化就行
            UserHookData.MilDigitizer = MilDigitizer;
            UserHookData.MilImageDisp = MilImageDisp;
            UserHookData.ProcessedImageCount = 0;

            // get a handle to the HookDataStruct object in the managed heap, we will use this 
            // handle to get the object back in the callback function
            GCHandle hUserData = GCHandle.Alloc(UserHookData);
            MIL_DIG_HOOK_FUNCTION_PTR ProcessingFunctionPtr = new MIL_DIG_HOOK_FUNCTION_PTR(ProcessingFunction);

            // Start the processing. The processing function is called with every frame grabbed.
            //开启回调函数M_START,MilGrabBufferList就是我们想要的buffer
            MIL.MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize, MIL.M_START, MIL.M_DEFAULT, ProcessingFunctionPtr, GCHandle.ToIntPtr(hUserData));

            // Here the main() is free to perform other tasks while the processing is executing.
            // ---------------------------------------------------------------------------------

            // Print a message and wait for a key press after a minimum number of frames.
            Console.WriteLine("Press <Enter> to stop.                    ");
            Console.WriteLine();
            Console.ReadKey();

            // Stop the processing.
            //关闭回调函数M_STOP
            MIL.MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize, MIL.M_STOP, MIL.M_DEFAULT, ProcessingFunctionPtr, GCHandle.ToIntPtr(hUserData));
          
            // Free the GCHandle when no longer used
            hUserData.Free();

            // Print statistics.//打印结尾
            Console.WriteLine("Press <Enter> to end.");
            Console.WriteLine();
            Console.ReadKey();

            // Free the grab buffers.  
            //清除缓存,注意顺序
            while (MilGrabBufferListSize > 0)
            {
                MIL.MbufFree(MilGrabBufferList[--MilGrabBufferListSize]);
            }

            // Free display buffer.释放显示buffer
            MIL.MbufFree(MilImageDisp);

            // Release defaults. //也有单独释放每一个关键角色的函数
            MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MIL.M_NULL);
        }

        // User's processing function called every time a grab buffer is ready.
        // -----------------------------------------------------------------------

        // Local defines.
        private const int STRING_LENGTH_MAX = 20;
        private const int STRING_POS_X = 20;
        private const int STRING_POS_Y = 20;
        //callback function//回调函数
        static MIL_INT ProcessingFunction(MIL_INT HookType, MIL_ID HookId, IntPtr HookDataPtr)
        {
            MIL_ID ModifiedBufferId = MIL.M_NULL;
            MIL_INT TimerTriggerMissed = MIL.M_NULL;
            // this is how to check if the user data is null, the IntPtr class
            // contains a member, Zero, which exists solely for this purpose
            if (!IntPtr.Zero.Equals(HookDataPtr))
            {
                // get the handle to the DigHookUserData object back from the IntPtr
                GCHandle hUserData = GCHandle.FromIntPtr(HookDataPtr);

                // get a reference to the DigHookUserData object
                HookDataStruct UserData = hUserData.Target as HookDataStruct;

                // Retrieve the MIL_ID of the grabbed buffer.
                MIL.MdigGetHookInfo(HookId, MIL.M_MODIFIED_BUFFER + MIL.M_BUFFER_ID, ref ModifiedBufferId);

                // Increment the frame counter.
                UserData.ProcessedImageCount++;

                //Save image              
                string str = string.Format("{0}\\{1}.bmp", UserData.FilePathName, UserData.ProcessedImageCount);
                MIL.MbufExport(str, MIL.M_BMP, ModifiedBufferId);
            
                //Print and draw the frame count(remove to reduce CPU usage).计数
                Console.Write("Processing frame #{0}.\r", UserData.ProcessedImageCount);
                MIL.MgraText(MIL.M_DEFAULT, ModifiedBufferId, STRING_POS_X, STRING_POS_Y, String.Format("{0}", UserData.ProcessedImageCount));

                // Execute the processing and update the display.处理并更新显示
                MIL.MimArith(ModifiedBufferId, MIL.M_NULL, UserData.MilImageDisp, MIL.M_NOT);
                //ModifiedBufferId是我们想要的图像buffer
            }
            return 0;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值