hjam40 / Camera.MAUI

A CameraView Control for preview, take photos and control the camera options
MIT License
459 stars 76 forks source link

Prior to Android API 24 (Android 7), camera connection does not work and causes the application to freeze #85

Open Lonnaty opened 1 year ago

Lonnaty commented 1 year ago

Because the code uses the OutputConfiguration class available only from API 24

surfaces.Add(new OutputConfiguration(previewSurface)); surfaces26.Add(previewSurface); previewBuilder.AddTarget(previewSurface); if (imgReader != null) { surfaces.Add(new OutputConfiguration(imgReader.Surface)); surfaces26.Add(imgReader.Surface); } if (mediaRecorder != null) { surfaces.Add(new OutputConfiguration(mediaRecorder.Surface)); surfaces26.Add(mediaRecorder.Surface); previewBuilder.AddTarget(mediaRecorder.Surface); }

Need to add something like this

if (OperatingSystem.IsAndroidVersionAtLeast(24)) { surfaces.Add(new OutputConfiguration(previewSurface)); }

I also had a situation that map.GetOutputSizes returns an empty array and the connection to the camera was not completed.

StreamConfigurationMap map = (StreamConfigurationMap)camChars.Get(CameraCharacteristics.ScalerStreamConfigurationMap); videoSize = ChooseVideoSize(map.GetOutputSizes(Class.FromType(typeof(ImageReader)))); var maxVideoSize = ChooseMaxVideoSize(map.GetOutputSizes(Class.FromType(typeof(ImageReader))));

I used the standard resolution of 1280x720 for this situation.

try { videoSize = ChooseVideoSize(map.GetOutputSizes(Class.FromType(typeof(ImageReader)))); maxVideoSize = ChooseMaxVideoSize(map.GetOutputSizes(Class.FromType(typeof(ImageReader)))); } catch { videoSize = new Size(1280, 720); maxVideoSize = new Size(1280, 720); }

Lonnaty commented 1 year ago

And this line while (textureView.SurfaceTexture == null) Thread.Sleep(100);located in the StartPreview method causes the application to freeze if the SurfaceTexture has not been loaded beforehand because you then block via Thread.Sleep, it will continue indefinitely. You need to use SurfaceTextureAvailable Event: https://learn.microsoft.com/en-us/dotnet/api/android.views.textureview.surfacetextureavailable?view=xamarin-android-sdk-13&viewFallbackFrom=xamarin-android-sdk-12

Lonnaty commented 1 year ago

But it seems the author forgot about his plugin and I’m writing these comments just like that