/// <summary> /// Close opened camera (if any) and release allocated resources. /// </summary> /// /// <remarks><para><note>The method also calls <see cref="StopAcquisition"/> method if it was not /// done by user.</note></para></remarks> /// /// <exception cref="VideoException">An error occurred while communicating with a camera. See error /// message for additional information.</exception> /// public void Close( ) { lock ( sync ) { if (deviceHandle != IntPtr.Zero) { if (isAcquisitionStarted) { try { StopAcquisition( ); } catch { } } try { int errorCode = XimeaAPI.xiCloseDevice(deviceHandle); HandleError(errorCode); } finally { deviceHandle = IntPtr.Zero; } } } }
/// <summary> /// Set camera's parameter. /// </summary> /// /// <param name="parameterName">Parameter name.</param> /// <param name="value">Float parameter value.</param> /// /// <remarks><para>The method allows to control different camera's parameters, like exposure time, gain value, etc. /// See <see cref="CameraParameter"/> class for the list of some possible configuration parameters. See /// XIMEA documentation for the complete list of supported parameters. /// </para></remarks> /// /// <exception cref="VideoException">An error occurred while communicating with a camera. See error /// message for additional information.</exception> /// <exception cref="NotConnectedException">No camera was opened, so can not access its methods.</exception> /// public void SetParam(string parameterName, float value) { lock ( sync ) { CheckConnection( ); int errorCode = XimeaAPI.xiSetParam(deviceHandle, parameterName, ref value, 4, ParameterType.Float); HandleError(errorCode); } }
/// <summary> /// Begin camera's work cycle and start data acquisition from it. /// </summary> /// /// <remarks><para>The <see cref="IsAcquisitionStarted"/> property can be used at any time to find if the /// acquisition was started or not.</para></remarks> /// /// <exception cref="VideoException">An error occurred while communicating with a camera. See error /// message for additional information.</exception> /// <exception cref="NotConnectedException">No camera was opened, so can not access its methods.</exception> /// public void StartAcquisition( ) { lock ( sync ) { CheckConnection( ); int errorCode = XimeaAPI.xiStartAcquisition(deviceHandle); HandleError(errorCode); isAcquisitionStarted = true; } }
/// <summary> /// Open XIMEA camera. /// </summary> /// /// <param name="deviceID">Camera ID to open.</param> /// /// <remarks><para>Opens the specified XIMEA camera preparing it for starting video acquisition /// which is done using <see cref="StartAcquisition"/> method. The <see cref="IsDeviceOpen"/> /// property can be used at any time to find if a camera was opened or not.</para></remarks> /// /// public void Open(int deviceID) { lock (_sync) { int errorCode = XimeaAPI.xiOpenDevice(deviceID, out IntPtr deviceHandle); HandleError(errorCode); // save the device handle is everything is fine _deviceHandle = deviceHandle; _isAcquisitionStarted = false; _deviceID = deviceID; } }
/// <summary> /// Get camera's parameter as float value. /// </summary> /// /// <param name="parameterName">Parameter name to get from camera.</param> /// /// <returns>Returns float value of the requested parameter.</returns> /// /// <remarks><para>See <see cref="CameraParameter"/> class for the list of some possible configuration parameters. See /// XIMEA documentation for the complete list of supported parameters.</para></remarks> /// /// public float GetParamFloat(string parameterName) { lock (_sync) { CheckConnection(); ParameterType type = ParameterType.Float; int errorCode = XimeaAPI.xiGetParam(_deviceHandle, parameterName, out float value, out int size, ref type); HandleError(errorCode); return(value); } }
/// <summary> /// Get camera's parameter as integer value. /// </summary> /// /// <param name="parameterName">Parameter name to get from camera.</param> /// /// <returns>Returns integer value of the requested parameter.</returns> /// /// <remarks><para>See <see cref="CameraParameter"/> class for the list of some possible configuration parameters. See /// XIMEA documentation for the complete list of supported parameters.</para></remarks> /// /// <exception cref="VideoException">An error occurred while communicating with a camera. See error /// message for additional information.</exception> /// <exception cref="NotConnectedException">No camera was opened, so can not access its methods.</exception> /// public int GetParamInt(string parameterName) { lock ( sync ) { CheckConnection( ); int value; int size; ParameterType type = ParameterType.Integer; int errorCode = XimeaAPI.xiGetParam(deviceHandle, parameterName, out value, out size, ref type); HandleError(errorCode); return(value); } }
/// <summary> /// End camera's work cycle and stops data acquisition. /// </summary> /// /// <exception cref="VideoException">An error occurred while communicating with a camera. See error /// message for additional information.</exception> /// <exception cref="NotConnectedException">No camera was opened, so can not access its methods.</exception> /// public void StopAcquisition( ) { lock ( sync ) { CheckConnection( ); try { int errorCode = XimeaAPI.xiStopAcquisition(deviceHandle); HandleError(errorCode); } finally { isAcquisitionStarted = false; } } }
/// <summary> /// Get camera's parameter as string value. /// </summary> /// /// <param name="parameterName">Parameter name to get from camera.</param> /// /// <returns>Returns string value of the requested parameter.</returns> /// /// <remarks><para>See <see cref="CameraParameter"/> class for the list of some possible configuration parameters. See /// XIMEA documentation for the complete list of supported parameters.</para></remarks> /// /// <exception cref="VideoException">An error occurred while communicating with a camera. See error /// message for additional information.</exception> /// <exception cref="NotConnectedException">No camera was opened, so can not access its methods.</exception> /// public string GetParamString(string parameterName) { lock ( sync ) { CheckConnection( ); byte[] bytes = new byte[260]; int size = bytes.Length; ParameterType type = ParameterType.String; unsafe { fixed(byte *ptr = bytes) { int errorCode = XimeaAPI.xiGetParam(deviceHandle, parameterName, ptr, out size, ref type); HandleError(errorCode); } } return(Encoding.ASCII.GetString(bytes, 0, size)); } }
/// <summary> /// Get image from the opened XIMEA camera. /// </summary> /// /// <param name="timeout">Maximum time to wait in milliseconds till image becomes available.</param> /// <param name="makeCopy">Make a copy of the camera's image or not.</param> /// /// <returns>Returns image retrieved from the camera.</returns> /// /// <remarks><para>If the <paramref name="makeCopy"/> is set to <see langword="true"/>, then the method /// creates a managed copy of the camera's image, so the managed image stays valid even when the camera /// is closed. However, setting this parameter to <see langword="false"/> creates a managed image which is /// just a wrapper around camera's unmanaged image. So if camera is closed and its resources are freed, the /// managed image becomes no longer valid and accessing it will generate an exception.</para></remarks> /// /// <exception cref="VideoException">An error occurred while communicating with a camera. See error /// message for additional information.</exception> /// <exception cref="NotConnectedException">No camera was opened, so can not access its methods.</exception> /// <exception cref="TimeoutException">Time out value reached - no image is available within specified time value.</exception> /// public Bitmap GetImage(int timeout, bool makeCopy) { lock ( sync ) { CheckConnection( ); int errorCode; XimeaImage ximeaImage = new XimeaImage( ); unsafe { ximeaImage.StructSize = sizeof(XimeaImage); } // get image from XIMEA camera try { errorCode = XimeaAPI.xiGetImage(deviceHandle, timeout, ref ximeaImage); } catch (AccessViolationException) { errorCode = 9; } // handle error if any HandleError(errorCode); // create managed bitmap for the unmanaged image provided by camera PixelFormat pixelFormat = PixelFormat.Undefined; int stride = 0; switch (ximeaImage.PixelFormat) { case ImageFormat.Grayscale8: pixelFormat = PixelFormat.Format8bppIndexed; stride = ximeaImage.Width; break; case ImageFormat.RGB24: pixelFormat = PixelFormat.Format24bppRgb; stride = ximeaImage.Width * 3; break; case ImageFormat.RGB32: pixelFormat = PixelFormat.Format32bppRgb; stride = ximeaImage.Width * 4; break; default: throw new VideoException("Unsupported pixel format."); } Bitmap bitmap = null; if (!makeCopy) { bitmap = new Bitmap(ximeaImage.Width, ximeaImage.Height, stride, pixelFormat, ximeaImage.BitmapData); } else { bitmap = new Bitmap(ximeaImage.Width, ximeaImage.Height, pixelFormat); // lock destination bitmap data BitmapData bitmapData = bitmap.LockBits( new Rectangle(0, 0, ximeaImage.Width, ximeaImage.Height), ImageLockMode.ReadWrite, pixelFormat); int dstStride = bitmapData.Stride; int lineSize = Math.Min(stride, dstStride); unsafe { byte *dst = (byte *)bitmapData.Scan0.ToPointer( ); byte *src = (byte *)ximeaImage.BitmapData.ToPointer( ); if (stride != dstStride) { // copy image for (int y = 0; y < ximeaImage.Height; y++) { AForge.SystemTools.CopyUnmanagedMemory(dst, src, lineSize); dst += dstStride; src += stride; } } else { AForge.SystemTools.CopyUnmanagedMemory(dst, src, stride * ximeaImage.Height); } } // unlock destination images bitmap.UnlockBits(bitmapData); } // set palette for grayscale image if (ximeaImage.PixelFormat == ImageFormat.Grayscale8) { ColorPalette palette = bitmap.Palette; for (int i = 0; i < 256; i++) { palette.Entries[i] = Color.FromArgb(i, i, i); } bitmap.Palette = palette; } return(bitmap); } }