Esempio n. 1
0
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            CheckPermissions();

            try
            {
                var version = Android.OS.Build.VERSION.SdkInt;

                if (version >= BuildVersionCodes.Gingerbread)
                {
                    var numCameras = Android.Hardware.Camera.NumberOfCameras;
                    var camInfo    = new Android.Hardware.Camera.CameraInfo();
                    var found      = false;

                    for (int i = 0; i < numCameras; i++)
                    {
                        Android.Hardware.Camera.GetCameraInfo(i, camInfo);
                        if (camInfo.Facing == CameraFacing.Back)
                        {
                            camera = Android.Hardware.Camera.Open(i);
                            found  = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        Android.Util.Log.Debug("ZXing.Net.Mobile", "Finding rear camera failed, opening camera 0...");
                        camera = Android.Hardware.Camera.Open(0);
                    }
                }
                else
                {
                    camera = Android.Hardware.Camera.Open();
                }
                if (camera == null)
                {
                    Android.Util.Log.Debug("ZXing.Net.Mobile", "Camera is null :(");
                }


                //camera = Android.Hardware.Camera.Open ();
                camera.SetPreviewDisplay(holder);
                camera.SetPreviewCallback(this);
            } catch (Exception ex) {
                ShutdownCamera();

                // TODO: log or otherwise handle this exception
                Console.WriteLine("Setup Error: " + ex);
                //throw;
            }
        }
Esempio n. 2
0
 public void SetupLiveCameraStream()
 {
     lock (lockObj)
     {
         if (surface == null)
         {
             return;
         }
         try
         {
             camera = Android.Hardware.Camera.Open(1);
             camera.SetDisplayOrientation(90);
             var cameraParams = camera.GetParameters();
             foreach (Android.Hardware.Camera.Size size in cameraParams.SupportedPreviewSizes)
             {
                 if (size.Width <= 176)
                 {
                     cameraParams.SetPreviewSize(size.Width, size.Height);
                 }
             }
             camera.SetParameters(cameraParams);
             camera.SetPreviewTexture(surface);
             camera.SetPreviewCallback(this);
             camera.StartPreview();
         }
         catch (Java.IO.IOException ex)
         {
         }
     }
 }
        public void ShutdownCamera()
        {
            if (Camera == null)
            {
                return;
            }

            // camera release logic takes about 0.005 sec so there is no need in async releasing
            var perf = PerformanceCounter.Start();

            try
            {
                try
                {
                    Camera.SetPreviewCallback(null);
                    Camera.SetPreviewDisplay(null);
                    Camera.StopPreview();
                }
                catch (Exception ex)
                {
                    Android.Util.Log.Error(MobileBarcodeScanner.TAG, ex.ToString());
                }
                Camera.Release();
                Camera = null;
            }
            catch (Exception e)
            {
                Android.Util.Log.Error(MobileBarcodeScanner.TAG, e.ToString());
            }

            PerformanceCounter.Stop(perf, "Shutdown camera took {0}ms");
        }
Esempio n. 4
0
        public bool OnSurfaceTextureDestroyed(SurfaceTexture surface)
        {
            camera.StopPreview();
            camera.SetPreviewCallback(null);
            camera.Release();

            return(true);
        }
Esempio n. 5
0
        public void ShutdownCamera()
        {
            tokenSource.Cancel();

            if (camera != null)
            {
                camera.SetPreviewCallback(null);
                camera.StopPreview();
                camera.Release();
                camera = null;
            }
        }
Esempio n. 6
0
        private void StartCamera()
        {
            holder = view.Holder;
            holder.AddCallback(this);

            camera = Android.Hardware.Camera.Open(1);
            Android.Hardware.Camera.Parameters p = camera.GetParameters();
            p.SetPreviewSize(320, 240);

            camera.SetDisplayOrientation(90);
            camera.SetParameters(p);
            camera.SetPreviewCallback(this);
        }
Esempio n. 7
0
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            try {
                camera = Android.Hardware.Camera.Open();
                camera.SetPreviewDisplay(holder);
                camera.SetPreviewCallback(this);
            } catch (Exception) {
                ShutdownCamera();

                // TODO: log or otherwise handle this exception

                //throw;
            }
        }
Esempio n. 8
0
			public void SurfaceCreated (ISurfaceHolder holder)
			{
				try {
					camera = Camera.Open ();
					camera.SetPreviewDisplay (holder);
					camera.SetPreviewCallback (this);

				} catch (Exception e) {
					ShutdownCamera ();

					// TODO: log or otherwise handle this exception

					throw;
				}
			}
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
 public void SurfaceCreated(ISurfaceHolder holder)
 {
     try
     {
         camera.SetPreviewDisplay(holder);
         camera.SetDisplayOrientation(90);
         camera.SetPreviewCallback(new MyPreviewCallback()
         {
             v = output
         });
         camera.StartPreview();
     }
     catch (Java.IO.IOException e)
     {
         throw e;
     }
 }
Esempio n. 11
0
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            try
            {
#if __ANDROID_9__
                var numCameras = Android.Hardware.Camera.NumberOfCameras;
                var camInfo    = new Android.Hardware.Camera.CameraInfo();
                var found      = false;

                for (int i = 0; i < numCameras; i++)
                {
                    Android.Hardware.Camera.GetCameraInfo(i, camInfo);
                    if (camInfo.Facing == CameraFacing.Back)
                    {
                        camera = Android.Hardware.Camera.Open(i);
                        found  = true;
                        break;
                    }
                }

                if (!found)
                {
                    Android.Util.Log.Debug("ZXing.Net.Mobile", "Finding rear camera failed, opening camera 0...");
                    camera = Android.Hardware.Camera.Open(0);
                }
#else
                camera = Android.Hardware.Camera.Open();
#endif
                if (camera == null)
                {
                    Android.Util.Log.Debug("ZXing.Net.Mobile", "Camera is null :(");
                }

                //camera = Android.Hardware.Camera.Open ();
                camera.SetPreviewDisplay(holder);
                camera.SetPreviewCallback(this);
            }
            catch (Exception)
            {
                ShutdownCamera();

                // TODO: log or otherwise handle this exception

                //throw;
            }
        }
Esempio n. 12
0
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            mCamera     = Open();
            mCameraInfo = GetCameraInfo();


            //https://www.captechconsulting.com/blogs/android-camera-orientation-made-simple
            mCamera.SetDisplayOrientation(getCorrectCameraOrientation(mCameraInfo, mCamera));

            try {
                mCamera.SetPreviewTexture(surface);
                mCamera.SetPreviewCallback(this);
                mCamera.StartPreview();
            } catch (IOException ioe) {
                // Something bad happened
            }
        }
Esempio n. 13
0
        public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int width, int height)
        {
            _camera = Camera.Open ();
            ConfigureCamera ();

            Preview preview = new Preview ();
            preview.OnFrame += OnPreviewFrame;
            _camera.SetPreviewCallback (preview);

            _textureView.LayoutParameters = new FrameLayout.LayoutParams (width, height);

            try {
                _camera.SetPreviewTexture (surface);
                _camera.StartPreview ();

            } catch (Exception e) {
                Console.WriteLine (e.Message);
            }
        }
Esempio n. 14
0
		public void OnPreviewFrame (byte[] data, Camera camera)
		{
			var parameters = camera.GetParameters ();
			var size = parameters.PreviewSize;

			var barcode = new Image (size.Width, size.Height, "Y800");
			barcode.SetData (data);

			var result = scanner.ScanImage (barcode);

			if (result == 0)
				return;
			

			camera.SetPreviewCallback (null);
			camera.StopPreview ();

			var scannerResult = GetScannerResult ();

			ScanComplete?.Invoke (this, new ScanCompleteEventArgs (scannerResult));
		}
Esempio n. 15
0
 public void start()
 {
     if (originalStart)
     {
         originalStart = false;
         Thread t = new Thread(new ThreadStart(reStarter));
         t.Start();
     }
     try
     {
         cam = Android.Hardware.Camera.Open();
         cam.GetParameters().PreviewFormat = ImageFormatType.Rgb565;
         var param = cam.GetParameters();
         param.SetPreviewSize(320, 240);
         cam.SetParameters(param);
         var x = cam.GetParameters().SupportedPreviewSizes;
         cam.SetDisplayOrientation(90);
         cam.SetPreviewCallback(this);
         cam.SetPreviewTexture(image.SurfaceTexture);
         cam.StartPreview();
     }
     catch (Exception e) { }
 }
        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();
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Create the camera and try to set auto focus with flash
        /// </summary>
        /// <param name="holder"></param>
		public void CreateCamera(ISurfaceHolder holder) {
			try
			{
				_camera = Camera.Open();
				Camera.Parameters p = _camera.GetParameters();
				p.PictureFormat = Android.Graphics.ImageFormatType.Jpeg;
				p.JpegQuality = 100;
				hasAutoFocus = (p.SupportedFocusModes.Contains(Camera.Parameters.FocusModeAuto));
				if (hasAutoFocus)
				{
					p.FocusMode = Camera.Parameters.FocusModeAuto;
					focusReady = true;
				}
				else if (p.SupportedFocusModes.Contains(Camera.Parameters.FocusModeContinuousPicture))
				{
					p.FocusMode = Camera.Parameters.FocusModeContinuousPicture;
				}
				if (p.SupportedFlashModes.Contains(Camera.Parameters.FlashModeOn))
				{
					p.FlashMode = Camera.Parameters.FlashModeOn;
				}
				_camera.SetParameters(p);
				_camera.SetDisplayOrientation(90);
				_camera.SetPreviewCallback(this);
				_camera.Lock();
				_camera.SetPreviewDisplay(holder);
				_camera.StartPreview();
				if (hasAutoFocus)
				{
					Toast.MakeText(this, "Press screen to focus", ToastLength.Long).Show();
				}
			}
			catch (Exception e)
			{
			}
		}
Esempio n. 18
0
		public void SurfaceCreated (ISurfaceHolder holder)
		{
			try {
				Console.WriteLine("Surface created");

				camera = Android.Hardware.Camera.Open ();
				camera.SetPreviewDisplay (holder);
				camera.SetPreviewCallback (this);

				Console.WriteLine("Surface Created end");

			} catch (Exception) {
				ShutdownCamera ();
				Console.WriteLine("Exception, shutdown camera");

				// TODO: log or otherwise handle this exception

				//throw;
			}
		}
		public void SurfaceCreated (ISurfaceHolder holder)
		{
			try 
			{
#if __ANDROID_9__

				var numCameras = Android.Hardware.Camera.NumberOfCameras;
				var camInfo = new Android.Hardware.Camera.CameraInfo();
				var found = false;

				for (int i = 0; i < numCameras; i++)
				{
					Android.Hardware.Camera.GetCameraInfo(i, camInfo);
					if (camInfo.Facing == CameraFacing.Back)
					{
						camera = Android.Hardware.Camera.Open(i);
						found = true;
						break;
					}
				}

				if (!found)
				{
					Android.Util.Log.Debug("ZXing.Net.Mobile", "Finding rear camera failed, opening camera 0...");
					camera = Android.Hardware.Camera.Open(0);
				}
#else
				camera = Android.Hardware.Camera.Open();
#endif
				if (camera == null)
					Android.Util.Log.Debug("ZXing.Net.Mobile", "Camera is null :(");


				//camera = Android.Hardware.Camera.Open ();
				camera.SetPreviewDisplay (holder);
				camera.SetPreviewCallback (this);

			} catch (Exception ex) {
				ShutdownCamera ();

				// TODO: log or otherwise handle this exception
				Console.WriteLine("Setup Error: " + ex);
				//throw;
			}
		}
Esempio n. 20
0
        protected override void OnStart()
        {
            try
            {
                if (Camera == null)
                {
                    Camera = Hardware.Camera.Open(cameraIndex);
                }

                if (Texture == null)
                {
                    Texture = new Graphics.SurfaceTexture(0);
                }

                CameraPreviewCallback callback = new CameraPreviewCallback();
                callback.PreviewUpdated += Callback_PreviewUpdated;

                Hardware.Camera.Parameters  parameter   = Camera.GetParameters();
                List <Hardware.Camera.Size> supportSize = parameter.SupportedPreviewSizes.OrderByDescending(x => x.Width).ToList();
                foreach (Hardware.Camera.Size size in supportSize)
                {
                    CvLogger.Log(this, $"Camera Support Size: W{size.Width},H{size.Height}");

                    if (size.Width == 960 && size.Height == 720)
                    {
                        parameter.SetPreviewSize(size.Width, size.Height);
                        CvLogger.Log(this, $"SET Camera Size: W{size.Width},H{size.Height}");
                    }
                }

                string[] supportedFocusMode = parameter.SupportedFocusModes.ToArray();
                if (supportedFocusMode.Contains(Hardware.Camera.Parameters.FocusModeContinuousVideo))
                {
                    parameter.FocusMode = Hardware.Camera.Parameters.FocusModeContinuousVideo;
                }
                else if (supportedFocusMode.Contains(Hardware.Camera.Parameters.FocusModeContinuousPicture))
                {
                    parameter.FocusMode = Hardware.Camera.Parameters.FocusModeContinuousPicture;
                }
                parameter.ColorEffect = Hardware.Camera.Parameters.EffectNone;

                width      = parameter.PreviewSize.Width;
                height     = parameter.PreviewSize.Height;
                fps        = parameter.PreviewFrameRate;
                cameraType = parameter.PreviewFormat;

                CvLogger.Log(this, string.Format("Camera is creating W{0} H{1} FPS{2}", width, height, fps));
                Camera.SetParameters(parameter);

                Camera.SetPreviewCallback(callback);
                Camera.SetPreviewTexture(Texture);
                Camera.StartPreview();

                cameraOn = true;
            }
            catch (Exception ex)
            {
                CvLogger.Log(this, "Camera Init Failed.\n" + ex.ToString());

                Dispose();

                throw new ArgumentException("Camera Exception", ex);
            }
        }
Esempio n. 21
0
        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);
            }
        }
Esempio n. 22
0
        public void SurfaceCreated(ISurfaceHolder holder)
        {
            CheckPermissions();

            var perf = PerformanceCounter.Start();

            try
            {
                var version = Android.OS.Build.VERSION.SdkInt;

                if (version >= BuildVersionCodes.Gingerbread)
                {
                    Android.Util.Log.Debug("ZXing.Net.Mobile", "Checking Number of cameras...");

                    var numCameras = Android.Hardware.Camera.NumberOfCameras;
                    var camInfo    = new Android.Hardware.Camera.CameraInfo();
                    var found      = false;
                    Android.Util.Log.Debug("ZXing.Net.Mobile", "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("ZXing.Net.Mobile", "Found " + whichCamera + " Camera, opening...");
                            camera = Android.Hardware.Camera.Open(i);
                            found  = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        Android.Util.Log.Debug("ZXing.Net.Mobile", "Finding " + whichCamera + " camera failed, opening camera 0...");
                        camera = Android.Hardware.Camera.Open(0);
                    }
                }
                else
                {
                    camera = Android.Hardware.Camera.Open();
                }
                if (camera == null)
                {
                    Android.Util.Log.Debug("ZXing.Net.Mobile", "Camera is null :(");
                }


                //camera = Android.Hardware.Camera.Open ();
                camera.SetPreviewDisplay(holder);
                camera.SetPreviewCallback(this);
            }
            catch (Exception ex)
            {
                ShutdownCamera();

                // TODO: log or otherwise handle this exception
                Console.WriteLine("Setup Error: " + ex);
                //throw;
            }

            PerformanceCounter.Stop(perf, "SurfaceCreated took {0}ms");
        }
Esempio n. 23
0
        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();
        }
Esempio n. 24
0
        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");
        }