yushulx / Capture-Vision-Maui

https://www.nuget.org/packages/Capture.Vision.Maui
19 stars 3 forks source link
barcode datamatrix dotnet library maui pdf417 qrcode

.NET MAUI Camera View with Dynamsoft Vision SDKs

This project helps developers create .NET MAUI applications featuring a custom camera view using Dynamsoft Vision SDKs. These SDKs provide capabilities for barcode, MRZ (Machine Readable Zone), and document detection.

Example

https://github.com/yushulx/Capture-Vision-Maui/tree/main/Capture.Vision.Maui.Example

Demo Video: .NET MAUI QR Code Scanner

Supported Platforms

Features

Getting Started

  1. Enable the camera view in MauiProgram.cs:

    builder.UseNativeCameraView()
  2. Request a free trial license and replace LICENSE-KEY with your own license key in the platform-specific code.

    App.xaml.cs for Windows:

    using Dynamsoft;
    using Microsoft.UI.Xaml;
    namespace Capture.Vision.Maui.Example.WinUI
    {
        public partial class App : MauiWinUIApplication
        {
            public App()
            {
                this.InitializeComponent();
                BarcodeQRCodeReader.InitLicense("LICENSE-KEY");
                DocumentScanner.InitLicense("LICENSE-KEY");
                MrzScanner.InitLicense("LICENSE-KEY");
            }
        }
    }

    MainActivity.cs for Android:

    using Android.App;
    using Android.Content.PM;
    using Android.OS;
    using Dynamsoft;
    
    namespace Capture.Vision.Maui.Example
    {
        public class MainActivity : MauiAppCompatActivity
        {
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                // Your platform-specific code here
                BarcodeQRCodeReader.InitLicense("LICENSE-KEY");
                DocumentScanner.InitLicense("LICENSE-KEY");
                MrzScanner.InitLicense("LICENSE-KEY");
            }
        }
    }

    Program.cs for iOS:

    using ObjCRuntime;
    using UIKit;
    using Dynamsoft;
    
    static void Main(string[] args)
    {
        DocumentScanner.InitLicense("LICENSE-KEY");
        BarcodeQRCodeReader.InitLicense("LICENSE-KEY");
        MrzScanner.InitLicense("LICENSE-KEY");
    
        UIApplication.Main(args, null, typeof(AppDelegate));
    }
  3. Create a content page to add the camera view:

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:skia="clr-namespace:SkiaSharp.Views.Maui.Controls;assembly=SkiaSharp.Views.Maui.Controls"
                xmlns:cv="clr-namespace:Capture.Vision.Maui;assembly=Capture.Vision.Maui"
                x:Class="Capture.Vision.Maui.Example.CameraPage"
                Title="CameraPage">
        <ScrollView>
            <Grid>
                <cv:CameraView x:Name="cameraView" HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand" EnableBarcode="True" EnableDocumentDetect="True" EnableMrz="True"
                                    ResultReady="cameraView_ResultReady" FrameReady="cameraView_FrameReady"
                                />
                <skia:SKCanvasView x:Name="canvasView" 
                            Margin="0"
                            HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
                            PaintSurface="OnCanvasViewPaintSurface" />
            </Grid>
        </ScrollView>
    </ContentPage>

    Set EnableBarcode, EnableDocumentDetect, and EnableMrz to activate barcode, document, and MRZ (Machine Readable Zone) detection, respectively. Use the cameraView_ResultReady event to retrieve the results.

    private void cameraView_ResultReady(object sender, ResultReadyEventArgs e)
    {
        if (e.Result is BarcodeResult[])
        {}
        else if (e.Result is DocumentResult)
        {}
        else if (e.Result is MrzResult)
        {}    
    }

Custom Image Processing

In the cameraView_FrameReady event, access the camera frame for custom image processing:

private void cameraView_FrameReady(object sender, FrameReadyEventArgs e)
{
    // process image
}

The FrameReadyEventArgs provide the image buffer, width, height, stride and format.

public class FrameReadyEventArgs : EventArgs
{ 
    public enum PixelFormat
    {
        GRAYSCALE,
        RGB888,
        BGR888,
        RGBA8888,
        BGRA8888,
    }
    public FrameReadyEventArgs(byte[] buffer, int width, int height, int stride, PixelFormat pixelFormat)
    {
        Buffer = buffer;
        Width = width;
        Height = height;
        Stride = stride;
        Format = pixelFormat;
    }

    public byte[] Buffer { get; private set; }
    public int Width { get; private set; }
    public int Height { get; private set; }
    public int Stride { get; private set; }
    public PixelFormat Format { get; private set; }
}

Currently, frames captured from Windows and Android devices are in the GRAYSCALE format, whereas frames from iOS devices use the BGRA8888 format.

Barcode Parameters Configuration

Customize barcode detection parameters to fit specific requirements, such as supported barcode formats and expected counts:

public CameraPage()
{
    InitializeComponent();
    ...
    // cameraView.BarcodeParameters = "{\"Version\":\"3.0\", \"ImageParameter\":{\"Name\":\"IP1\", \"BarcodeFormatIds\":[\"BF_QR_CODE\", \"BF_ONED\"], \"ExpectedBarcodesCount\":20}}";
}

References

Building NuGet Package from Source Code

cd Capture.Vision.Maui
dotnet build --configuration Release