/// <summary> /// Will capture a single image and store it in the baseline images list. Each image will be used to determine if we have an alert or not. /// </summary> private async void CaptureBaseLineImage() { try { lowLagCapture = await MediaCaptureElement.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8)); CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync(); SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; WriteableBitmap writeableBitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight); softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer); byte[] imageBytes = new byte[4 * softwareBitmap.PixelWidth * softwareBitmap.PixelHeight]; softwareBitmap.CopyToBuffer(imageBytes.AsBuffer()); if (baselineImages.Count > 6) { baselineImages.Clear(); DisplayImages.Clear(); } baselineImages.Add(imageBytes); DisplayImages.Add(writeableBitmap); await lowLagCapture.FinishAsync(); } catch (Exception) { // Sometimes when you serial click the capture button we get an explosion... // Eat it and move on } }
/// <summary> /// Tests the systems capture and email functionality. /// </summary> /// <param name="sender">Sending button</param> /// <param name="e">Button args</param> private async void RunTestsExecuted() { try { if (captureTimer.IsEnabled) { captureTimer.Stop(); } IsNotRunningTest = false; var stream = new InMemoryRandomAccessStream(); await MediaCaptureElement.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); await Task.Delay(10); List <IRandomAccessStream> streamTestList = new List <IRandomAccessStream>() { stream }; await SendAlertEmail(streamTestList, true); IsNotRunningTest = true; captureTimer.Start(); } catch (Exception) { /*Working with hardware has a lot of exception cases.... nom nom nom*/ } }
/// <summary> /// Sets up the application and initializes the camera. /// </summary> public async void Setup(CaptureElement captureElement) { Windows.System.Display.DisplayRequest _displayRequest = null; //create the request instance if needed if (_displayRequest == null) { _displayRequest = new Windows.System.Display.DisplayRequest(); } //make request to put in active state _displayRequest.RequestActive(); StorageFolder storageFolder = ApplicationData.Current.LocalFolder; if (await FileExists("config.json")) { StorageFile sampleFile = await storageFolder.GetFileAsync("config.json"); string file = await FileIO.ReadTextAsync(sampleFile); ConfigurationSettings = JsonConvert.DeserializeObject <ConfigModel>(file); } else { StorageFile sampleFile = await storageFolder.CreateFileAsync("config.json", CreationCollisionOption.ReplaceExisting); string settingsJson = "{\"smtpSettings\":{\"smtpRelay\":\"\",\"smtpPort\":465,\"useSSL\":true,\"smtpUserName\":\"\",\"smtpPassword\":\"\",\"recipient\":\"\",},\"appConfig\":{\"sendEmails\":false,\"captureDelay\":500,\"alertDelay\":2,\"alertThreshold\":2,\"pixelDelta\":.3,\"imageDelta\":7,}}"; await FileIO.WriteTextAsync(sampleFile, settingsJson); ConfigurationSettings = JsonConvert.DeserializeObject <ConfigModel>(settingsJson); } Sensitivity = ConfigurationSettings.AppConfig.ImageDelta; ConfigurationSettings.SmtpSettings.PropertyChanged += SmtpSettings_PropertyChanged; await MediaCaptureElement.InitializeAsync(); try { captureElement.Source = MediaCaptureElement; } catch (Exception) { } displayRequest.RequestActive(); await MediaCaptureElement.StartPreviewAsync(); baselineTimer.Interval = new TimeSpan(0, 0, 10); baselineTimer.Tick += OnBaselineTimerTick; baselineTimer.Start(); captureTimer.Interval = new TimeSpan(0, 0, 0, 0, ConfigurationSettings.AppConfig.CaptureDelay); captureTimer.Tick += OnCaptureTimerTick; }
public async Task <bool> Dispose() { await MediaCaptureElement.StopPreviewAsync(); MediaCaptureElement.Dispose(); MediaCaptureElement = null; baselineImages = null; captureTimer.Stop(); baselineTimer.Tick -= OnBaselineTimerTick; captureTimer.Tick -= OnCaptureTimerTick; delayTimer.Tick -= OnDelayTimerTick; displayRequest = null; streamList = null; DisplayImages = null; AlertDisplayImages = null; UpdateSettingsCommand = null; RunTestsCommand = null; SaveImageCommand = null; return(true); }
/// <summary> /// Captures a photo and sends it off for analysis /// </summary> private async void TakePhoto() { try { lowLagCapture = await MediaCaptureElement.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8)); CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync(); SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; await lowLagCapture.FinishAsync(); byte[] imageBytes = new byte[4 * softwareBitmap.PixelWidth * softwareBitmap.PixelHeight]; softwareBitmap.CopyToBuffer(imageBytes.AsBuffer()); bool isAlert = CheckForMotion(imageBytes); if (isAlert) { WriteableBitmap writeableBitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight); softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer); AlertDisplayImages.Add(new AlertDisplayImageModel() { AlertDisplayImage = writeableBitmap, AlertDisplayCaption = DateTime.Now.ToString() }); delayTimer.Interval = new TimeSpan(0, 0, ConfigurationSettings.AppConfig.AlertDelay); captureTimer.Stop(); delayTimer.Tick += OnDelayTimerTick; delayTimer.Start(); // It seems silly that we need to capture a second image but the first image that was captured isn't in a format that can // be easily emailed. This being the case, I decided it'd just be easier to grab another capture in the correct format and // email it off. The delta between the images is negligable var stream = new InMemoryRandomAccessStream(); await MediaCaptureElement.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); await Task.Delay(10); streamList.Add(stream); if (AutoSaveAlertImages) { } if (ConfigurationSettings.AppConfig.SendEmails && streamList.Count > ConfigurationSettings.AppConfig.AlertThreshold) { captureTimer.Stop(); await SendAlertEmail(streamList); await Task.Delay(new TimeSpan(0, 1, 0)); } } } catch (Exception error) { // Getting random COM errors. Just eat it and continue. There's nothing I can do about this. } }