Camera.MAUI    1.5.1 
                    dotnet add package Camera.MAUI --version 1.5.1
NuGet\Install-Package Camera.MAUI -Version 1.5.1
<PackageReference Include="Camera.MAUI" Version="1.5.1" />
paket add Camera.MAUI --version 1.5.1
#r "nuget: Camera.MAUI, 1.5.1"
// Install Camera.MAUI as a Cake Addin #addin nuget:?package=Camera.MAUI&version=1.5.1 // Install Camera.MAUI as a Cake Tool #tool nuget:?package=Camera.MAUI&version=1.5.1
Camera.MAUI/Camera.MAUI.ZXing
A Camera View 控件和一个 Barcode Endode/Decode 控件(基于 ZXing.Net)用于 .NET MAUI 应用程序。
CameraView
A ContetView 控件用于摄像头管理,具有以下属性
| Android | iOS/Mac | Windows | |
|---|---|---|---|
| 预览 | ✅ | ✅ | ✅ | 
| 镜像预览 | ✅ | ✅ | ✅ | 
| 闪光灯 | ✅ | ✅ | ✅ | 
| 手电筒 | ✅ | ✅ | ✅ | 
| 缩放 | ✅ | ✅ | ✅ | 
| 拍摄快照 | ✅ | ✅ | ✅ | 
| 保存快照 | ✅ | ✅ | ✅ | 
| 条码检测/解码 | ✅ | ✅ | ✅ | 
| 录像/音频录制 | ✅ | ✅ | ✅ | 
| 拍摄照片 | ✅ | ✅ | ✅ | 
安装和配置 CameraView
- 将 Camera.MAUI NuGet 包下载并安装到您的应用程序中。 
- 将 Camera.MAUI.ZXing NuGet 包下载并安装到您的应用程序中(如果您想使用 ZXing 进行条码检测/解码)。 
- 在您的 - 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 相机和麦克风。
有关权限的更多信息,请参阅 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 类型(摄像头属性):CameraInfo 包含以下属性
    public string Name
    public string DeviceId
    public CameraPosition Position
    public bool HasFlashUnit
    public float MinZoomFactor
    public float MaxZoomFactor
    public float HorizontalViewAngle
    public float VerticalViewAngle
    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实例
对于条码检测,您必须设置Camera控件的BarCodeDecoder属性。使用Camera.MAUI.ZXing启用并处理条码检测
    using Camera.MAUI.ZXing;
    cameraView.BarcodeDetected += CameraView_BarcodeDetected;
    cameraView.BarCodeDecoder = new ZXingBarcodeDecoder();
    cameraView.BarCodeOptions = new BarcodeDecodeOptions
    {
        AutoRotate = true,
        PossibleFormats = { 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" />
设置BarcodeEncoder属性以启用图像生成(以Camera.MAUI.ZXing为例)
barcodeImage.BarcodeEncoder = new ZXingBarcodeEncoder();
设置条码属性以生成图像
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- 无依赖。
 
- 
                                                    net7.0-android33.0- 无依赖。
 
- 
                                                    net7.0-ios16.1- 无依赖。
 
- 
                                                    net7.0-maccatalyst16.1- 无依赖。
 
- 
                                                    net7.0-windows10.0.19041- 无依赖。
 
- 
                                                    net8.0- Microsoft.Maui.Controls (>= 8.0.6)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.6)
 
- 
                                                    net8.0-android34.0- Microsoft.Maui.Controls (>= 8.0.6)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.6)
 
- 
                                                    net8.0-ios17.2- Microsoft.Maui.Controls (>= 8.0.6)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.6)
 
- 
                                                    net8.0-maccatalyst17.2- Microsoft.Maui.Controls (>= 8.0.6)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.6)
 
- 
                                                    net8.0-windows10.0.19041- Microsoft.Maui.Controls (>= 8.0.6)
- Microsoft.Maui.Controls.Compatibility (>= 8.0.6)
 
NuGet 包 (1)
显示依赖 Camera.MAUI 的前 1 个 NuGet 包
| 包 | 下载 | 
|---|---|
| Camera.MAUI.ZXing 一个基于 ZXing.Net 的条码编码/解码控件,用于在 .NET MAUI 应用程序中与 Camera.MAUI 一起使用。 | 
GitHub 仓库
该包未在任何流行的 GitHub 仓库中使用。
| 版本 | 下载 | 最后更新 | 
|---|---|---|
| 1.5.1 | 41,939 | 2/29/2024 | 
| 1.5.0 | 2,933 | 2/29/2024 | 
| 1.4.4 | 109,653 | 6/9/2023 | 
| 1.4.3 | 2,468 | 5/24/2023 | 
| 1.4.2 | 721 | 5/19/2023 | 
| 1.4.1 | 1,296 | 5/17/2023 | 
| 1.4.0 | 1,028 | 5/12/2023 | 
| 1.3.5 | 3,138 | 4/15/2023 | 
| 1.3.4 | 783 | 4/13/2023 | 
| 1.3.3 | 210 | 4/13/2023 | 
| 1.3.2 | 174 | 4/13/2023 | 
| 1.3.1 | 309 | 4/11/2023 | 
| 1.3.0 | 393 | 4/9/2023 | 
| 1.2.1 | 4,257 | 3/30/2023 | 
| 1.2.0 | 260 | 3/30/2023 | 
| 1.1.0 | 504 | 3/10/2023 | 
| 1.0.0 | 425 | 3/9/2023 | 
将条码编码器/解码器分为不同的包。修复了一些错误