/// <summary> /// Captures a compressed video frame from the camera. /// This method will try querying the image for a number of times until a valid image is received /// or all the queries time out. /// </summary> /// <returns>A Bitmap.</returns> /// <exception cref="TimeoutException">Thrown if the capture image query times out for every try.</exception> public Bitmap ForceCaptureImage() { int minResponseTimeout = Math.Max(this.ResponseTimeout, this._currentImageResolution.Timeout / 2); int maxResponseTimeout = Math.Max(this._currentImageResolution.Timeout, minResponseTimeout); int numTries = 5;// should be > 1; int increaseTimeout = (maxResponseTimeout - minResponseTimeout) / (numTries - 1); this.ResponseTimeout = minResponseTimeout; Bitmap frame = null; for (int i = 0; i < numTries; i++) { try { frame = this.CaptureImage(); break; } catch (TimeoutException) { this.ResponseTimeout += increaseTimeout; } } // Revert ResponseTimeout to the default value. this.ResponseTimeout = _responseTimeout; if (frame == null) { throw new TimeoutException("Image capture timed out for " + numTries + " tries."); } // Set current image resolution from the captured image. ImageResolutions res = this._currentImageResolution.Resolution; switch (frame.Width) { case 80: res = ImageResolutions.Resolution80x64; break; case 160: res = ImageResolutions.Resolution160x128; break; case 320: res = ImageResolutions.Resolution320x240; break; } if (this._currentImageResolution.Resolution != res) { this._currentImageResolution = new ImageResolution(res); } return(frame); }
/// <summary> /// Sets the camera capture resolution. /// </summary> /// <param name="resolution">A predefined image resolution to use when capturing images from the camera.</param> /// <exception cref="ArgumentException">Thrown if ImageResolution is set to a value other than one of the predefined resolutions.</exception> public void SetImageResolution(ImageResolutions resolution) { switch (resolution) { case ImageResolutions.Resolution80x64: this.SendCommand(SerialCommands.SetCaptureResolutionTo_80x64); break; case ImageResolutions.Resolution160x128: this.SendCommand(SerialCommands.SetCaptureResolutionTo_160x128); break; case ImageResolutions.Resolution320x240: this.SendCommand(SerialCommands.SetCaptureResolutionTo_320x240); break; default: throw new ArgumentException("Invalid resolution"); } this._currentImageResolution = new ImageResolution(resolution); }
static ImageResolution() { _defaultResolution = new ImageResolution(ImageResolutions.Resolution160x128); _defaultResTimeout = 1200;//ms }