You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/maui/views/camera-view.md
+88-3Lines changed: 88 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -177,11 +177,48 @@ The `CameraView` can be added to a .NET MAUI application in the following way.
177
177
178
178
The result will be a surface rendering the output of the default camera connected to the device.
179
179
180
-
## Access the current camera
180
+
## ICameraProvider
181
181
182
-
The `SelectedCamera` property provides the ability to access the currently selected camera.
182
+
The `ICameraProvider` interface provides access to the list of cameras available on the current device, as well as methods for initializing and refreshing that list. It is registered internally as a singleton service when the `CommunityToolkit.Maui.Camera` package is used (see [Getting started](../get-started.md?tabs=CommunityToolkitMauiCamera)), so it can be injected into view models or other classes through the constructor.
183
183
184
-
The following example shows how to bind the `SelectedCamera` property from the `CameraView` to a property on the `CameraViewModel` with the same name (`SelectedCamera`).
184
+
The following example show how to request an `ICameraProvider` through dependency injection and call `InitializeAsync` to obtain a list of available cameras. This performs a one-time discovery of cameras and populates the `AvailableCameras` property. Subsequent calls will reuse the cached results.
If camera availability changes at runtime (e.g. an external USB camera is plugged in), call `RefreshAvailableCameras` to force a refresh. Unlike `InitializeAsync`, this always re-runs camera discovery to ensure the list is up to date, at the cost of performance as it may be expensive on some platforms (e.g., Windows).
The `SelectedCamera` property provides the ability to get and set the currently selected camera.
218
+
219
+
It is a bindable property with default `TwoWay` binding. This means that when it is bound to a property in a view model, updating the `SelectedCamera` of the `CameraView` will also automatically update the corresponding property in the view model.
220
+
221
+
The following example shows how to bind the `SelectedCamera` property from the `CameraView` to a property on the `CameraViewModel` with the same name (`SelectedCamera`), and a `Picker` to change the selected camera.
185
222
186
223
```xaml
187
224
<ContentPage
@@ -195,11 +232,56 @@ The following example shows how to bind the `SelectedCamera` property from the `
The following describes the different behaviors of the code:
271
+
272
+
- No initial camera specified
273
+
If `SelectedCamera` is not assigned in the view model, the `CameraView` automatically defaults to the first available camera after loading.
274
+
Because the binding is `TwoWay`, the `Picker` will also reflect that camera as selected.
275
+
276
+
- Initial camera specified in the view model
277
+
If you assign an initial value (e.g. `SelectedCamera = Cameras.LastOrDefault();`), then both the `CameraView` and the `Picker` will start with that camera selected.
278
+
279
+
- User changes selection
280
+
When the user selects a camera from the `Picker`, the `SelectedCamera` property in the view model is updated, which in turn updates the `CameraView` to display the newly selected camera.
281
+
282
+
> [!NOTE]
283
+
> If the `SelectedCamera` is not specified an initial value, it will be set automatically to the first available camera on the device after the `CameraView` is loaded.
284
+
203
285
## Control Zoom
204
286
205
287
The `SelectedCamera` property provides both a `MinimumZoomFactor` and a `MaximumZoomFactor` property, these are read-only and provide developers with a programmatic way of determining what zoom can be applied to the current camera. In order to change the zoom on the current camera the `CameraView` provides the `ZoomFactor` property.
The `CameraView` provides the ability to programmatically start the preview from the camera. This is possible through both the `StartCameraPreview` method or the `StartCameraPreviewCommand`.
533
615
616
+
> [!NOTE]
617
+
> The camera previw is always automatically started when the `CameraView` is loaded, so you don't need to call `StartCameraPreview` explicitly. The `StartCameraPreview` and `StopCameraPreview` is used to stop and start the camera preview after the `CameraView` is loaded and still active.
618
+
534
619
The following example shows how to add a `Button` into the application and setup the following bindings:
535
620
536
621
- Bind the `Command` property of the `Button` to the `StartCameraPreviewCommand` property on the `CameraView`.
0 commit comments