Camera.MAUI.ZXing 1.0.0

dotnet add package Camera.MAUI.ZXing --version 1.0.0                
NuGet\Install-Package Camera.MAUI.ZXing -Version 1.0.0                
该命令旨在在 Visual Studio 的包管理器控制台中使用,因为它使用了 NuGet 模块的 Install-Package 版本。
<PackageReference Include="Camera.MAUI.ZXing" Version="1.0.0" />                
对于支持 PackageReference 的项目,请将此 XML 节点复制到项目文件中以引用包。
paket add Camera.MAUI.ZXing --version 1.0.0                
#r "nuget: Camera.MAUI.ZXing, 1.0.0"                
#r 指令可用于 F# Interactive 和 Polyglot Notebooks。将此内容复制到交互式工具或脚本的源代码中,以引用包。
// Install Camera.MAUI.ZXing as a Cake Addin
#addin nuget:?package=Camera.MAUI.ZXing&version=1.0.0

// Install Camera.MAUI.ZXing as a Cake Tool
#tool nuget:?package=Camera.MAUI.ZXing&version=1.0.0                

Camera.MAUI/Camera.MAUI.ZXing

一个为 .NET MAUI 应用程序提供相机视图控件和条形码编码/解码控件(基于 ZXing.Net)的库。

CameraView

一个用于相机管理的 ContentView 控件,具有以下属性:

Android iOS/Mac Windows
预览
镜像预览
闪光灯
手电筒
缩放
拍照
保存快照
条码检测/解码
视频/音频录制
拍摄照片

安装和配置 CameraView

  1. Camera.MAUI NuGet 包下载并安装到您的应用程序中。

  2. Camera.MAUI.ZXing NuGet 包下载并安装到您的应用程序中(如果想要使用 ZXing 进行条码检测/解码)。

  3. 在您的 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();
    }
    
  4. 将相机/麦克风权限添加到您的应用程序

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 )中,转到功能并标记网络摄像头和麦克风。

有关权限的更多信息,请参阅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 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 Control 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

A 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 兼容。
兼容的目标框架
包含的目标框架(在包中)
了解更多关于 目标框架.NET Standard 的信息。

NuGet 包

该包未由任何 NuGet 包使用。

GitHub 仓库

该包未由任何流行的 GitHub 仓库使用。

版本 下载 最后更新
1.0.0 17,976 2/29/2024

第一个版本