private void UpdateCameraAspect() { try { Camera.Parameters camParams = camera.GetParameters(); Camera.CameraInfo info = new Camera.CameraInfo(); Camera.GetCameraInfo((int)Android.Hardware.CameraFacing.Back, info); Camera.Size size = GetOptimalPreviewSize(camParams.SupportedPreviewSizes, width, height); camParams.SetPreviewSize(size.Width, size.Height); int rotation = (info.Orientation + 360) % 360; camParams.SetRotation(rotation); if (camParams.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture)) { camParams.FocusMode = Camera.Parameters.FocusModeContinuousPicture; } camera.SetParameters(camParams); } catch (Exception e) { Console.WriteLine(e.Message); } }
private int GetCameraDisplayOrientation() { int degrees; var windowManager = _context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>(); var display = windowManager.DefaultDisplay; var rotation = display.Rotation; if (ZxingActivity.ScanningOptions.AndroidOptions?.ModifyCameraDisplayOrientationDelegate != null) { degrees = ZxingActivity.ScanningOptions.AndroidOptions.ModifyCameraDisplayOrientationDelegate.Invoke((int)rotation); } else { switch (rotation) { case SurfaceOrientation.Rotation0: degrees = 0; break; case SurfaceOrientation.Rotation90: degrees = 90; break; case SurfaceOrientation.Rotation180: degrees = 180; break; case SurfaceOrientation.Rotation270: degrees = 270; break; default: throw new ArgumentOutOfRangeException(); } } var info = new Camera.CameraInfo(); Camera.GetCameraInfo(_cameraId, info); int correctedDegrees; if (info.Facing == CameraFacing.Front) { correctedDegrees = (info.Orientation + degrees) % 360; correctedDegrees = (360 - correctedDegrees) % 360; // compensate the mirror } else { // back-facing correctedDegrees = (info.Orientation - degrees + 360) % 360; } return(correctedDegrees); }
// Populate |cameraOrientations| with the first cameras that have each of // the facing values. private void populateCameraOrientations() { Camera.CameraInfo info = new Camera.CameraInfo(); for (int i = 0; i < Camera.NumberOfCameras; ++i) { Camera.GetCameraInfo(i, info); if (cameraOrientations[(int)info.Facing] != -1) { continue; } cameraOrientations[(int)info.Facing] = info.Orientation; } }
private void OpenCamera() { try { var version = Build.VERSION.SdkInt; if (version >= BuildVersionCodes.Gingerbread) { System.Diagnostics.Debug.WriteLine("Checking Number of cameras..."); var numCameras = Camera.NumberOfCameras; var camInfo = new Camera.CameraInfo(); var found = false; System.Diagnostics.Debug.WriteLine("Found " + numCameras + " cameras..."); var whichCamera = CameraFacing.Back; for (var i = 0; i < numCameras; i++) { Camera.GetCameraInfo(i, camInfo); if (camInfo.Facing == whichCamera) { System.Diagnostics.Debug.WriteLine( "Found " + whichCamera + " Camera, opening..."); camera = Camera.Open(i); cameraId = i; found = true; break; } } if (!found) { System.Diagnostics.Debug.WriteLine( "Finding " + whichCamera + " camera failed, opening camera 0..."); camera = Camera.Open(0); cameraId = 0; } } else { camera = Camera.Open(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Setup Error: {ex}"); } }
private void OpenCamera(CameraFacing whichCamera) { try { var version = Build.VERSION.SdkInt; if (version >= BuildVersionCodes.Gingerbread) { var numCameras = Camera.NumberOfCameras; var camInfo = new Camera.CameraInfo(); var found = false; for (var i = 0; i < numCameras; i++) { Camera.GetCameraInfo(i, camInfo); if (camInfo.Facing == whichCamera) { Camera = Camera.Open(i); _cameraId = i; found = true; break; } } if (!found) { Camera = Camera.Open(0); _cameraId = 0; } } else { Camera = Camera.Open(); } //if (Camera != null) // Camera.SetPreviewCallback(_cameraEventListener); //else // MobileBarcodeScanner.LogWarn(MobileBarcodeScanner.TAG, "Camera is null :("); } catch (Exception ex) { ShutdownCamera(); // MobileBarcodeScanner.LogError("Setup Error: {0}", ex); } }
int GetCameraDisplayOrientation() { int degrees; var windowManager = context.GetSystemService(Context.WindowService).JavaCast <IWindowManager>(); var display = windowManager.DefaultDisplay; var rotation = display.Rotation; switch (rotation) { case SurfaceOrientation.Rotation0: degrees = 0; break; case SurfaceOrientation.Rotation90: degrees = 90; break; case SurfaceOrientation.Rotation180: degrees = 180; break; case SurfaceOrientation.Rotation270: degrees = 270; break; default: throw new ArgumentOutOfRangeException(); } var info = new Camera.CameraInfo(); Camera.GetCameraInfo(cameraId, info); int correctedDegrees; if (info.Facing == CameraFacing.Front) { correctedDegrees = (info.Orientation + degrees) % 360; correctedDegrees = (360 - correctedDegrees) % 360; // compensate the mirror } else { // back-facing correctedDegrees = (info.Orientation - degrees + 360) % 360; } return(correctedDegrees); }
private void OnOrientationChanged(int orientation) { if (_camera == null) { return; } var info = new Camera.CameraInfo(); Camera.GetCameraInfo(_cameraId, info); orientation = (orientation + 45) / 90 * 90; var rotation = info.Facing == Camera.CameraInfo.CameraFacingFront ? (info.Orientation - orientation + 360) % 360 : (info.Orientation + orientation) % 360; _currentRotation = rotation; }
private int FindBackFacingCamera() { var cameraId = 0; // Search for the front facing camera int numberOfCameras = Camera.NumberOfCameras; for (int i = 0; i < numberOfCameras; i++) { var info = new Camera.CameraInfo(); Camera.GetCameraInfo(i, info); if (info.Facing == Camera.CameraInfo.CameraFacingBack) { cameraId = i; break; } } return(cameraId); }
int getCameraDisplayOrientation(Activity context) { var degrees = 0; var display = context.WindowManager.DefaultDisplay; var rotation = display.Rotation; switch (rotation) { case SurfaceOrientation.Rotation0: degrees = 0; break; case SurfaceOrientation.Rotation90: degrees = 90; break; case SurfaceOrientation.Rotation180: degrees = 180; break; case SurfaceOrientation.Rotation270: degrees = 270; break; } var info = new Camera.CameraInfo(); Camera.GetCameraInfo(cameraId, info); int correctedDegrees; if (info.Facing == CameraFacing.Front) { correctedDegrees = (info.Orientation + degrees) % 360; correctedDegrees = (360 - correctedDegrees) % 360; // compensate the mirror } else { // back-facing correctedDegrees = (info.Orientation - degrees + 360) % 360; } return(correctedDegrees); }
int GetCorrectedCameraDisplayDegrees(int degrees) { var info = new Camera.CameraInfo(); Camera.GetCameraInfo(cameraId, info); int correctedDegrees; if (info.Facing == CameraFacing.Front) { correctedDegrees = (info.Orientation + degrees) % 360; correctedDegrees = (360 - correctedDegrees) % 360; // compensate the mirror } else { // back-facing correctedDegrees = (info.Orientation - degrees + 360) % 360; } return(correctedDegrees); }
private void prepareCamera(int encWidth, int encHeight) { if (_camera != null) { throw new RuntimeException("camera already initialized"); } Camera.CameraInfo info = new Camera.CameraInfo(); // Try to find a front-facing camera (e.g. for videoconferencing). int numCameras = Camera.NumberOfCameras; for (int i = 0; i < numCameras; i++) { Camera.GetCameraInfo(i, info); if (info.Facing == Camera.CameraInfo.CameraFacingFront) { _camera = Camera.Open(i); break; } } if (_camera == null) { Log.Debug(TAG, "No front-facing camera found; opening default"); _camera = Camera.Open(); // opens first back-facing camera } if (_camera == null) { throw new RuntimeException("Unable to open camera"); } Camera.Parameters parms = _camera.GetParameters(); choosePreviewSize(parms, encWidth, encHeight); // leave the frame rate set to default _camera.SetParameters(parms); Camera.Size size = parms.PreviewSize; Log.Debug(TAG, "Camera preview size is " + size.Width + "x" + size.Height); }
public override void OnCreate (Bundle savedInstanceState) { base.OnCreate (savedInstanceState); // Create a RelativeLayout container that will hold a SurfaceView, // and set it as the content of our activity. mPreview = new Preview (this.Activity); // Find the total number of cameras available mNumberOfCameras = Camera.NumberOfCameras; // Find the ID of the default camera Camera.CameraInfo cameraInfo = new Camera.CameraInfo (); for (int i = 0; i < mNumberOfCameras; i++) { Camera.GetCameraInfo (i, cameraInfo); if (cameraInfo.Facing == Camera.CameraInfo.CameraFacingBack) { mDefaultCameraId = i; } } SetHasOptionsMenu (mNumberOfCameras > 1); }
int getCameraDisplayOrientation(Activity context) { var degrees = 0; var display = context.WindowManager.DefaultDisplay; var rotation = display.Rotation; switch (rotation) { case SurfaceOrientation.Rotation0: degrees = 0; break; case SurfaceOrientation.Rotation90: degrees = 90; break; case SurfaceOrientation.Rotation180: degrees = 180; break; case SurfaceOrientation.Rotation270: degrees = 270; break; } Camera.CameraInfo info = new Camera.CameraInfo(); Camera.GetCameraInfo(cameraId, info); int correctedDegrees = (360 + info.Orientation - degrees) % 360; return(correctedDegrees); }
public override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Create a RelativeLayout container that will hold a SurfaceView, // and set it as the content of our activity. mPreview = new Preview(this.Activity); // Find the total number of cameras available mNumberOfCameras = Camera.NumberOfCameras; // Find the ID of the default camera Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int i = 0; i < mNumberOfCameras; i++) { Camera.GetCameraInfo(i, cameraInfo); if (cameraInfo.Facing == Camera.CameraInfo.CameraFacingBack) { mDefaultCameraId = i; } } SetHasOptionsMenu(mNumberOfCameras > 1); }
//int cameraDegrees = 0; int getCameraDisplayOrientation(Activity context) { var degrees = 0; var display = context.WindowManager.DefaultDisplay; var rotation = display.Rotation; switch(rotation) { case SurfaceOrientation.Rotation0: degrees = 0; break; case SurfaceOrientation.Rotation90: degrees = 90; break; case SurfaceOrientation.Rotation180: degrees = 180; break; case SurfaceOrientation.Rotation270: degrees = 270; break; } Camera.CameraInfo info = new Camera.CameraInfo(); Camera.GetCameraInfo(cameraId, info); int correctedDegrees = (360 + info.Orientation - degrees) % 360; return correctedDegrees; }
private int GetCameraDisplayOrientation() { int degrees; var windowManager = _context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>(); var display = windowManager.DefaultDisplay; var rotation = display.Rotation; switch (rotation) { case SurfaceOrientation.Rotation0: degrees = 0; break; case SurfaceOrientation.Rotation90: degrees = 90; break; case SurfaceOrientation.Rotation180: degrees = 180; break; case SurfaceOrientation.Rotation270: degrees = 270; break; default: throw new ArgumentOutOfRangeException(); } var info = new Camera.CameraInfo(); Camera.GetCameraInfo(_cameraId, info); int correctedDegrees; if (info.Facing == CameraFacing.Front) { correctedDegrees = (info.Orientation + degrees)%360; correctedDegrees = (360 - correctedDegrees)%360; // compensate the mirror } else { // back-facing correctedDegrees = (info.Orientation - degrees + 360)%360; } return correctedDegrees; }
private void OpenCamera() { 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 (_scanningOptions.UseFrontCameraIfAvailable.HasValue && _scanningOptions.UseFrontCameraIfAvailable.Value) whichCamera = CameraFacing.Front; for (var 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(_cameraEventListener); else MobileBarcodeScanner.LogWarn(MobileBarcodeScanner.TAG, "Camera is null :("); } catch (Exception ex) { ShutdownCamera(); MobileBarcodeScanner.LogError("Setup Error: {0}", ex); } }
/** * Gets the current screen rotation in order to understand how much * the surface needs to be rotated */ private void UpdateCameraDisplayOrientation() { int cameraID = _cameraId; if (_camera == null) { Console.WriteLine("updateCameraDisplayOrientation(): warning, camera is null"); return; } int result = 0; MainActivity parentActivity = (MainActivity)Context; int rotation = (int)parentActivity.Window.WindowManager.DefaultDisplay.Rotation; int degrees = 0; switch (rotation) { case (int)SurfaceOrientation.Rotation0: degrees = 0; break; case (int)SurfaceOrientation.Rotation90: degrees = 90; break; case (int)SurfaceOrientation.Rotation180: degrees = 180; break; case (int)SurfaceOrientation.Rotation270: degrees = 270; break; } if ((int)Build.VERSION.SdkInt >= 9) { // on >= API 9 we can proceed with the CameraInfo method // and also we have to keep in mind that the camera could be the front one Camera.CameraInfo info = new Camera.CameraInfo(); Camera.GetCameraInfo(cameraID, info); if (info.Facing == Camera.CameraInfo.CameraFacingFront) { result = (info.Orientation + degrees) % 360; result = (360 - result) % 360; // compensate the mirror } else { // back-facing result = (info.Orientation - degrees + 360) % 360; } } else { // TODO: on the majority of API 8 devices, this trick works good // and doesn't produce an upside-down preview. // ... but there is a small amount of devices that don't like it! result = Math.Abs(degrees - 90); } _camera.SetDisplayOrientation(result); // save settings }
int getCameraDisplayOrientation (Activity context) { var degrees = 0; var display = context.WindowManager.DefaultDisplay; var rotation = display.Rotation; switch (rotation) { case SurfaceOrientation.Rotation0: degrees = 0; break; case SurfaceOrientation.Rotation90: degrees = 90; break; case SurfaceOrientation.Rotation180: degrees = 180; break; case SurfaceOrientation.Rotation270: degrees = 270; break; } var info = new Camera.CameraInfo (); Camera.GetCameraInfo (cameraId, info); int correctedDegrees; if (info.Facing == CameraFacing.Front) { correctedDegrees = (info.Orientation + degrees) % 360; correctedDegrees = (360 - correctedDegrees) % 360; // compensate the mirror } else { // back-facing correctedDegrees = (info.Orientation - degrees + 360) % 360; } return correctedDegrees; }
private void SetupAndStartCamera(SurfaceTexture surface = null) { try { if (_isSurfaceAvailable) { if (_camera == null) { _camera = Camera.Open((int)_cameraType); _camera.SetErrorCallback(this); for (var ii = 0; ii < Camera.NumberOfCameras - 1; ii++) { var info = new Camera.CameraInfo(); Camera.GetCameraInfo(ii, info); if (info.CanDisableShutterSound) { _camera.EnableShutterSound(false); } } var parameters = _camera.GetParameters(); parameters.FlashMode = Camera.Parameters.FlashModeOff; parameters.VideoStabilization = false; parameters.JpegQuality = 100; TurnOnContinuousFocus(parameters); var landscapePictureDescendingSizes = parameters .SupportedPictureSizes .Where(p => p.Width > p.Height) .OrderByDescending(p => p.Width * p.Height) .ToList(); var landscapePreviewDescendingSizes = parameters .SupportedPreviewSizes .Where(p => p.Width > p.Height) .OrderByDescending(p => p.Width * p.Height) .ToList(); foreach (var pictureSize in landscapePictureDescendingSizes) { foreach (var previewSize in landscapePreviewDescendingSizes) { if (Math.Abs((double)pictureSize.Width / pictureSize.Height - (double)previewSize.Width / previewSize.Height) < 0.0001) { _pictureSize = pictureSize; _previewSize = previewSize; break; } } if (_pictureSize != null) { break; } } if (_pictureSize == null || _previewSize == null) { _pictureSize = landscapePictureDescendingSizes.First(); _previewSize = landscapePreviewDescendingSizes.First(); } parameters.SetPictureSize(_pictureSize.Width, _pictureSize.Height); parameters.SetPreviewSize(_previewSize.Width, _previewSize.Height); _camera.SetParameters(parameters); _camera.SetPreviewTexture(_surfaceTexture); } if (surface != null) { _surfaceTexture = surface; } SetOrientation(); StartCamera(); } } catch (Exception e) { _cameraModule.ErrorMessage = e.ToString(); } }
public void StartScanning (Action<Result> scanResultCallback, MobileBarcodeScanningOptions options = null) { this.callback = scanResultCallback; this.scanningOptions = options ?? MobileBarcodeScanningOptions.Default; lastPreviewAnalysis = DateTime.UtcNow.AddMilliseconds (this.scanningOptions.InitialDelayBeforeAnalyzingFrames); isAnalyzing = true; Console.WriteLine ("StartScanning"); 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) Android.Util.Log.Debug (MobileBarcodeScanner.TAG, "Camera is null :("); camera.SetPreviewCallback (this); } catch (Exception ex) { ShutdownCamera (); Console.WriteLine ("Setup Error: " + ex); } PerformanceCounter.Stop (perf, "Camera took {0}ms"); }
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(); } }
private void SetCameraDisplayOrientation(int cameraId) { var rotation = Activity.WindowManager.DefaultDisplay.Rotation; var degrees = 0; switch (rotation) { case SurfaceOrientation.Rotation0: degrees = 0; break; case SurfaceOrientation.Rotation90: degrees = 90; break; case SurfaceOrientation.Rotation180: degrees = 180; break; case SurfaceOrientation.Rotation270: degrees = 270; break; } var result = 0; var info = new Camera.CameraInfo(); Camera.GetCameraInfo(cameraId, info); if (info.Facing == CameraFacing.Back) { result = 360 - degrees + info.Orientation; } else if (info.Facing == CameraFacing.Front) { result = 360 - degrees - info.Orientation; result += 360; } result = result % 360; _camera.SetDisplayOrientation(result); var parameters = _camera.GetParameters(); parameters.PictureFormat = ImageFormat.Jpeg; parameters.JpegQuality = 90; if (parameters.SupportedFocusModes != null && parameters.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousVideo)) { parameters.FocusMode = Camera.Parameters.FocusModeContinuousVideo; } if (parameters.SupportedFlashModes != null && parameters.SupportedFlashModes.Contains(Camera.Parameters.FlashModeOff)) { parameters.FlashMode = Camera.Parameters.FlashModeOff; } if (parameters.SupportedSceneModes != null && parameters.SupportedSceneModes.Contains(Camera.Parameters.SceneModeAuto)) { parameters.SceneMode = Camera.Parameters.SceneModeAuto; } if (parameters.SupportedWhiteBalance != null && parameters.SupportedWhiteBalance.Contains(Camera.Parameters.WhiteBalanceAuto)) { parameters.WhiteBalance = Camera.Parameters.WhiteBalanceAuto; } _camera.SetParameters(parameters); }
public async Task StartScanningAsync (Action<Result> scanResultCallback, MobileBarcodeScanningOptions options = null) { this.callback = scanResultCallback; this.scanningOptions = options ?? MobileBarcodeScanningOptions.Default; lastPreviewAnalysis = DateTime.UtcNow.AddMilliseconds (this.scanningOptions.InitialDelayBeforeAnalyzingFrames); isAnalyzing = true; Console.WriteLine ("StartScanning"); 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) Android.Util.Log.Debug (MobileBarcodeScanner.TAG, "Camera is null :("); camera.SetPreviewCallback (this); } catch (Exception ex) { ShutdownCamera (); Console.WriteLine ("Setup Error: " + ex); } PerformanceCounter.Stop (perf, "Camera took {0}ms"); if (!surfaceChanged) await waitSurface.WaitAsync (); if (camera == null) return; perf = PerformanceCounter.Start (); var parameters = camera.GetParameters (); parameters.PreviewFormat = ImageFormatType.Nv21; 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, "SurfaceChanged took {0}ms"); AutoFocus (); }
private void OpenCamera() { 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 (_scannerHost.ScanningOptions.UseFrontCameraIfAvailable.HasValue && _scannerHost.ScanningOptions.UseFrontCameraIfAvailable.Value) { whichCamera = CameraFacing.Front; } for (var 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(_cameraEventListener); //else // MobileBarcodeScanner.LogWarn(MobileBarcodeScanner.TAG, "Camera is null :("); } catch (Exception ex) { ShutdownCamera(); MobileBarcodeScanner.LogError("Setup Error: {0}", ex); } }
void OnSurfaceCreated() { surfaceCreated = true; lastPreviewAnalysis = DateTime.UtcNow.AddMilliseconds(this.scanningOptions.InitialDelayBeforeAnalyzingFrames); isAnalyzing = true; Console.WriteLine("StartScanning"); 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) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Camera is null :("); } camera.SetPreviewCallback(this); } catch (Exception ex) { ShutdownCamera(); Console.WriteLine("Setup Error: " + ex); } PerformanceCounter.Stop(perf, "Camera took {0}ms"); }
public async Task StartScanningAsync(Action <Result> scanResultCallback, MobileBarcodeScanningOptions options = null) { this.callback = scanResultCallback; this.scanningOptions = options ?? MobileBarcodeScanningOptions.Default; lastPreviewAnalysis = DateTime.UtcNow.AddMilliseconds(this.scanningOptions.InitialDelayBeforeAnalyzingFrames); isAnalyzing = true; Console.WriteLine("StartScanning"); 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) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Camera is null :("); } camera.SetPreviewCallback(this); } catch (Exception ex) { ShutdownCamera(); Console.WriteLine("Setup Error: " + ex); } PerformanceCounter.Stop(perf, "Camera took {0}ms"); if (!surfaceChanged) { await waitSurface.WaitAsync(); } if (camera == null) { return; } perf = PerformanceCounter.Start(); var parameters = camera.GetParameters(); parameters.PreviewFormat = ImageFormatType.Nv21; 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, "SurfaceChanged took {0}ms"); AutoFocus(); }
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 (supportedFocusModes.Contains (Camera.Parameters.FocusModeAuto)) parameters.FocusMode = Camera.Parameters.FocusModeAuto; else if (supportedFocusModes.Contains (Camera.Parameters.FocusModeContinuousVideo)) parameters.FocusMode = Camera.Parameters.FocusModeContinuousVideo; 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 (); }