Skip to content

Add CameraView#1758

Merged
TheCodeTraveler merged 101 commits intoCommunityToolkit:mainfrom
zhitaop:feature/zp/camera-view
Jun 8, 2024
Merged

Add CameraView#1758
TheCodeTraveler merged 101 commits intoCommunityToolkit:mainfrom
zhitaop:feature/zp/camera-view

Conversation

@zhitaop
Copy link
Copy Markdown
Contributor

@zhitaop zhitaop commented Mar 17, 2024

Description of Change

As described in #259 (comment), this PR adds the initial implementation of the CameraView proposal on MacCatalyst, Windows, Android and iOS, and a corresponding demo page in the sample app . It's based on the fantastic work from the following PR/ branch #1080 and #1387.
Below is the summary of the proposed API design, it has been updated with feedback from #259 (comment)

public class CameraView
{
    public static readonly BindableProperty SelectedCameraProperty; // To be discussed: ActiveCameraProperty?
    public static readonly BindableProperty ZoomFactorProperty;
    public static readonly BindableProperty ResolutionProperty;
    public static readonly BindableProperty IsCameraBusyProperty;
    public static readonly BindableProperty IsAvailableProperty;
    public static readonly BindableProperty FlashModeProperty;
    public static readonly BindableProperty IsTorchOnProperty;

    public event EventHandler<MediaCapturedEventArgs> MediaCaptured;
    public event EventHandler<MediaCaptureFailedEventArgs> MediaCaptureFailed;

    public CameraInfo? SelectedCamera { get; set; } // To be discussed: ActiveCamera?
    public float ZoomFactor { get; set; }
    public Size CaptureResolution { get; set; } //  renamed from `Resolution`
    public bool IsCameraBusy { get; set; }
    public bool IsAvailable { get; set; }
    public bool IsTorchOn { get; set; }
    public CameraFlashMode FlashMode { get; set; }

    public void Shutter()
    public void Start()
    public void Stop()
}

public enum CameraFlashMode
{
    Off,
    On,
    Auto
}

public class CameraInfo
{
    public string Name { get; }
    public string DeviceId { get; }
    public CameraPosition Position { get; }
    public bool IsFlashSupported { get; } //To be discussed:  adding a list of capabilities like Auto Focus, etc?
    public float MinimumZoomFactor { get; }
    public float MaximumZoomFactor { get; }
    public ObservableCollection<Size> SupportedResolutions { get; } // renamed from `AvailableResolutions`. 
    //Observable makes it easier for binding as the resolution would only be updated after the camera view is initialized on Windows 
}

public enum CameraPosition // To be discussed: adding External value
{
    Unknown,
    Rear,
    Front
}

public class CameraProvider
{
    public ObservableCollection<CameraInfo> AvailableCameras { get; }
    public void RefreshAvailableCameras();
}

Linked Issues

@zhitaop zhitaop changed the title CameraView and sample app initial commit CameraView initial work (Windows, Android, iOS) Mar 17, 2024
@zhitaop
Copy link
Copy Markdown
Contributor Author

zhitaop commented Mar 18, 2024

@dotnet-policy-service agree

Copy link
Copy Markdown
Contributor

@bijington bijington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhitaop thank you so much for this PR! I have added my initial comments and most I don't think require much effort if they do need to be changed.

I plan to try and test some bits later this week. I would also love to look at possible macOS support which I am happy to contribute, I wouldn't let this hold up the PR completing though, worst case I can always open a follow up PR to add that in.

Comment thread src/CommunityToolkit.Maui.CameraView/CommunityToolkit.Maui.CameraView.csproj Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/CameraInfo.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/CameraView.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/CameraView.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/CameraView.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/Providers/CameraProvider.android.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/CameraInfo.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/Views/CameraManager.ios.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/Views/CameraManager.ios.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/Views/CameraManager.shared.cs Outdated
@jfversluis
Copy link
Copy Markdown
Member

I added the Analyzers for the initialization code for you!

@jfversluis jfversluis added the needs discussion Discuss it on the next Monthly standup label Mar 30, 2024
jfversluis and others added 2 commits April 4, 2024 11:20
* Add exception handling when AvailableCameras is empty
* Use expression body
* Remove nullable in CameraInfo
@bijington
Copy link
Copy Markdown
Contributor

@zhitaop Thanks for the responses. Sorry things have been chaotic here so I haven't responded or reviewed yet. I'll try and take a look this week

Copy link
Copy Markdown
Contributor

@bijington bijington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhitaop Thanks for your responses, I have added some further comments. I think the majority looks great and we could be close to getting this ready to merge. Let me know your thoughts and whether you need any assistance

Comment thread src/CommunityToolkit.Maui.CameraView/.editorconfig Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/.editorconfig Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/CameraInfo.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/CommunityToolkit.Maui.CameraView.csproj Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/CameraView.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui.CameraView/Views/CameraManager.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui.sln Outdated
@TheCodeTraveler TheCodeTraveler removed the needs discussion Discuss it on the next Monthly standup label May 2, 2024
@bijington bijington marked this pull request as ready for review May 2, 2024 19:28
@TheCodeTraveler TheCodeTraveler changed the title CameraView initial work (Windows, Android, iOS) CameraView initial work May 5, 2024
@TheCodeTraveler TheCodeTraveler changed the title CameraView initial work Add CameraView May 5, 2024
@TheCodeTraveler
Copy link
Copy Markdown
Collaborator

Thanks @pictos - great review! I've incorporated your comments here: 0f85de2

I see a lot of exceptions that could be thrown on Manage
I went through every branch where we throw new CameraException(), and the only time we do that now when no cameras are available on the device is when the user does something that requires the camera. For example, when the dev calls StartCameraPreview() or UpdateCaptureResolution() (methods that require a camera), we'll throw CameraException.

This should ensure devs are able to catch the CameraException now.

TheCodeTraveler
TheCodeTraveler previously approved these changes Jun 6, 2024
Copy link
Copy Markdown
Collaborator

@TheCodeTraveler TheCodeTraveler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok gang! I think this is ready to merge. 🚀

That being said, Pedro brought up some good comments in his review that we should discuss first! Let's chat about it in tomorrow's standup to get a group consensus before we click merge 🙌

@zhitaop
Copy link
Copy Markdown
Contributor Author

zhitaop commented Jun 6, 2024

@brminnick @bijington Thanks for all the great work! There has been definitely great progress and the code looks much more organized and consistent to the rest of the toolkit now.
After some testing on Windows using the sample app, I did notice a few issues that might worth looking into:

  1. Due to the access level of CameraProvider , there is no way to access the AvaiableCameras other than CameraView.GetAvailableCameras. My original intent is for CameraProvider to be a public singleton class decoupled from CameraView so that it can be used to access the available CameraInfo without a CameraView instance. Both Android and iOS use a similar pattern in their native camera APIs.
  2. The camera switcher and some of the original debug text (camera name, zoom level, etc) are missing on the sample camera view page
  3. UpdateCameraInfo in CameraManage.Windows.cs is not needed anymore, since now CameraInfo is fully initialized in CameraProvider.Windows.cs and does not need to be updated.

I haven't got the chance to look into these issues myself, so just leave them here as discussion points before the merge!

bijington and others added 5 commits June 6, 2024 21:56
…to Sample App

`ICameraProvder` is public and can be accessed via Dependency Injection (or IPlatformApplication.Current.Services.GetRequiredService<ICameraProvider>()
@TheCodeTraveler TheCodeTraveler removed the needs discussion Discuss it on the next Monthly standup label Jun 8, 2024
Copy link
Copy Markdown
Collaborator

@TheCodeTraveler TheCodeTraveler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're good to go! Thanks @zhitaop!!!

FYI - We discussed your latest review in our standup on Thursday. Here's a quick rundown:

  1. Due to the access level of CameraProvider , there is no way to access the AvaiableCameras other than CameraView.GetAvailableCameras.

The interface ICameraProvider is public and we add it to the IServiceCollection; it can be accessed by developers using Dependency Injection or IPlatformApplication.Current.Services.GetRequiredService<ICameraProvider>(). I've added an example of this to the Sample App in CameraViewViewModel.

The camera switcher and some of the original debug text (camera name, zoom level, etc) are missing on the sample camera view page

Yea, that was me being a bit lazy. I removed it because the AvailableCameras requires async code and after refactoring its async implementation, the value was null when the page first displayed. You're totally welcome to re-add it in a future PR, though! I was just being a bit lazy removing it and not re-implementing the UI with the async implementation, wanting to not delay this PR any further.

UpdateCameraInfo in CameraManage.Windows.cs is not needed anymore, since now CameraInfo is fully initialized in CameraProvider.Windows.cs and does not need to be updated.

Removed ✅

@TheCodeTraveler TheCodeTraveler dismissed bijington’s stale review June 8, 2024 16:08

Changes implemented

@TheCodeTraveler TheCodeTraveler enabled auto-merge (squash) June 8, 2024 16:09
@TheCodeTraveler TheCodeTraveler merged commit 8bc1f04 into CommunityToolkit:main Jun 8, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Proposal] CameraView minimal port

7 participants