protected int LookforSelectedCamera(bool force = false) { bool selectedCameraFound = false; selectedCameraConnected = false; if (!IsEnabled && !force) { return(-1); } int numberConnectedCameras = WebcamManager.NumberConnectedCameras(); if (numberConnectedCameras == 0) { selectedCameraConnected = false; Logger.WriteLine("No camera available"); ErrorLog.AddError(ErrorType.Failure, "Motion detection is disabled because no camera was detected"); return(-1); } int index = -1; if (selectedCameraPath == default(string)) { ErrorLog.AddError(ErrorType.Message, "No webcam configuration. Defaulting to first available webcam."); return(-1); } else { for (int i = 0; i < numberConnectedCameras; i++) { if (WebcamManager.GetCamera(i).WebCamPath == selectedCameraPath) { if (WebcamManager.GetCamera(i).IsActive) { index = i; selectedCameraConnected = true; selectedCameraFound = true; break; } else if (WebcamManager.StartCamera(i, captureWidth, captureHeight)) { index = i; selectedCameraConnected = true; selectedCameraFound = true; break; } } } if (!selectedCameraFound && numberConnectedCameras > 0) { ErrorLog.AddError(ErrorType.Failure, "The originally selected camera is not available."); } return(index); } }
private void UpdatePanel() { if (WebcamManager.IsServiceRestartRequired()) { WebcamManager.RestartService(); } if (!callbacksRegistered) { WebcamManager.RegisterWebcamConnectionEvent(OnCameraConnected); WebcamManager.RegisterWebcamRemovalEvent(OnCameraDisconnected); callbacksRegistered = true; } numberConnectedCameras = WebcamManager.NumberConnectedCameras(); cameraList = new List <int>(); selectedCameraIndex = -1; int numAvailableCameras = 0; cameraPreviews.Clear(); //remove previous viewers //populate the camera list and find the one that is available and set it as current selection... if (numberConnectedCameras == 0) { ShowNoCamerasFoundPanel(); return; } for (int i = 0; i < numberConnectedCameras; i++) { //if (WebcamManager.StartCamera(i, captureWidth, captureHeight)) { ImageViewer viewer = new ImageViewer(); viewer.ViewerWidth = previewWidth; viewer.ViewerHeight = previewHeight; viewer.TopTitle = WebcamManager.GetCamera(i).WebCamName; if (WebcamManager.StartCamera(i, captureWidth, captureHeight)) { if (viewer.TopTitle == selectedCameraName || selectedCameraIndex == -1) { selectedCameraIndex = i; } viewer.SetImageSource(WebcamManager.GetCamera(i)); viewer.MouseEnter += Viewer_MouseEnter; viewer.MouseLeave += Viewer_MouseLeave; viewer.PreviewMouseLeftButtonUp += Viewer_MouseLeftButtonUp; ++numAvailableCameras; } else { viewer.ImageSource.Source = loadBitmap(Properties.Resources.CamBusyIcon); viewer.BottomTitle = "This webcam is already busy"; } cameraList.Add(i); cameraPreviews.Add(viewer); } } if (selectedCameraIndex != -1) { cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerBorderColor = "LightGreen"; cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerWidth = previewHeight + 16; cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerHeight = cameraPreviews[cameraList.IndexOf(selectedCameraIndex)].ViewerWidth * 3 / 4; } //release inactive cameras so that they can be made available to other processes... WebcamManager.ReleaseInactiveCameras(); CameraReadiness = true; }