A Camera View control for .NET MAUI applications.
$ dotnet add package Camera.MAUIA Camera View control and a Barcode Endode/Decode control (based on ZXing.Net) for .NET MAUI applications.
A ContetView control for camera management with the next properties:
| Android | iOS/Mac | Windows | |
|---|---|---|---|
| Preview | ✅ | ✅ | ✅ |
| Mirror preview | ✅ | ✅ | ✅ |
| Flash | ✅ | ✅ | ✅ |
| Torch | ✅ | ✅ | ✅ |
| Zoom | ✅ | ✅ | ✅ |
| Take snapshot | ✅ | ✅ | ✅ |
| Save snapshot | ✅ | ✅ | ✅ |
| Barcode detection/decode | ✅ | ✅ | ✅ |
Download and Install Camera.MAUI NuGet package on your application.
Initialize the plugin in your 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();
}
Add camera permissions to your application:
In your AndroidManifest.xml file (Platforms\Android) add the following permission:
<uses-permission android:name="android.permission.CAMERA" />
In your info.plist file (Platforms\iOS / Platforms\MacCatalyst) add the following permission:
<key>NSCameraUsageDescription</key>
<string>This app uses camera for...</string>Make sure that you enter a clear and valid reason for your app to access the camera. This description will be shown to the user.
In your Package.appxmanifest file (Platforms\Windows) go to Capabilities and mark Web Camera.
For more information on permissions, see the Microsoft Docs.
In XAML, make sure to add the right XML namespace:
xmlns:cv="xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
Use the control:
<cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="200"/>Configure the events:
cameraView.CamerasLoaded += CameraView_CamerasLoaded;
cameraView.BarcodeDetected += CameraView_BarcodeDetected;Configure the camera to use:
private void CameraView_CamerasLoaded(object sender, EventArgs e)
{
if (cameraView.Cameras.Count > 0)
{
cameraView.Camera = cameraView.Cameras.First();
MainThread.BeginInvokeOnMainThread(async () =>
{
if (await cameraView.StartCameraAsync() == CameraResult.Success)
{
controlButton.Text = "Stop";
playing = true;
}
});
}
}Start camera playback:
if (await cameraView.StartCameraAsync() == CameraResult.Success)
{
playing = true;
}Stop camera playback:
if (await cameraView.StopCameraAsync() == CameraResult.Success)
{
playing = false;
}Set Flash mode
cameraView.FlashMode = FlashMode.Auto;Toggle Torch
cameraView.TorchEnabled = !cameraView.TorchEnabled;Set mirrored mode
cameraView.MirroredImage = true;Set zoom factor
if (cameraView.MaxZoomFactor >= 2.5f)
cameraView.ZoomFactor = 2.5f;Get a snapshot from the playback
ImageSource imageSource = cameraView.GetSnapShot(ImageFormat.PNG);Enable and Handle barcodes detection:
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.BarCodeDetectionEnabled = true;
private void CameraView_BarcodeDetected(object sender, ZXingHelper.BarcodeEventArgs args)
{
Debug.WriteLine("BarcodeText=" + args.Result[0].Text);
}A ContentView control for generate codebars images.
In XAML, make sure to add the right XML namespace:
xmlns:cv="xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
Use the control and its bindable properties:
<cv:BarcodeImage x:Name="barcodeImage" Aspect="AspectFit"
WidthRequest="400" HeightRequest="400"
BarcodeWidth="200" BarcodeHeight="200" BarcodeMargin="5"
BarcodeBackground="White" BarcodeForeground="Blue"
BarcodeFormat="QR_CODE" />Set the barcode property to generate the image:
barcodeImage.Barcode = "https://github.com/hjam40/Camera.MAUI";