Esempio n. 1
0
        /// <summary>
        /// https://codelabs.developers.google.com/codelabs/camerax-getting-started#3
        /// </summary>
        private void StartCamera()
        {
            var cameraProviderFuture = ProcessCameraProvider.GetInstance(this);

            cameraProviderFuture.AddListener(new Runnable(() =>
            {
                // Used to bind the lifecycle of cameras to the lifecycle owner
                var cameraProvider = (ProcessCameraProvider)cameraProviderFuture.Get();

                // Preview
                var preview = new Preview.Builder().Build();
                preview.SetSurfaceProvider(viewFinder.CreateSurfaceProvider());

                // Take Photo
                this.imageCapture = new ImageCapture.Builder().Build();

                // Frame by frame analyze
                var imageAnalyzer = new ImageAnalysis.Builder().Build();
                imageAnalyzer.SetAnalyzer(cameraExecutor, new LuminosityAnalyzer(luma =>
                                                                                 Log.Debug(TAG, $"Average luminosity: {luma}")
                                                                                 ));

                // Select back camera as a default, or front camera otherwise
                CameraSelector cameraSelector = null;
                if (cameraProvider.HasCamera(CameraSelector.DefaultBackCamera) == true)
                {
                    cameraSelector = CameraSelector.DefaultBackCamera;
                }
                else if (cameraProvider.HasCamera(CameraSelector.DefaultFrontCamera) == true)
                {
                    cameraSelector = CameraSelector.DefaultFrontCamera;
                }
                else
                {
                    throw new System.Exception("Camera not found");
                }

                try
                {
                    // Unbind use cases before rebinding
                    cameraProvider.UnbindAll();

                    // Bind use cases to camera
                    cameraProvider.BindToLifecycle(this, cameraSelector, preview, imageCapture, imageAnalyzer);
                }
                catch (Exception exc)
                {
                    Log.Debug(TAG, "Use case binding failed", exc);
                    Toast.MakeText(this, $"Use case binding failed: {exc.Message}", ToastLength.Short).Show();
                }
            }), ContextCompat.GetMainExecutor(this)); //GetMainExecutor: returns an Executor that runs on the main thread.
        }
Esempio n. 2
0
        /// <summary>
        /// 初始化并绑定摄像头
        /// </summary>
        /// <param name="lifecycleOwner"></param>
        public void InitAndStartCamera(ILifecycleOwner lifecycleOwner, Action <bool, string> InitCallBack)
        {
            var cameraProviderFuture = ProcessCameraProvider.GetInstance(this.Context);

            cameraExecutor = Executors.NewSingleThreadExecutor();

            cameraProviderFuture.AddListener(new Java.Lang.Runnable(() =>
            {
                // Used to bind the lifecycle of cameras to the lifecycle owner
                var cameraProvider = (ProcessCameraProvider)cameraProviderFuture.Get();

                // Preview
                var preview = new Preview.Builder()
                              .Build();
                preview.SetSurfaceProvider(_CameraPreView.SurfaceProvider);

                // Take Photo
                this._ImageCapture = new ImageCapture.Builder()
                                     .SetTargetResolution(new Size(CaptureImageSize_Width, CaptureImageSize_Height))
                                     .Build();

                // Frame by frame analyze(Not Use Now)
                var imageAnalyzer         = new ImageAnalysis.Builder().Build();
                imageAnalysisFrameProcess = new ImageAnalysisFrameProcess();
                imageAnalyzer.SetAnalyzer(cameraExecutor, imageAnalysisFrameProcess);
                //imageAnalyzer.SetAnalyzer(cameraExecutor, new LuminosityAnalyzer(luma =>
                //    Log.Debug("", $"Average luminosity: {luma}")
                //    ));

                #region Select back camera as a default, or front camera otherwise
                CameraSelector cameraSelector = null;
                //if (cameraProvider.HasCamera(CameraSelector.DefaultBackCamera) == true)
                //    cameraSelector = CameraSelector.DefaultBackCamera;
                //else if (cameraProvider.HasCamera(CameraSelector.DefaultFrontCamera) == true)
                //    cameraSelector = CameraSelector.DefaultFrontCamera;
                //else
                //    throw new System.Exception("Camera not found");
                switch (CameraFacing)
                {
                case CameraFacing.Back:
                    if (cameraProvider.HasCamera(CameraSelector.DefaultBackCamera) == true)
                    {
                        cameraSelector = CameraSelector.DefaultBackCamera;
                    }
                    else
                    {
                        if (cameraProvider.HasCamera(CameraSelector.DefaultFrontCamera) == true)
                        {
                            cameraSelector = CameraSelector.DefaultFrontCamera;
                        }
                        else
                        {
                            InitCallBack?.Invoke(false, "Not found any camera device");
                            return;
                        }
                    }
                    break;

                case CameraFacing.Front:
                    {
                        if (cameraProvider.HasCamera(CameraSelector.DefaultFrontCamera) == true)
                        {
                            cameraSelector = CameraSelector.DefaultFrontCamera;
                        }
                        else
                        {
                            if (cameraProvider.HasCamera(CameraSelector.DefaultBackCamera) == true)
                            {
                                cameraSelector = CameraSelector.DefaultBackCamera;
                            }
                            else
                            {
                                InitCallBack?.Invoke(false, "Not found any camera device");
                                return;
                            }
                        }
                    }
                    break;
                }
                #endregion
                try
                {
                    // Unbind use cases before rebinding
                    cameraProvider.UnbindAll();
                    // Bind use cases to camera
                    var camera        = cameraProvider.BindToLifecycle(lifecycleOwner, cameraSelector, preview, _ImageCapture, imageAnalyzer);
                    _CameraController = camera.CameraControl;
                    _CameraInfo       = camera.CameraInfo;
                    InitCallBack?.Invoke(true, "");
                }
                catch (Exception exc)
                {
                    Toast.MakeText(this.Context, $"Use case binding failed: {exc.Message}", ToastLength.Short).Show();
                }
            }), AndroidX.Core.Content.ContextCompat.GetMainExecutor(this.Context));
        }