ProcessCameraProvider.BindToLifecycle Not Working

Nathan Sokalski 4,116 Reputation points
2023-03-05T22:46:59.57+00:00

I have the following code to bind my camera to a PreviewView:

private void StartPreview()
{
	this.cameraproviderfuture.AddListener(new Runnable(() =>
	{
		//Preview
		this.preview = new Preview.Builder().SetTargetResolution(new Size(1280, 720)).Build();
		this.preview.SetSurfaceProvider(this.pvPreview.SurfaceProvider);

		this.imgcapture = new ImageCapture.Builder().Build();

		CameraSelector camselector = null;
		if (this.cameraprovider.HasCamera(CameraSelector.DefaultBackCamera)) { camselector = CameraSelector.DefaultBackCamera; }
		else if (this.cameraprovider.HasCamera(CameraSelector.DefaultFrontCamera)) { camselector = CameraSelector.DefaultFrontCamera; }
		else { throw new System.Exception("Camera Not Found"); }

		try
		{
			this.cameraprovider.UnbindAll();
			this.cameraprovider.BindToLifecycle(this, camselector, this.preview, this.imgcapture);
		}
		catch (Java.Lang.Exception e) { System.Diagnostics.Debug.WriteLine($"StartPreview: {e.Message}"); }
	}), ContextCompat.GetMainExecutor(this));

	//this.cameraprovider.IsBound(this.preview) is still false at this point
}

However, this does not seem to work (as stated in the comment at the end of the code above), but it also does not give any Exceptions. What is the problem?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,368 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,235 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,291 Reputation points Microsoft Vendor
    2023-03-09T08:11:49.76+00:00

    Hello,

    I tested Xamarin with reference to this official tutorial on Android API level 30, and the results showed that BindToLifecycle works as expected and the IsBound method returns true.

    Are you missing permissions or API levels when writing code?

    Please refer to the following tutorial and xamarin code sample.

    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();
    
            var preview = new Preview.Builder().Build();
            preview.SetSurfaceProvider(viewFinder.CreateSurfaceProvider());
    
            this.imageCapture = new ImageCapture.Builder().Build();
    
            var imageAnalyzer = new ImageAnalysis.Builder().Build();
            imageAnalyzer.SetAnalyzer(cameraExecutor, new LuminosityAnalyzer(luma =>
                Log.Debug(TAG, $"Average luminosity: {luma}")
                ));
    
           // Select back camera as a default
            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();
            }
            var b = cameraProvider.IsBound(preview); // the result will be true.
    
       }), ContextCompat.GetMainExecutor(this)); 
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments