static private LogWarn ( string format ) : void | ||
format | string | |
return | void |
async Task SetupCamera() { if (camera != null) { return; } await tcsSurfaceReady.Task; lastPreviewAnalysis = DateTime.UtcNow.AddMilliseconds(this.scanningOptions.InitialDelayBeforeAnalyzingFrames); isAnalyzing = true; CheckCameraPermissions(); var perf = PerformanceCounter.Start(); GetExclusiveAccess(); try { var version = Build.VERSION.SdkInt; if (version >= BuildVersionCodes.Gingerbread) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Checking Number of cameras..."); var numCameras = Camera.NumberOfCameras; var camInfo = new Camera.CameraInfo(); var found = false; Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Found " + numCameras + " cameras..."); var whichCamera = CameraFacing.Back; if (this.scanningOptions.UseFrontCameraIfAvailable.HasValue && this.scanningOptions.UseFrontCameraIfAvailable.Value) { whichCamera = CameraFacing.Front; } for (int i = 0; i < numCameras; i++) { Camera.GetCameraInfo(i, camInfo); if (camInfo.Facing == whichCamera) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Found " + whichCamera + " Camera, opening..."); camera = Camera.Open(i); cameraId = i; found = true; break; } } if (!found) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Finding " + whichCamera + " camera failed, opening camera 0..."); camera = Camera.Open(0); cameraId = 0; } } else { camera = Camera.Open(); } if (camera != null) { camera.SetPreviewCallback(this); } else { MobileBarcodeScanner.LogWarn(MobileBarcodeScanner.TAG, "Camera is null :("); return; } } catch (Exception ex) { ShutdownCamera(); MobileBarcodeScanner.LogError("Setup Error: {0}", ex); return; } PerformanceCounter.Stop(perf, "Setup Camera took {0}ms"); perf = PerformanceCounter.Start(); var parameters = camera.GetParameters(); parameters.PreviewFormat = ImageFormatType.Nv21; // First try continuous video, then auto focus, then fixed var supportedFocusModes = parameters.SupportedFocusModes; if (Build.VERSION.SdkInt >= BuildVersionCodes.IceCreamSandwich && supportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture)) { parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture; } else if (supportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousVideo)) { parameters.FocusMode = Camera.Parameters.FocusModeContinuousVideo; } else if (supportedFocusModes.Contains(Camera.Parameters.FocusModeAuto)) { parameters.FocusMode = Camera.Parameters.FocusModeAuto; } else if (supportedFocusModes.Contains(Camera.Parameters.FocusModeFixed)) { parameters.FocusMode = Camera.Parameters.FocusModeFixed; } var selectedFps = parameters.SupportedPreviewFpsRange.FirstOrDefault(); if (selectedFps != null) { // This will make sure we select a range with the lowest minimum FPS // and maximum FPS which still has the lowest minimum // This should help maximize performance / support for hardware foreach (var fpsRange in parameters.SupportedPreviewFpsRange) { if (fpsRange [0] <= selectedFps [0] && fpsRange [1] > selectedFps [1]) { selectedFps = fpsRange; } } parameters.SetPreviewFpsRange(selectedFps [0], selectedFps [1]); } var availableResolutions = new List <CameraResolution> (); foreach (var sps in parameters.SupportedPreviewSizes) { availableResolutions.Add(new CameraResolution { Width = sps.Width, Height = sps.Height }); } // Try and get a desired resolution from the options selector var resolution = scanningOptions.GetResolution(availableResolutions); // If the user did not specify a resolution, let's try and find a suitable one if (resolution == null) { // Loop through all supported sizes foreach (var sps in parameters.SupportedPreviewSizes) { // Find one that's >= 640x360 but <= 1000x1000 // This will likely pick the *smallest* size in that range, which should be fine if (sps.Width >= 640 && sps.Width <= 1000 && sps.Height >= 360 && sps.Height <= 1000) { resolution = new CameraResolution { Width = sps.Width, Height = sps.Height }; break; } } } // Google Glass requires this fix to display the camera output correctly if (Build.Model.Contains("Glass")) { resolution = new CameraResolution { Width = 640, Height = 360 }; // Glass requires 30fps parameters.SetPreviewFpsRange(30000, 30000); } // Hopefully a resolution was selected at some point if (resolution != null) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Selected Resolution: " + resolution.Width + "x" + resolution.Height); parameters.SetPreviewSize(resolution.Width, resolution.Height); } camera.SetParameters(parameters); SetCameraDisplayOrientation(this.activity); camera.SetPreviewDisplay(this.Holder); camera.StartPreview(); PerformanceCounter.Stop(perf, "Setup Camera Parameters took {0}ms"); // Docs suggest if Auto or Macro modes, we should invoke AutoFocus at least once var currentFocusMode = camera.GetParameters().FocusMode; if (currentFocusMode == Camera.Parameters.FocusModeAuto || currentFocusMode == Camera.Parameters.FocusModeMacro) { AutoFocus(); } }