CameraView
CameraView
可讓您連線到相機、顯示相機的預覽並拍照。 CameraView
也提供功能來支持拍攝相片、控制快閃、將擷取的媒體儲存到檔案,以及為事件提供不同的勾點。
下列各節會以累加方式建置如何在 .NET MAUI 應用程式中使用 CameraView
。 它們依賴的 CameraViewModel
用法。 ,將會設定為 BindingContext
範例 CameraViewPage
的 。
平臺特定初始化
CameraView
是 nuget 套件的CommunityToolkit.Maui.Camera
一部分。 若要先使用 , CameraView
請參閱 開始使用 一節。 需要下列平臺特定設定。
下列許可權必須新增至 Platforms/Android/AndroidManifest.xml
檔案:
<uses-permission android:name="android.permission.CAMERA" />
這應該會在專案內 <manifest>
新增。 以下顯示更完整的範例:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" />
<uses-permission android:name="android.permission.CAMERA" />
</manifest>
基本使用方式
CameraView
可以透過下列方式新增至 .NET MAUI 應用程式。
包含 XAML 命名空間
若要在 XAML 中使用工具組,必須將下列專案 xmlns
新增至您的頁面或檢視:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
因此,下列專案:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
</ContentPage>
將修改為包含 xmlns
,如下所示:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
</ContentPage>
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0" />
</Grid>
</ContentPage>
結果將會是呈現連接到裝置之預設相機輸出的表面。
存取目前的相機
屬性 SelectedCamera
可讓您存取目前選取的相機。
下列範例示範如何將 屬性從 CameraView
系結SelectedCamera
至具有相同名稱 (SelectedCamera
) 之 上的 CameraViewModel
屬性。
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}" />
</Grid>
</ContentPage>
控件縮放
屬性 SelectedCamera
同時 MinimumZoomFactor
提供 和 MaximumZoomFactor
屬性,這些是唯讀的,並提供開發人員以程序設計方式判斷可以套用至目前相機的縮放比例。 若要變更目前相機上的縮放比例, CameraView
會提供 ZoomFactor
屬性。
注意
如果在 外部 MinimumZoomFactor
提供值,且 MaximumZoomFactor
CameraView
會夾住值,使其保持在界限內。
下列範例示範如何將 新增 Slider
至應用程式,並設定下列系結:
- 將
Maximum
屬性Slider
系結到MaximumZoomFactor
屬性的SelectedCamera
。 - 將
Minimum
屬性Slider
系結到MinimumZoomFactor
屬性的SelectedCamera
。 - 將
Value
的Slider
屬性系結至CurrentZoom
類別上的CameraViewModel
屬性。
最後的變更牽涉到將 ZoomFactor
的 CameraView
屬性系結至 CurrentZoom
類別上的 CameraViewModel
屬性。
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
</Grid>
</ContentPage>
相機快閃模式
CameraView
可讓您以程式設計方式變更裝置上的快閃模式,可能的選項如下:
Off
- 快閃關閉,不會使用。On
- 快閃開啟且 一律 會使用。Auto
- 會根據光源條件自動使用閃光。
屬性 SelectedCamera
也提供 IsFlashSupported
,讓您能夠判斷目前選取的相機是否具有可控制的閃光。
下列範例示範如何將 新增 Picker
至應用程式,並設定下列系結:
- 將
IsVisible
屬性Picker
系結到IsFlashSupported
屬性的SelectedCamera
。 - 將
ItemsSource
的Picker
屬性系結至FlashModes
類別上的CameraViewModel
屬性 - 列舉可能值的CameraFlashMode
簡單清單。 - 將
SelectedItem
的Picker
屬性系結至FlashMode
類別上的CameraViewModel
屬性。
最後的變更牽涉到將 CameraFlashMode
的 CameraView
屬性系結至 FlashMode
類別上的 CameraViewModel
屬性。
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
</Grid>
</ContentPage>
ImageCaptureResolution
CameraView
可讓您以程式設計方式變更從目前相機擷取的影像解析度。
注意
這不會變更相機預覽中顯示的解析度。
屬性 SelectedCamera
也提供 SupportedResolutions
,讓您能夠判斷目前相機支援的解析度。
下列範例示範如何將 新增 Picker
至應用程式,並設定下列系結:
- 將
ItemsSource
屬性Picker
系結到SupportedResolutions
屬性的SelectedCamera
。 - 將
SelectedItem
的Picker
屬性系結至SelectedResolution
類別上的CameraViewModel
屬性。
最後的變更牽涉到將 ImageCaptureResolution
的 CameraView
屬性系結至 SelectedResolution
類別上的 CameraViewModel
屬性。
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
<Picker
Grid.Column="2"
Grid.Row="1"
Title="Available Resolutions"
ItemsSource="{Binding SelectedCamera.SupportedResolutions}"
SelectedItem="{Binding SelectedResolution}" />
</Grid>
</ContentPage>
CaptureImage
CameraView
提供以程序設計方式觸發影像擷取的功能。 這可以透過 CaptureImage
方法或 CaptureImageCommand
。
下列範例示範如何將 新增 Button
至應用程式,並設定下列系結:
- 將
Command
的Button
屬性系結至CaptureImageCommand
上的CameraView
屬性。
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
x:Name="Camera"
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
<Picker
Grid.Column="2"
Grid.Row="1"
Title="Available Resolutions"
ItemsSource="{Binding SelectedCamera.SupportedResolutions}"
SelectedItem="{Binding SelectedResolution}" />
<Button
Grid.Column="0"
Grid.Row="2"
Command="{Binding CaptureImageCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
</Grid>
</ContentPage>
注意
若要使用已擷取 CameraView
的映像,請提供 MediaCaptured
事件。
開始預覽
CameraView
提供從相機以程式設計方式啟動預覽的功能。 這可以透過 StartCameraPreview
方法或 StartCameraPreviewCommand
。
下列範例示範如何將 新增 Button
至應用程式,並設定下列系結:
- 將
Command
的Button
屬性系結至StartCameraPreviewCommand
上的CameraView
屬性。
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
x:Name="Camera"
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
<Picker
Grid.Column="2"
Grid.Row="1"
Title="Available Resolutions"
ItemsSource="{Binding SelectedCamera.SupportedResolutions}"
SelectedItem="{Binding SelectedResolution}" />
<Button
Grid.Column="0"
Grid.Row="2"
Command="{Binding CaptureImageCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
<Button
Grid.Column="1"
Grid.Row="2"
Command="{Binding StartCameraPreviewCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
</Grid>
</ContentPage>
停止預覽
CameraView
可讓您以程式設計方式停止相機的預覽。 這可以透過 StopCameraPreview
方法或 StopCameraPreviewCommand
。
下列範例示範如何將 新增 Button
至應用程式,並設定下列系結:
- 將
Command
的Button
屬性系結至StopCameraPreviewCommand
上的CameraView
屬性。
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.CameraViewPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,30,30">
<toolkit:CameraView
x:Name="Camera"
Grid.ColumnSpan="3"
Grid.Row="0"
SelectedCamera="{Binding SelectedCamera}"
ZoomFactor="{Binding CurrentZoom}"
CameraFlashMode="{Binding FlashMode}" />
<Slider
Grid.Column="0"
Grid.Row="1"
Value="{Binding CurrentZoom}"
Maximum="{Binding SelectedCamera.MaximumZoomFactor, FallbackValue=1}"
Minimum="{Binding SelectedCamera.MinimumZoomFactor, FallbackValue=1}"/>
<Picker
Grid.Column="1"
Grid.Row="1"
Title="Flash"
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"
ItemsSource="{Binding FlashModes}"
SelectedItem="{Binding FlashMode}" />
<Picker
Grid.Column="2"
Grid.Row="1"
Title="Available Resolutions"
ItemsSource="{Binding SelectedCamera.SupportedResolutions}"
SelectedItem="{Binding SelectedResolution}" />
<Button
Grid.Column="0"
Grid.Row="2"
Command="{Binding CaptureImageCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
<Button
Grid.Column="1"
Grid.Row="2"
Command="{Binding StartCameraPreviewCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
<Button
Grid.Column="2"
Grid.Row="2"
Command="{Binding StopCameraPreviewCommand, Source={x:Reference Camera}}"
Text="Capture Image" />
</Grid>
</ContentPage>
範例
您可以在 .NET MAUI Community Toolkit 範例應用程式中找到這項功能的範例。
API
您可以在 .NET MAUI Community Toolkit GitHub 存放庫上找到 的CameraView
原始程式碼。