private void singleFrameGrabMethod() { PylonGrabResult_t grabResult; PylonBuffer <Byte> imgBuf = null; /* Buffer used for grabbing. */ try { /* 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.*/ var channel = 0; if (!Pylon.DeviceGrabSingleFrame(_pylonDevHandle, channel, ref imgBuf, out grabResult, _singleFrameWaitup)) { /* Timeout occurred. */ _latestMessage = "Timeout"; } /* Check to see if the image was grabbed successfully. */ if (grabResult.Status == EPylonGrabStatus.Grabbed) { /* Success. Perform image processing. */ _latestImage = new HImage("byte", grabResult.SizeX, grabResult.SizeY, imgBuf.Pointer); } else if (grabResult.Status == EPylonGrabStatus.Failed) { _latestMessage = "Frame wasn't grabbed successfully. Error code = " + grabResult.ErrorCode; } } catch (Exception ex) { throw ex; } finally { /* Release the buffer. */ imgBuf.Dispose(); /* Free memory for grabbing. */ imgBuf = null; } }
//关闭相机 public override void CloseCameraSoftTrigger() { imgBuf.Dispose(); imgBuf = null; Pylon.Terminate(); }
static void Main(string[] args) { for (uint i = 0; i < args.Length; i++) { switch (args[i]) { case par_cameraSerialNumber: arg_cameraSerialNumber = args[i + 1]; i++; break; case par_pathToFile: arg_pathToFile = args[i + 1]; i++; break; case par_imageFormat: arg_imageFormat = args[i + 1]; i++; break; case par_packageSize: arg_packageSize = args[i + 1]; i++; break; case par_interPackageDelay: arg_interPackageDelay = args[i + 1]; i++; break; case par_attemptsToGrab: arg_attemptsToGrab = args[i + 1]; i++; break; case par_exposureTime: arg_exposureTime = args[i + 1]; i++; break; default: break; } } //arg_exposureTime = "35000"; bool error = false; if (arg_pathToFile.Length == 0) { Console.WriteLine("Path to file is empty"); error = true; } if (!(arg_imageFormat.Equals("BMP") || !arg_imageFormat.Equals("PNG") || !arg_imageFormat.Equals("JPG") || !arg_imageFormat.Equals("RAW") || !arg_imageFormat.Equals("TIFF"))) { Console.WriteLine("File format should be [BMP|PNG|JPG|RAW|TIFF]"); error = true; } if (arg_cameraSerialNumber.Length == 0) { Console.WriteLine("Camera serial number is empty"); error = true; } int exposureTime = 0; try { exposureTime = Int32.Parse(arg_exposureTime); } catch (Exception e) { Console.WriteLine("Wrong exposure time value"); error = true; } int interPackageDelay = 0; try { interPackageDelay = Int32.Parse(arg_interPackageDelay); } catch (Exception e) { Console.WriteLine("Wrong interPackageDelay value"); error = true; } int attemptsToGrap = 0; try { attemptsToGrap = Int16.Parse(arg_attemptsToGrab); }catch (Exception e) { Console.WriteLine("Wrong attempts to grab value"); error = true; } //sodapef if (error) { Console.WriteLine("Parameters usage:"); Console.WriteLine("-s Camera serial number"); Console.WriteLine("-o Path to file"); Console.WriteLine("-d Inter package delay in ticks (default 1000)"); Console.WriteLine("-a Attempts tp grab image (default 1)"); Console.WriteLine("-p Package size (default 1500)"); Console.WriteLine("-e Exposure time (default 35000)"); Console.WriteLine("-f Image format [BMP|PNG|JPG|RAW|TIFF]"); return; } PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE(); /* Handle for the pylon device. */ try { uint numDevices; /* Number of available devices. */ const int numGrabs = 1; /* Number of images to grab. */ PylonBuffer <Byte> imgBuf = null; /* Buffer used for grabbing. */ bool isAvail; Pylon.Initialize(); numDevices = Pylon.EnumerateDevices(); if (0 == numDevices) { Console.WriteLine("Error: No devices found"); return; } bool deviceFound = false; uint deviceNum = 0; for (uint di = 0; di < numDevices; di++) { PYLON_DEVICE_INFO_HANDLE hDi = Pylon.GetDeviceInfoHandle((uint)di); string serial = Pylon.DeviceInfoGetPropertyValueByName(hDi, Pylon.cPylonDeviceInfoSerialNumberKey); deviceNum = di; if (serial.Equals(arg_cameraSerialNumber)) { deviceFound = true; break; } } if (!deviceFound) { Console.WriteLine("Error: No devices found by serial number"); return; } hDev = Pylon.CreateDeviceByIndex(deviceNum); Pylon.DeviceOpen(hDev, Pylon.cPylonAccessModeControl | Pylon.cPylonAccessModeStream | Pylon.cPylonAccessModeExclusive); isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_PixelFormat_Mono8"); if (!isAvail) { Console.WriteLine("Error: Device doesn't support the Mono8 pixel format"); return; } Pylon.DeviceFeatureFromString(hDev, "PixelFormat", "Mono8"); isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_AcquisitionStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "AcquisitionStart"); Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off"); } isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameBurstStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameBurstStart"); Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off"); } isAvail = Pylon.DeviceFeatureIsAvailable(hDev, "EnumEntry_TriggerSelector_FrameStart"); if (isAvail) { Pylon.DeviceFeatureFromString(hDev, "TriggerSelector", "FrameStart"); Pylon.DeviceFeatureFromString(hDev, "TriggerMode", "Off"); } isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPSPacketSize"); if (isAvail) { int packageSize = 1500; try{ packageSize = Int32.Parse(arg_packageSize); } catch (Exception e) { } Pylon.DeviceSetIntegerFeature(hDev, "GevSCPSPacketSize", packageSize); } isAvail = Pylon.DeviceFeatureIsWritable(hDev, "GevSCPD"); if (isAvail) { Pylon.DeviceSetIntegerFeature(hDev, "GevSCPD", interPackageDelay); } Pylon.DeviceFeatureFromString(hDev, "ExposureAuto", "Off"); /* * isAvail = Pylon.DeviceFeatureIsWritable(hDev, "ExposureTimeRaw"); * if (isAvail) * { * try * { * Pylon.DeviceSetFloatFeature(hDev, "ExposureTimeRaw", exposureTime); * } * catch (Exception e) * { * Console.WriteLine("Some error 1"); * } * }*/ isAvail = Pylon.DeviceFeatureIsWritable(hDev, "ExposureTimeAbs"); if (isAvail) { Pylon.DeviceSetFloatFeature(hDev, "ExposureTimeAbs", (long)exposureTime); /* * try{ * Pylon.DeviceSetFloatFeature(hDev, "ExposureTimeAbs", (long)exposureTime); * }catch (Exception e) { * //Console.WriteLine("Some error 2"); * } */ } Byte min, max; PylonGrabResult_t grabResult; for (int attempt = 0; attempt < attemptsToGrap; attempt++) { if (!Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 5000)) { /* Timeout occurred. */ //Console.WriteLine("Frame {0}: timeout.", i + 1); Console.WriteLine("Error: timeout"); } /* Check to see if the image was grabbed successfully. */ if (grabResult.Status == EPylonGrabStatus.Grabbed) { /* Success. Perform image processing. */ getMinMax(imgBuf.Array, grabResult.SizeX, grabResult.SizeY, out min, out max); //Console.WriteLine("Grabbed frame {0}. Min. gray value = {1}, Max. gray value = {2}", i + 1, min, max); Console.WriteLine("Frame grabbed success"); /* Display image */ //Pylon.ImagePersistenceSave<Byte>(EPylonImageFileFormat.ImageFileFormat_Png, "C:\\Users\\v.yakubov\\grabber\\test.png", imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); if (arg_imageFormat.Equals("PNG")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Png, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else if (arg_imageFormat.Equals("JPG")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Jpeg, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else if (arg_imageFormat.Equals("RAW")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Raw, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else if (arg_imageFormat.Equals("TIFF")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Tiff, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else if (arg_imageFormat.Equals("BMP")) { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Bmp, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } else { Pylon.ImagePersistenceSave <Byte>(EPylonImageFileFormat.ImageFileFormat_Bmp, arg_pathToFile, imgBuf, grabResult.PixelType, (uint)grabResult.SizeX, (uint)grabResult.SizeY, 0, EPylonImageOrientation.ImageOrientation_TopDown); } break; } else if (grabResult.Status == EPylonGrabStatus.Failed) { //Console.Error.WriteLine("Frame {0} wasn't grabbed successfully. Error code = {1}", i + 1, grabResult.ErrorCode); Console.WriteLine("Error: failed"); } } imgBuf.Dispose(); Pylon.DeviceClose(hDev); Pylon.DestroyDevice(hDev); imgBuf = null; Pylon.Terminate(); } catch (Exception e) { try { if (hDev.IsValid) { if (Pylon.DeviceIsOpen(hDev)) { Pylon.DeviceClose(hDev); } Pylon.DestroyDevice(hDev); } } catch (Exception) {} Pylon.Terminate(); /* Releases all pylon resources. */ Environment.Exit(1); } }
static void Main(string[] args) { PYLON_DEVICE_HANDLE hDev = new PYLON_DEVICE_HANDLE(); /* Handle for the pylon device. */ try { uint numDevices; /* Number of available devices. */ const int numGrabs = 10; /* Number of images to grab. */ PylonBuffer <Byte> imgBuf = null; /* Buffer used for grabbing. */ bool isAvail; int i; #if DEBUG /* This is a special debug setting needed only for GigE cameras. * See 'Building Applications with pylon' in the Programmer's Guide. */ Environment.SetEnvironmentVariable("PYLON_GIGE_HEARTBEAT", "300000" /*ms*/); #endif /* Before using any pylon methods, the pylon runtime must be initialized. */ Pylon.Initialize(); /* Enumerate all camera devices. You must call * PylonEnumerateDevices() before creating a device. */ numDevices = Pylon.EnumerateDevices(); if (0 == numDevices) { throw new Exception("No devices found."); } /* 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. */ throw new Exception("Device doesn't support the Mono8 pixel format."); } /* ... 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); } /* Grab some images in a loop. */ for (i = 0; i < numGrabs; ++i) { Byte min, max; 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.*/ if (!Pylon.DeviceGrabSingleFrame(hDev, 0, ref imgBuf, out grabResult, 500)) { /* Timeout occurred. */ Console.WriteLine("Frame {0}: timeout.", i + 1); } /* Check to see if the image was grabbed successfully. */ if (grabResult.Status == EPylonGrabStatus.Grabbed) { /* Success. Perform image processing. */ getMinMax(imgBuf.Array, grabResult.SizeX, grabResult.SizeY, out min, out max); Console.WriteLine("Grabbed frame {0}. Min. gray value = {1}, Max. gray value = {2}", i + 1, min, max); /* Display image */ Pylon.ImageWindowDisplayImage <Byte>(0, imgBuf, grabResult); } else if (grabResult.Status == EPylonGrabStatus.Failed) { Console.Error.WriteLine("Frame {0} wasn't grabbed successfully. Error code = {1}", i + 1, grabResult.ErrorCode); } } /* Release the buffer. */ imgBuf.Dispose(); /* Clean up. Close and release the pylon device. */ Pylon.DeviceClose(hDev); Pylon.DestroyDevice(hDev); /* Free memory for grabbing. */ imgBuf = null; Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); /* Shut down the pylon runtime system. Don't call any pylon method after * calling Pylon.Terminate(). */ Pylon.Terminate(); } catch (Exception e) { /* Retrieve the error message. */ string msg = GenApi.GetLastErrorMessage() + "\n" + GenApi.GetLastErrorDetail(); Console.Error.WriteLine("Exception caught:"); Console.Error.WriteLine(e.Message); if (msg != "\n") { Console.Error.WriteLine("Last error message:"); Console.Error.WriteLine(msg); } try { if (hDev.IsValid) { /* ... Close and release the pylon device. */ if (Pylon.DeviceIsOpen(hDev)) { Pylon.DeviceClose(hDev); } Pylon.DestroyDevice(hDev); } } catch (Exception) { /*No further handling here.*/ } Pylon.Terminate(); /* Releases all pylon resources. */ Console.Error.WriteLine("\nPress enter to exit."); Console.ReadLine(); Environment.Exit(1); } }