/// <summary> /// Turn the lamp off /// </summary> public void TurnOff() { if (camera == null) { camera = Camera.Open(); } if (camera == null) { Debug.WriteLine("Camera failed to initialize"); return; } var p = camera.GetParameters(); var supportedFlashModes = p.SupportedFlashModes; if (supportedFlashModes == null) { supportedFlashModes = new List <string>(); } var flashMode = string.Empty; if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeTorch)) { flashMode = Android.Hardware.Camera.Parameters.FlashModeOff; } if (!string.IsNullOrEmpty(flashMode)) { p.FlashMode = flashMode; camera.SetParameters(p); } }
private void ToggleFlashButtonTapped(object sender, EventArgs e) { flashOn = !flashOn; if (flashOn) { if (cameraType == CameraFacing.Back) { toggleFlashButton.SetBackgroundResource(Resource.Drawable.FlashButton); cameraType = CameraFacing.Back; camera.StopPreview(); camera.Release(); camera = Camera.Open((int)cameraType); var parameters = camera.GetParameters(); parameters.FlashMode = Camera.Parameters.FlashModeTorch; camera.SetParameters(parameters); camera.SetPreviewTexture(surfaceTexture); PrepareAndStartCamera(); } } else { toggleFlashButton.SetBackgroundResource(Resource.Drawable.NoFlashButton); camera.StopPreview(); camera.Release(); camera = Camera.Open((int)cameraType); var parameters = camera.GetParameters(); parameters.FlashMode = Camera.Parameters.FlashModeOff; camera.SetParameters(parameters); camera.SetPreviewTexture(surfaceTexture); PrepareAndStartCamera(); } }
private async void EnableCamera(int cameraToSwitch) { try { _camera = Camera.Open(cameraToSwitch); _cameraId = cameraToSwitch; SetPreviewSize(FullScreen); SetCameraDisplayOrientation(_cameraId); _camera.SetPreviewDisplay(_holder); _camera.StartPreview(); } catch (Exception ex) { if (ex.GetType() == typeof(Java.Lang.RuntimeException) && ex.Message == "Fail to connect to camera service") { Activity.Finish(); } else { await AppSettings.Logger.Error(ex); Activity.ShowAlert(new InternalException(LocalizationKeys.CameraSettingError, ex), ToastLength.Short); } } }
public void OpenCamera() { if (MyCamera != null) { MyCamera.StopPreview(); MyCamera.Release(); MyCamera = null; } if (_cameraId >= 0) { Camera.GetCameraInfo(_cameraId, _cameraInfo); MyCamera = Camera.Open(_cameraId); Camera.Parameters param = MyCamera.GetParameters(); param.SetRotation(0); MyCamera.SetParameters(param); try { if (_surfaceTexture != null) { MyCamera.SetPreviewTexture(_surfaceTexture); MyCamera.StartPreview(); } } catch (Exception) { } } UpdateRotation(); }
public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { camera = Camera.Open((int)cameraType); textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height); surfaceTexture = surface; camera.SetPreviewTexture(surface); PrepareAndStartCamera(); }
/// <summary> /// Turn the lamp on /// </summary> public void TurnOn() { // Additional information about using the camera light here: // http://forums.xamarin.com/discussion/24237/camera-led-or-flash // http://stackoverflow.com/questions/5503480/use-camera-flashlight-in-android?rq=1 if (camera == null) { camera = Camera.Open(); } if (camera == null) { Debug.WriteLine("Camera failed to initialize"); return; } var p = camera.GetParameters(); var supportedFlashModes = p.SupportedFlashModes; if (supportedFlashModes == null) { supportedFlashModes = new List <string>(); } var flashMode = string.Empty; if (supportedFlashModes.Contains(Android.Hardware.Camera.Parameters.FlashModeTorch)) { flashMode = Android.Hardware.Camera.Parameters.FlashModeTorch; } if (!string.IsNullOrEmpty(flashMode)) { p.FlashMode = flashMode; camera.SetParameters(p); } camera.StartPreview(); // nexus 5 fix here: http://stackoverflow.com/questions/21417332/nexus-5-4-4-2-flashlight-led-not-turning-on try { camera.SetPreviewTexture(new SurfaceTexture(0)); } catch (IOException ex) { // Ignore } }
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}"); } }
/// <summary> /// Events when surfacetexture is available, sets camera parameters /// </summary> /// <param name="surface">Surface</param> /// <param name="width">Width</param> /// <param name="height">Height</param> public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { #pragma warning disable 618 camera = Camera.Open((int)cameraType); var parameters = camera.GetParameters(); if (parameters.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture)) { parameters.FocusMode = Camera.Parameters.FocusModeContinuousPicture; } camera.SetParameters(parameters); #pragma warning restore 618 textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height); camera.SetPreviewTexture(surface); PrepareAndStartCamera(); }
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); } }
public BarcodeScannerRenderer(Context context) : base(context) { cameraInfo = new Camera.CameraInfo(); Camera.GetCameraInfo(0, cameraInfo); cam = Camera.Open(0); parameters = cam.GetParameters(); Camera.Size size = parameters.PictureSize; cheight = size.Height; cwidth = size.Width; maxZoom = parameters.MaxZoom; cam.SetPreviewCallback(this); }
public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int w, int h) { _camera = Camera.Open(); try { _camera.SetPreviewTexture(surface); _camera.SetDisplayOrientation(90); _camera.StartPreview(); } catch (Java.IO.IOException ex) { Console.WriteLine(ex.Message); } Camera.Parameters tmp = _camera.GetParameters(); tmp.FocusMode = Camera.Parameters.FocusModeContinuousPicture; _camera.SetParameters(tmp); DrawRectangle(); }
public static void Start(SurfaceTexture Surface, int Width, int Height) { if (Cam == null) { Cam = Camera.Open(); } Camera.Parameters Parms = Cam.GetParameters(); MaxZoom = Parms.MaxZoom; if (Parms.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture)) { Parms.FocusMode = Camera.Parameters.FocusModeContinuousPicture; Cam.SetParameters(Parms); } Cam.SetPreviewTexture(Surface); Cam.SetDisplayOrientation(90); StartPreview(); SetVertical(); }
private void SwitchCameraButtonTapped(object sender, EventArgs e) { if (cameraType == CameraFacing.Front) { cameraType = CameraFacing.Back; camera.StopPreview(); camera.Release(); camera = Camera.Open((int)cameraType); camera.SetPreviewTexture(surfaceTexture); PrepareAndStartCamera(); } else { cameraType = CameraFacing.Front; camera.StopPreview(); camera.Release(); camera = Camera.Open((int)cameraType); camera.SetPreviewTexture(surfaceTexture); PrepareAndStartCamera(); } }
private void StartCamPrep() { camera = Camera.Open((int)cameraType); camEnabled = true; textureView.LayoutParameters = new FrameLayout.LayoutParams(width, height, GravityFlags.Center); if (cameraType == CameraFacing.Back) { previousPhoto.LayoutParameters = new FrameLayout.LayoutParams(width, height, GravityFlags.Fill); } else { previousPhoto.LayoutParameters = new FrameLayout.LayoutParams(width, height, GravityFlags.Top); } surfaceTexture = surface; camera.SetPreviewTexture(surface); takePhotoButton.Enabled = true; switchCameraButton.Enabled = true; PrepareAndStartCamera(); }
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); } }
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(); } }
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"); }
protected override void OnResume() { base.OnResume(); camera = Camera.Open(); }
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(); } }