CameraMaui 1.4.8
dotnet add package CameraMaui --version 1.4.8
NuGet\Install-Package CameraMaui -Version 1.4.8
<PackageReference Include="CameraMaui" Version="1.4.8" />
paket add CameraMaui --version 1.4.8
#r "nuget: CameraMaui, 1.4.8"
// Install CameraMaui as a Cake Addin #addin nuget:?package=CameraMaui&version=1.4.8 // Install CameraMaui as a Cake Tool #tool nuget:?package=CameraMaui&version=1.4.8
Camera.MAUI
A Camera View 控件和一个 Barcode Endode/Decode 控件(基于 ZXing.Net)为 .NET MAUI 应用程序。
CameraView
A ContetView 控件,用于相机管理,具有以下属性
Android | iOS/Mac | Windows | |
---|---|---|---|
预览 | ✅ | ✅ | ✅ |
镜像预览 | ✅ | ✅ | ✅ |
闪光灯 | ✅ | ✅ | ✅ |
手电筒 | ✅ | ✅ | ✅ |
缩放 | ✅ | ✅ | ✅ |
拍照 | ✅ | ✅ | ✅ |
保存快照 | ✅ | ✅ | ✅ |
条码检测/解码 | ✅ | ✅ | ✅ |
视频/音频录制 | ✅ | ✅ | ✅ |
拍照 | ✅ | ✅ | ✅ |
安装和配置 CameraView
在您的应用程序中下载并安装 Camera.MAUI NuGet 包。
在您的
MauiProgram.cs
中初始化插件// Add the using to the top using Camera.MAUI; public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .UseMauiCameraView(); // Add the use of the plugging return builder.Build(); }
将相机/麦克风权限添加到您应用程序中
Android
在您的 AndroidManifest.xml
文件中( Platforms\Android)添加以下权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
iOS/MacCatalyst
在您的 info.plist
文件中( Platforms\iOS / Platforms\MacCatalyst)添加以下权限
<key>NSCameraUsageDescription</key>
<string>This app uses camera for...</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to the microphone for record videos</string>
确保您输入一个清晰且有效的理由以表明您应用程序对相机进行访问的必要性。此描述将显示给用户。
Windows
在您的 Package.appxmanifest
文件中( Platforms\Windows)转到功能并标记 Web Camera 和 Microphone。
有关权限的更多信息,请参阅 Microsoft Docs。
使用 CameraView
在 XAML 中,请确保添加正确的 XML 命名空间
xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
使用控件
<cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="200"/>
配置事件
cameraView.CamerasLoaded += CameraView_CamerasLoaded;
cameraView.BarcodeDetected += CameraView_BarcodeDetected;
配置要使用的相机和麦克风
private void CameraView_CamerasLoaded(object sender, EventArgs e)
{
if (cameraView.NumCamerasDetected > 0)
{
if (cameraView.NumMicrophonesDetected > 0)
cameraView.Microphone = cameraView.Microphones.First();
cameraView.Camera = cameraView.Cameras.First();
MainThread.BeginInvokeOnMainThread(async () =>
{
if (await cameraView.StartCameraAsync() == CameraResult.Success)
{
controlButton.Text = "Stop";
playing = true;
}
});
}
}
CameraInfo 类型(Camera 属性):CameraInfo 具有以下属性
public string Name
public string DeviceId
public CameraPosition Position
public bool HasFlashUnit
public float MinZoomFactor
public float MaxZoomFactor
public List<Size> AvailableResolutions
启动相机播放
if (await cameraView.StartCameraAsync(new Size(1280, 720)) == CameraResult.Success)
{
playing = true;
}
停止相机播放
if (await cameraView.StopCameraAsync() == CameraResult.Success)
{
playing = false;
}
设置闪光灯模式
cameraView.FlashMode = FlashMode.Auto;
切换手电筒
cameraView.TorchEnabled = !cameraView.TorchEnabled;
设置镜像模式
cameraView.MirroredImage = true;
设置缩放因子
if (cameraView.MaxZoomFactor >= 2.5f)
cameraView.ZoomFactor = 2.5f;
从播放中获取快照
ImageSource imageSource = cameraView.GetSnapShot(ImageFormat.PNG);
bool result = cameraView.SaveSnapShot(ImageFormat.PNG, filePath);
录制视频
var result = await cameraView.StartRecordingAsync(Path.Combine(FileSystem.Current.CacheDirectory, "Video.mp4"), new Size(1920, 1080));
....
result = await cameraView.StopRecordingAsync();
拍照
var stream = await cameraView.TakePhotoAsync();
if (stream != null)
{
var result = ImageSource.FromStream(() => stream);
snapPreview.Source = result;
}
使用MVVM与控制: 控件有多个用于获取快照的绑定属性
/// Binding property for use this control in MVVM.
public CameraView Self
/// Sets how often the SnapShot property is updated in seconds.
/// Default 0: no snapshots are taken
/// WARNING! A low frequency directly impacts over control performance and memory usage (with AutoSnapShotAsImageSource = true)
/// </summary>
public float AutoSnapShotSeconds
/// Sets the snaphost image format
public ImageFormat AutoSnapShotFormat
/// Refreshes according to the frequency set in the AutoSnapShotSeconds property (if AutoSnapShotAsImageSource is set to true) or when GetSnapShot is called or TakeAutoSnapShot is set to true
public ImageSource SnapShot
/// Refreshes according to the frequency set in the AutoSnapShotSeconds property or when GetSnapShot is called.
/// WARNING. Each time a snapshot is made, the previous stream is disposed.
public Stream SnapShotStream
/// Change from false to true refresh SnapShot property
public bool TakeAutoSnapShot
/// If true SnapShot property is refreshed according to the frequency set in the AutoSnapShotSeconds property
public bool AutoSnapShotAsImageSource
/// Starts/Stops the Preview if camera property has been set
public bool AutoStartPreview
{
get { return (bool)GetValue(AutoStartPreviewProperty); }
set { SetValue(AutoStartPreviewProperty, value); }
}
/// Full path to file where record video will be recorded.
public string AutoRecordingFile
{
get { return (string)GetValue(AutoRecordingFileProperty); }
set { SetValue(AutoRecordingFileProperty, value); }
}
/// Starts/Stops record video to AutoRecordingFile if camera and microphone properties have been set
public bool AutoStartRecording
{
get { return (bool)GetValue(AutoStartRecordingProperty); }
set { SetValue(AutoStartRecordingProperty, value); }
}
<cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="200"
BarCodeOptions="{Binding BarCodeOptions}"
BarCodeResults="{Binding BarCodeResults, Mode=OneWayToSource}"
Cameras="{Binding Cameras, Mode=OneWayToSource}" Camera="{Binding Camera}"
AutoStartPreview="{Binding AutoStartPreview}"
NumCamerasDetected="{Binding NumCameras, Mode=OneWayToSource}"
AutoSnapShotAsImageSource="True" AutoSnapShotFormat="PNG"
TakeAutoSnapShot="{Binding TakeSnapshot}" AutoSnapShotSeconds="{Binding SnapshotSeconds}"
Microphones="{Binding Microphones, Mode=OneWayToSource}" Microphone="{Binding Microphone}"
NumMicrophonesDetected="{Binding NumMicrophones, Mode=OneWayToSource}"
AutoRecordingFile="{Binding RecordingFile}"
AutoStartRecording="{Binding AutoStartRecording}"/>
您可以在MVVM示例中找到一个完整的MVVM示例
启用并处理条码检测
cameraView.BarcodeDetected += CameraView_BarcodeDetected;
cameraView.BarCodeOptions = new ZXingHelper.BarcodeDecodeOptions
{
AutoRotate = true,
PossibleFormats = { ZXing.BarcodeFormat.QR_CODE },
ReadMultipleCodes = false,
TryHarder = true,
TryInverted = true
};
cameraView.BarCodeDetectionFrameRate = 10;
cameraView.BarCodeDetectionMaxThreads = 5;
cameraView.ControlBarcodeResultDuplicate = true;
cameraView.BarCodeDetectionEnabled = true;
private void CameraView_BarcodeDetected(object sender, ZXingHelper.BarcodeEventArgs args)
{
Debug.WriteLine("BarcodeText=" + args.Result[0].Text);
}
使用事件或可绑定属性BarCodeResults
/// Event launched every time a code is detected in the image if "BarCodeDetectionEnabled" is set to true.
public event BarcodeResultHandler BarcodeDetected;
/// It refresh each time a barcode is detected if BarCodeDetectionEnabled porperty is true
public Result[] BarCodeResults
BarcodeImage
一个用于生成条形码图像的 ContentView 控件
在 XAML 中,请确保添加正确的 XML 命名空间
xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
使用该控件及其可绑定属性
<cv:BarcodeImage x:Name="barcodeImage" Aspect="AspectFit"
WidthRequest="400" HeightRequest="400"
BarcodeWidth="200" BarcodeHeight="200" BarcodeMargin="5"
BarcodeBackground="White" BarcodeForeground="Blue"
BarcodeFormat="QR_CODE" />
将条码属性设置以生成图像
barcodeImage.Barcode = "https://github.com/hjam40/Camera.MAUI";
产品 | 版本 兼容和额外的计算目标框架版本。 |
---|---|
.NET | net7.0是兼容的。 net7.0-android已被计算。 net7.0-android33.0是兼容的。 net7.0-ios已被计算。 net7.0-ios16.1是兼容的。 net7.0-maccatalyst已被计算。 net7.0-maccatalyst16.1是兼容的。 net7.0-macos已被计算。 net7.0-tvos已被计算。 net7.0-windows已被计算。 net7.0-windows10.0.19041是兼容的。 net8.0是兼容的。 net8.0-android已被计算。 net8.0-android34.0是兼容的。 net8.0-browser已被计算。 net8.0-ios已被计算。 net8.0-ios17.2是兼容的。 net8.0-maccatalyst已被计算。 net8.0-maccatalyst17.2是兼容的。 net8.0-macos已被计算。 net8.0-tvos已被计算。 net8.0-windows已被计算。 net8.0-windows10.0.19041是兼容的。 |
-
net7.0
- ZXing.Net (>= 0.16.9)
-
net7.0-android33.0
- ZXing.Net (>= 0.16.9)
-
net7.0-ios16.1
- ZXing.Net (>= 0.16.9)
-
net7.0-maccatalyst16.1
- ZXing.Net (>= 0.16.9)
-
net7.0-windows10.0.19041
- ZXing.Net (>= 0.16.9)
-
net8.0
- Microsoft.Maui.Controls (>= 8.0.7)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.7)
- ZXing.Net (>= 0.16.9)
-
net8.0-android34.0
- Microsoft.Maui.Controls (>= 8.0.7)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.7)
- ZXing.Net (>= 0.16.9)
-
net8.0-ios17.2
- Microsoft.Maui.Controls (>= 8.0.7)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.7)
- ZXing.Net (>= 0.16.9)
-
net8.0-maccatalyst17.2
- Microsoft.Maui.Controls (>= 8.0.7)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.7)
- ZXing.Net (>= 0.16.9)
-
net8.0-windows10.0.19041
- Microsoft.Maui.Controls (>= 8.0.7)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.7)
- ZXing.Net (>= 0.16.9)
NuGet 包
此包未被任何 NuGet 包使用。
GitHub 代码库
此包未被任何流行的 GitHub 代码库使用。