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); }
private void FindAndOpenCamera() { bool cameraPermissions = CheckCameraPermissions(); if (!cameraPermissions) { return; } string errorMessage = "Unknown error"; bool foundCamera = false; IListenableFuture cameraProviderFuture = ProcessCameraProvider.GetInstance(this); cameraProviderFuture.AddListener(new Runnable(() => { // Camera provider is now guaranteed to be available mCameraProvider = cameraProviderFuture.Get() as ProcessCameraProvider; try { // Find first back-facing camera that has necessary capability. CameraSelector cameraSelector = new CameraSelector.Builder().RequireLensFacing((int)mLensFacing).Build(); // Find a good size for output - largest 4:3 aspect ratio that's less than 720p Preview.Builder previewBuilder = new Preview.Builder() .SetTargetAspectRatio(AspectRatio.Ratio43); Camera2Interop.Extender previewExtender = new Camera2Interop.Extender(previewBuilder); previewExtender.SetSessionCaptureCallback(mCaptureCallback); Preview preview = previewBuilder.Build(); // Must unbind the use-cases before rebinding them mCameraProvider.UnbindAll(); ICamera camera = mCameraProvider.BindToLifecycle( this as ILifecycleOwner, cameraSelector, preview); if (camera != null) { // Found suitable camera - get info, open, and set up outputs mCameraInfo = camera.CameraInfo; mCameraControl = camera.CameraControl; preview.SetSurfaceProvider(this); foundCamera = true; } if (!foundCamera) { errorMessage = GetString(Resource.String.camera_no_good); } SwitchRenderMode(0); } catch (CameraAccessException e) { errorMessage = GetErrorString(e); } if (!foundCamera) { ShowErrorDialog(errorMessage); } }), ContextCompat.GetMainExecutor(this)); }
protected override void OnPause() { base.OnPause(); // Wait until camera is closed to ensure the next application can open it if (mCameraProvider != null) { mCameraProvider.UnbindAll(); mCameraProvider = null; } }
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); } }
// Declare and bind preview, capture and analysis use cases private void BindCameraUseCases() { // Get screen metrics used to setup camera for full screen resolution var metrics = new DisplayMetrics(); viewFinder.Display.GetRealMetrics(metrics); Log.Debug(Tag, "Screen metrics: " + metrics); var screenAspectRatio = AspectRatio(metrics.WidthPixels, metrics.HeightPixels); Log.Debug(Tag, "Preview aspect ratio: " + screenAspectRatio); var rotation = viewFinder.Display.Rotation; // CameraProvider if (cameraProvider == null) { throw new Java.Lang.IllegalStateException("Camera initialization failed."); } // CameraSelector var cameraSelector = (new CameraSelector.Builder()).RequireLensFacing(lensFacing).Build(); // Preview preview = new Preview.Builder() // We request aspect ratio but no resolution .SetTargetAspectRatio(screenAspectRatio) // Set initial target rotation .SetTargetRotation((int)rotation) .Build(); // ImageCapture imageCapture = new ImageCapture.Builder() .SetCaptureMode(ImageCapture.CaptureModeMinimizeLatency) // We request aspect ratio but no resolution to match preview config, but letting // CameraX optimize for whatever specific resolution best fits our use cases .SetTargetAspectRatio(screenAspectRatio) // Set initial target rotation, we will have to call this again if rotation changes // during the lifecycle of this use case .SetTargetRotation((int)rotation) .Build(); // ImageAnalysis imageAnalyzer = new ImageAnalysis.Builder() // We request aspect ratio but no resolution .SetTargetAspectRatio(screenAspectRatio) // Set initial target rotation, we will have to call this again if rotation changes // during the lifecycle of this use case .SetTargetRotation((int)rotation) .Build(); // The analyzer can then be assigned to the instance imageAnalyzer.SetAnalyzer(cameraExecutor, new LuminosityAnalyzer( (double luma) => { // Values returned from our analyzer are passed to the attached listener // We log image analysis results here - you should do something useful // instead! Log.Debug(Tag, "Average luminosity: " + luma.ToString("0.00")); }) ); // Must unbind the use-cases before rebinding them cameraProvider.UnbindAll(); try { // A variable number of use-cases can be passed here - // camera provides access to CameraControl & CameraInfo camera = cameraProvider.BindToLifecycle( (ILifecycleOwner)this, cameraSelector, preview, imageCapture, imageAnalyzer); // Attach the viewfinder's surface provider to preview use case preview?.SetSurfaceProvider(viewFinder.SurfaceProvider); } catch (Java.Lang.Exception exc) { Log.Error(Tag, "Use case binding failed: " + exc); } }