public void ShutdownCamera() { tokenSource.Cancel(); var theCamera = camera; camera = null; // make this asyncronous so that we can return from the view straight away instead of waiting for the camera to release. Task.Factory.StartNew(() => { try { if (theCamera != null) { try { theCamera.SetPreviewCallback(null); theCamera.StopPreview(); } catch (Exception ex) { Android.Util.Log.Error(MobileBarcodeScanner.TAG, ex.ToString()); } theCamera.Release(); } } catch (Exception e) { Android.Util.Log.Error(MobileBarcodeScanner.TAG, e.ToString()); } finally { ReleaseExclusiveAccess(); } }); }
public void OnAutoFocus(bool success, Android.Hardware.Camera camera) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "AutoFocused"); Task.Factory.StartNew(() => { int slept = 0; while (!tokenSource.IsCancellationRequested && slept < 2000) { System.Threading.Thread.Sleep(100); slept += 100; } if (!tokenSource.IsCancellationRequested) { AutoFocus(); } }); }
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 void OnPreviewFrame(byte[] bytes, Android.Hardware.Camera camera) { if (!isAnalyzing) { return; } //Check and see if we're still processing a previous frame if (processingTask != null && !processingTask.IsCompleted) { return; } if ((DateTime.UtcNow - lastPreviewAnalysis).TotalMilliseconds < scanningOptions.DelayBetweenAnalyzingFrames) { return; } // Delay a minimum between scans if (wasScanned && ((DateTime.UtcNow - lastPreviewAnalysis).TotalMilliseconds < scanningOptions.DelayBetweenContinuousScans)) { return; } wasScanned = false; var cameraParameters = camera.GetParameters(); var width = cameraParameters.PreviewSize.Width; var height = cameraParameters.PreviewSize.Height; //var img = new YuvImage(bytes, ImageFormatType.Nv21, cameraParameters.PreviewSize.Width, cameraParameters.PreviewSize.Height, null); lastPreviewAnalysis = DateTime.UtcNow; processingTask = Task.Factory.StartNew(() => { try { if (barcodeReader == null) { barcodeReader = new BarcodeReader(null, null, null, (p, w, h, f) => new PlanarYUVLuminanceSource(p, w, h, 0, 0, w, h, false)); //new PlanarYUVLuminanceSource(p, w, h, dataRect.Left, dataRect.Top, dataRect.Width(), dataRect.Height(), false)) if (this.scanningOptions.TryHarder.HasValue) { barcodeReader.Options.TryHarder = this.scanningOptions.TryHarder.Value; } if (this.scanningOptions.PureBarcode.HasValue) { barcodeReader.Options.PureBarcode = this.scanningOptions.PureBarcode.Value; } if (!string.IsNullOrEmpty(this.scanningOptions.CharacterSet)) { barcodeReader.Options.CharacterSet = this.scanningOptions.CharacterSet; } if (this.scanningOptions.TryInverted.HasValue) { barcodeReader.TryInverted = this.scanningOptions.TryInverted.Value; } if (this.scanningOptions.PossibleFormats != null && this.scanningOptions.PossibleFormats.Count > 0) { barcodeReader.Options.PossibleFormats = new List <BarcodeFormat> (); foreach (var pf in this.scanningOptions.PossibleFormats) { barcodeReader.Options.PossibleFormats.Add(pf); } } } bool rotate = false; int newWidth = width; int newHeight = height; var cDegrees = getCameraDisplayOrientation(this.activity); if (cDegrees == 90 || cDegrees == 270) { rotate = true; newWidth = height; newHeight = width; } var start = PerformanceCounter.Start(); if (rotate) { bytes = rotateCounterClockwise(bytes, width, height); } var result = barcodeReader.Decode(bytes, newWidth, newHeight, RGBLuminanceSource.BitmapFormat.Unknown); PerformanceCounter.Stop(start, "Decode Time: {0} ms (width: " + width + ", height: " + height + ", degrees: " + cDegrees + ", rotate: " + rotate + ")"); if (result == null || string.IsNullOrEmpty(result.Text)) { return; } Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Barcode Found: " + result.Text); wasScanned = true; callback(result); } catch (ReaderException) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "No barcode Found"); // ignore this exception; it happens every time there is a failed scan } catch (Exception) { // TODO: this one is unexpected.. log or otherwise handle it throw; } }); }
public void StartScanning(Action <Result> scanResultCallback, MobileBarcodeScanningOptions options = null) { this.callback = scanResultCallback; this.options = options ?? MobileBarcodeScanningOptions.Default; lastPreviewAnalysis = DateTime.UtcNow.AddMilliseconds(options.InitialDelayBeforeAnalyzingFrames); isAnalyzing = true; Console.WriteLine("StartScanning"); CheckPermissions(); 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 = Android.Hardware.Camera.NumberOfCameras; var camInfo = new Android.Hardware.Camera.CameraInfo(); var found = false; Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Found " + numCameras + " cameras..."); var whichCamera = CameraFacing.Back; if (options.UseFrontCameraIfAvailable.HasValue && options.UseFrontCameraIfAvailable.Value) { whichCamera = CameraFacing.Front; } for (int i = 0; i < numCameras; i++) { Android.Hardware.Camera.GetCameraInfo(i, camInfo); if (camInfo.Facing == whichCamera) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Found " + whichCamera + " Camera, opening..."); camera = Android.Hardware.Camera.Open(i); cameraId = i; found = true; break; } } if (!found) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Finding " + whichCamera + " camera failed, opening camera 0..."); camera = Android.Hardware.Camera.Open(0); cameraId = 0; } } else { camera = Android.Hardware.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, "SurfaceCreated took {0}ms"); }
public void StartScanning (Action<Result> scanResultCallback, MobileBarcodeScanningOptions options = null) { this.callback = scanResultCallback; this.options = options ?? MobileBarcodeScanningOptions.Default; lastPreviewAnalysis = DateTime.UtcNow.AddMilliseconds(options.InitialDelayBeforeAnalyzingFrames); isAnalyzing = true; Console.WriteLine ("StartScanning"); CheckPermissions (); 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 = Android.Hardware.Camera.NumberOfCameras; var camInfo = new Android.Hardware.Camera.CameraInfo(); var found = false; Android.Util.Log.Debug (MobileBarcodeScanner.TAG, "Found " + numCameras + " cameras..."); var whichCamera = CameraFacing.Back; if (options.UseFrontCameraIfAvailable.HasValue && options.UseFrontCameraIfAvailable.Value) whichCamera = CameraFacing.Front; for (int i = 0; i < numCameras; i++) { Android.Hardware.Camera.GetCameraInfo(i, camInfo); if (camInfo.Facing == whichCamera) { Android.Util.Log.Debug (MobileBarcodeScanner.TAG, "Found " + whichCamera + " Camera, opening..."); camera = Android.Hardware.Camera.Open(i); cameraId = i; found = true; break; } } if (!found) { Android.Util.Log.Debug(MobileBarcodeScanner.TAG, "Finding " + whichCamera + " camera failed, opening camera 0..."); camera = Android.Hardware.Camera.Open(0); cameraId = 0; } } else { camera = Android.Hardware.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, "SurfaceCreated took {0}ms"); }
public void ShutdownCamera () { tokenSource.Cancel(); var theCamera = camera; camera = null; // make this asyncronous so that we can return from the view straight away instead of waiting for the camera to release. Task.Factory.StartNew(() => { try { if (theCamera != null) { try { theCamera.SetPreviewCallback(null); theCamera.StopPreview(); } catch (Exception ex) { Android.Util.Log.Error (MobileBarcodeScanner.TAG, ex.ToString ()); } theCamera.Release(); } } catch (Exception e) { Android.Util.Log.Error(MobileBarcodeScanner.TAG, e.ToString()); } finally { ReleaseExclusiveAccess(); } }); }