public async Task TakePicturesAsync(string filename, TimeSpan duration, int msWaitBetweenPictures) { try { // Singleton initialized lazily. Reference once in your application. MMALCamera cam = this.MMALSharpCameraInstance; MMALCameraConfig.StillResolution = new Resolution(1080, 920); cam.ConfigureCameraSettings(); using (var imgCaptureHandler = new IndexedImageStreamCaptureHandler(filename)) { Console.WriteLine($"Current filename in handler: {imgCaptureHandler.CurrentFilename}"); var cts = new CancellationTokenSource(duration); var timelapse = new Timelapse { Mode = TimelapseMode.Millisecond, Value = msWaitBetweenPictures, CancellationToken = cts.Token }; await cam.TakePictureTimelapse(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420, timelapse); } // Cleanup disposes all unmanaged resources and unloads Broadcom library. To be called when no more processing is to be done // on the camera. Console.WriteLine($"Wrote picture to: {filename} with running index"); } catch (Exception ex) { Console.WriteLine($"{nameof(MMALSharpCamera)} {nameof(TakePictureAsync)} Failed"); Console.WriteLine($"{nameof(MMALSharpCamera)} {nameof(TakePictureAsync)} {ex.ToString()}"); Console.WriteLine($"{nameof(MMALSharpCamera)} {nameof(TakePictureAsync)} Failed"); } }
public async Task <TakeTimelapseResponse> StartTimelapseAsync(TakeTimelapseRequest req) { if (IsTakingTimelapse()) { return(new TakeTimelapseResponse() { ErrorMessage = $"Timelapse {currentTimelapseId?.ToString()} is being taken", }); } var cameraUsed = false; try { var path = EnsureLocalDirectoryExists(); currentTimelapseId = Guid.NewGuid().ToString(); var pathForImages = Path.Combine(path, currentTimelapseId); await cameraInUse.WaitAsync(); cameraUsed = true; // This example will take an image every 10 seconds for 4 hours var imgCaptureHandler = new ImageStreamCaptureHandler(pathForImages, "jpg"); timelapseCts = new CancellationTokenSource(TimeSpan.FromSeconds(req.Duration)); var tl = new Timelapse { Mode = TimelapseMode.Second, CancellationToken = timelapseCts.Token, Value = req.Interval }; Logger.Log($"Starting timelapse {currentTimelapseId}"); _ = camera.TakePictureTimelapse(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420, tl) .ContinueWith(async(t) => await PrepareTimelapseVideoAsync(t, imgCaptureHandler, pathForImages)); return(new TakeTimelapseResponse() { Id = currentTimelapseId, Duration = req.Duration, Interval = req.Interval, }); } catch { if (cameraUsed) { cameraInUse.Release(); } throw; } }