void BindScan(ProcessCameraProvider cameraProvider, int width, int height) { var preview = new Preview.Builder().Build(); // 绑定预览 preview.SetSurfaceProvider(binding !.previewView.SurfaceProvider); // 使用后置相机 var cameraSelector = new CameraSelector.Builder() .RequireLensFacing(CameraSelector.LensFacingBack) .Build(); // 绑定图片扫描解析 var imageAnalysis = new ImageAnalysis.Builder() .SetTargetResolution(new(width, height)) .SetBackpressureStrategy(ImageAnalysis.StrategyKeepOnlyLatest) .Build(); // 绑定图片扫描解析 analyser = new((barCodes, imageWidth, imageHeight) => { // 解绑当前所有相机操作 cameraProvider.UnbindAll(); ScanCompletedHandler?.Invoke(barCodes.Select(x => x.GetRawBytes())); ScanCompletedHandler = null; Finish(); }); imageAnalysis.SetAnalyzer(Executors.NewSingleThreadExecutor(), analyser); camera = cameraProvider.BindToLifecycle(this, cameraSelector, imageAnalysis, preview); }
/// <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. }
private void BindCameraUseCases() { var metrics = new DisplayMetrics(); _previewView.Display !.GetRealMetrics(metrics); var rotation = GetRotation(_previewView.Display.Rotation); var cameraSelector = new CameraSelector.Builder().RequireLensFacing(CameraSelector.LensFacingBack) .Build(); var aspectRatio = AspectRatio(metrics.WidthPixels, metrics.HeightPixels); _preview = new Preview.Builder() .SetTargetRotation(rotation) .SetTargetAspectRatio(aspectRatio) .Build(); _imageCapture = new ImageCapture.Builder() .SetCaptureMode(ImageCapture.CaptureModeMinimizeLatency) .SetTargetRotation(rotation) .SetTargetAspectRatio(aspectRatio) .Build(); var imageAnalyzer = new ImageAnalysis.Builder() .SetTargetRotation(rotation) .SetTargetAspectRatio(aspectRatio) .Build(); imageAnalyzer.SetAnalyzer(_cameraExecutor, new LuminosityAnalyzer(this)); _cameraProvider.UnbindAll(); try { _camera = _cameraProvider.BindToLifecycle(this, cameraSelector, _preview, _imageCapture, imageAnalyzer); _preview.SetSurfaceProvider(_previewView.SurfaceProvider); } catch (Java.Lang.Exception ex) { Log.Error(TAG, "Use case binding failed", ex); } }
/// <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)); }