//******************************************************************************* // Autofocus hook function responsible to move the lens. static MIL_INT MoveLensHookFunction(MIL_INT HookType, MIL_INT Position, IntPtr UserDataHookPtr) { // 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(UserDataHookPtr)) { // get the handle to the DigHookUserData object back from the IntPtr GCHandle hUserData = GCHandle.FromIntPtr(UserDataHookPtr); // get a reference to the DigHookUserData object DigHookUserData UserData = hUserData.Target as DigHookUserData; // Here, the lens position must be changed according to the Position parameter. // In that case, we simulate the lens position change followed by a grab. if (HookType == MIL.M_CHANGE || HookType == MIL.M_ON_FOCUS) { SimulateGrabFromCamera(UserData.SourceImage, UserData.FocusImage, (int)Position, UserData.Display); UserData.Iteration++; } } return(0); }
static void Main(string[] args) { MIL_ID MilApplication = MIL.M_NULL; // Application identifier. MIL_ID MilSystem = MIL.M_NULL; // System identifier. MIL_ID MilDisplay = MIL.M_NULL; // Display identifier. MIL_ID MilSource = MIL.M_NULL; // Source image. MIL_ID MilCameraFocus = MIL.M_NULL; // Focus simulated image. MIL_INT FocusPos = 0; // Best focus position. DigHookUserData UserData = new DigHookUserData(); // User data passed to the hook. // Allocate defaults. MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL); // Load the source image. MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilSource); MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilCameraFocus); MIL.MbufClear(MilCameraFocus, 0); // Select image on the display. MIL.MdispSelect(MilDisplay, MilCameraFocus); // Simulate the first image grab. SimulateGrabFromCamera(MilSource, MilCameraFocus, FOCUS_START_POSITION, MilDisplay); // Initialize user data needed within the hook function. UserData.SourceImage = MilSource; UserData.FocusImage = MilCameraFocus; UserData.Iteration = 0; UserData.Display = MilDisplay; // Pause to show the original image. Console.Write("\nAUTOFOCUS:\n"); Console.Write("----------\n\n"); Console.Write("Automatic focusing operation will be done on this image.\n"); Console.Write("Press <Enter> to continue.\n\n"); Console.ReadKey(); Console.Write("Autofocusing...\n\n"); // Perform Autofocus. // Since lens movement is hardware specific, no digitizer is used here. // We simulate the lens movement with by smoothing the image data in // the hook function instead. // get a handle to the DigHookUserData object in the managed heap, we will use this // handle to get the object back in the callback function GCHandle hUserData = GCHandle.Alloc(UserData); // We give the handle to the DigHookUserData object by casting it to a IntPtr, // later in the hook handler we will receive our IntPtr back and cast it again // to a GCHandle to get a handle to our object MIL.MdigFocus(MIL.M_NULL, MilCameraFocus, MIL.M_DEFAULT, MoveLensHookFunction, GCHandle.ToIntPtr(hUserData), FOCUS_MIN_POSITION, FOCUS_START_POSITION, FOCUS_MAX_POSITION, FOCUS_MAX_POSITION_VARIATION, FOCUS_MODE + FOCUS_SENSITIVITY, ref FocusPos); // Free the GCHandle when no longer used hUserData.Free(); // Print the best focus position and number of iterations. Console.Write("The best focus position is {0}.\n", FocusPos); Console.Write("The best focus position found in {0} iterations.\n\n", UserData.Iteration); Console.Write("Press <Enter> to end.\n"); Console.ReadKey(); // Free all allocations. MIL.MbufFree(MilSource); MIL.MbufFree(MilCameraFocus); MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL); }