Dynamsoft.CaptureVision.Xamarin.Forms 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Dynamsoft.CaptureVision.Xamarin.Forms --version 1.0.0
NuGet\Install-Package Dynamsoft.CaptureVision.Xamarin.Forms -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dynamsoft.CaptureVision.Xamarin.Forms" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dynamsoft.CaptureVision.Xamarin.Forms --version 1.0.0
#r "nuget: Dynamsoft.CaptureVision.Xamarin.Forms, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dynamsoft.CaptureVision.Xamarin.Forms as a Cake Addin
#addin nuget:?package=Dynamsoft.CaptureVision.Xamarin.Forms&version=1.0.0

// Install Dynamsoft.CaptureVision.Xamarin.Forms as a Cake Tool
#tool nuget:?package=Dynamsoft.CaptureVision.Xamarin.Forms&version=1.0.0

Barcode Reader User Guide for Xamarin

Dynamsoft Capture Vision (DCV) is an aggregating SDK of a series of specific functional products including:

  • Dynamsoft Camera Enhancer (DCE) which provides camera enhancements and UI configuration APIs.
  • Dynamsoft Barcode Reader (DBR) which provides barcode decoding algorithm and APIs.
  • Dynamsoft Label Recognizer (DLR) which provides label content recognizing algorithm and APIs.
  • Dynamsoft Document Normalizer (DDN) which provides document scanning algorithms and APIs.

Note: DCV Flutter edition currently only includes DCE and DBR modules. DLR and DDN modules are still under development and will be included in the future.

System Requirements

Xamarin

  • NETStandard.Library 2.0+
  • Xamarin.Forms 5.0.0.2478+
  • Xamarin.Essentials: 1.3.1+ (1.4.0+ Recommended)

Android

  • Supported OS: Android 5.0 (API Level 21) or higher.
  • Supported ABI: armeabi-v7a, arm64-v8a, x86 and x86_64.
  • Development Environment: Android Studio 3.4+ (Android Studio 4.2+ recommended).
  • JDK: 1.8+

iOS

  • Supported OS: iOS 9.0 or higher.
  • Supported ABI: arm64 and x86_64.
  • Development Environment: Xcode 10 and above.

Installation

In the NuGet Package Manager>Manage Packages for Solution of your project, search for Dynamsoft.CaptureVision.Xamarin.Forms. Select all the modules of your project and click install.

Build Your Barcode Scanner App

Now you will learn how to create a simple barcode scanner using Dynamsoft Capture Vision Xamarin SDK.

Note: You can get the full source code of a similar project: Barcode Reader Simple Sample.

Set up Development Environment

If you are a beginner with Xamarin, please follow the guide on the Xamarin official website to set up the development environment.

Initialize the Project

Visual Studio
  1. Open the Visual Studio and select Create a new project.
  2. Select Mobile App (Xamarin.Forms) and click Next.
  3. Name the project SimpleBarcodeScanner. Select a location for the project and click Create.
  4. Select Blank and click Create.
Visual Studio for Mac
  1. Open Visual Studio and select New.
  2. Select Multiplatform > App > Blank App > C# and click Next.
  3. Name the project SimpleBarcodeScanner and click Next.
  4. Select a location for the project and click Create.

Include the Library

Add NuGet package Dynamsoft.CaptureVision.Xamarin.Forms to your project. You can view the installation section on how to add the library.

Initialize IDCVBarcodeReader and IDCVCameraEnhancer

You have to initialize the following two interfaces to decode barcodes with the library.

  • IDCVBarcodeReader: The interface that defines barcode decoding APIs and helps set up barcode decoding configurations.
  • IDCVCameraEnhancer: The interface that defines camera controlling APIs and helps you to set up a camera module to capture the video stream.

In App.xaml.cs, add the following code to initialize the IDCVBarcodeReader and IDCVCameraEnhancer objects.

using DCVXamarin;

namespace SimpleBarcodeScanner
{
    public partial class App : Application
    {
        public static IDCVCameraEnhancer camera;
        public static IDCVBarcodeReader barcodeReader;
        public App(IDCVCameraEnhancer dce, IDCVBarcodeReader dbr)
        {
            InitializeComponent();
            camera = dce;
            barcodeReader = dbr;
            MainPage = new NavigationPage(new MainPage());
        }
    }
}
Implement the Interfaces for Android

Open the MainActivity.cs in SimpleBarcodeScanner.Android folder. Change the code of LoadApplication to:

using DCVXamarin.Droid;

namespace dbr.Droid
{
    ...
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            DCVCameraEnhancer dce = new DCVCameraEnhancer(context: this);
            DCVBarcodeReader dbr = new DCVBarcodeReader();
            LoadApplication(new App(dce, dbr));
            ...
        }
        ...
    }
}
Implement the Interfaces for iOS

Open the AppDelegate.cs in SimpleBarcodeScanner.iOS folder. Change the code of LoadApplication to:

using DCVXamarin.iOS;

namespace dbr.iOS
{
    ...
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();
            DCVCameraEnhancer dce = new DCVCameraEnhancer();
            DCVBarcodeReader dbr = new DCVBarcodeReader();
            LoadApplication(new App(dce, dbr));
            return base.FinishedLaunching(app, options);
        }
    }
}

License Activation

The barcode reading module of Dynamsoft Capture Vision needs a valid license to work. Please refer to the Licensing section for more info on how to obtain a license.

Go back to App.xaml.cs. Add the following code to activate the license:

namespace SimpleBarcodeScanner
{
    // Add ILicenseVerificationListener to the class and implement LicenseVerificationCallback.
    public partial class App : Application, ILicenseVerificationListener
    {
        public App(IDCVCameraEnhancer dce, IDCVBarcodeReader dbr)
        {
            ...
            // Initialize the license of barcode reader module. Please note that the following license is a public trial.
            barcodeReader.InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", this);
            // You can add any extra barcode reader settings configurations after the license initialization
            ...
        }

        // Implement LicenseVerificationCallback to get message from the license server.
        public void LicenseVerificationCallback(bool isSuccess, string msg)
        {
            if (!isSuccess)
            {
                System.Console.WriteLine(msg);
            }
        }
    }
}

Add a Button to Start Scanning

In the MainPage.xaml, add the following code:

<StackLayout>
    <Button x:Name="startScanning" Text="Start Scanning" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnStartScanningButtonClicked" />
</StackLayout>

In the MainPage.xaml.cs, add the following code:

async void OnStartScanningButtonClicked(object sender, EventArgs e)
{
    await Navigation.PushAsync(new ScanningPage());
}

Configure the CameraView

Create a new Xamarin.Forms content page in the project and name it ScanningPage. In the ScanningPage.xaml add the CameraView:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dynamsoft = "clr-namespace:DCVXamarin;assembly=DCVXamarin"
             x:Class="SimpleBarcodeScanner.ScanningPage">
    <ContentPage.Content>
        <StackLayout>
            <Label x:Name="barcodeResultLabel" Text="No Barcode detected"></Label>
            
            <dynamsoft:DCVCameraView OverlayVisible="True"
                            HorizontalOptions="FillAndExpand"
                            VerticalOptions="FillAndExpand" >
            </dynamsoft:DCVCameraView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Open the Camera and Start Barcode Decoding

In this section, we are going to add code to start barcode decoding in the ScanningPage of the project. Open the ScanningPage.xaml.cs and add the following code:

using DCVXamarin;

namespace SimpleBarcodeScanner
{
    public partial class ScanningPage : ContentPage
    {
        public ScanningPage()
        {
            InitializeComponent();
            // Bind the CameraEnhancer to the BarcodeReader so that it can continuously obtain video stream from the camera for barcode decoding.
            App.barcodeReader.SetCameraEnhancer(App.camera);
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            // Open the camera the the view appears.
            App.camera.Open();
            // Start barcode decoding thread when the view appears.
            App.barcodeReader.StartScanning();
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            // Close the camera the the view appears.
            App.camera.Close();
            // Stop barcode decoding thread when the view appears.
            App.barcodeReader.StopScanning();
        }
    }
}

Obtaining Barcode Results

With the barcode decoding thread now configured, the library will start decoding from the video steam when the ScanningPage appears. You can use the interface IBarcodeResultListener to obtain the decoded BarcodeResults.

In ScanningPage.xaml, add a label for displaying the barcode results

<StackLayout>
    <Label x:Name="barcodeResultLabel" Text="No Barcode detected"></Label>
    ...
</StackLayout>

In ScanningPage.xaml.cs, add configurations of IBarcodeResultListener.

namespace SimpleBarcodeScanner
{
    // Add IBarcodeResultListener to your ScanningPage so that you can receive the barcode results.
    public partial class ScanningPage : ContentPage, IBarcodeResultListener
    {
        public ScanningPage()
        {
            ...
            App.barcodeReader.AddResultlistener(this);
        }
        ...
        // Implement the BarcodeResultCallback so that you can receive the barcode results when the barcodes are decoded.
        public void BarcodeResultCallback(int frameID, BarcodeResult[] barcodeResults)
        {
            // Here is an example code on how to display the barcode results on the view.
            string newBarcodeText = "";
            // The parameter 'barcodeResults' is an Array of BarcodeResult object.
            // Parse the 'barcodeResults' when it is not null.
            if (barcodeResults != null && barcodeResults.Length>0)
            {
                for (int i=0; i<barcodeResults.Length; i++)
                {
                    {
                        //The BarcodeText property of BarcodeResult is the string of barcode text result.
                        newBarcodeText += barcodeResults[i].BarcodeText;
                        newBarcodeText += "; ";
                    }
                }
                // On UI thread, refresh the previously prepared label with new barcode results.
                Device.BeginInvokeOnMainThread(()=> {
                    barcodeResultLabel.Text = newBarcodeText;
                });
            }
        }
    }
}

Add Camera Permission

Android

Add the following code in MainActivity.cs for requesting camera permission on Android devices.

using Xamarin.Essentials;

namespace SimpleBarcodeScanner.Droid
{
    ...
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            ...
            // Add camera permission.
            Permissions.RequestAsync<Permissions.Camera>();
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            ...
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
iOS

Add Privacy - Camera Usage Description and your message to the info.plist of your project.

Run the Project

Run Android on Windows

Select SimpleBarcodeScanner.Android and select your device. Run the project.

Run iOS & Android on macOS
  1. Right click on the SimpleBarcodeReader.iOS or SimpleBarcodeReader.Android and select Set As Startup Project.
  2. In the menu, select and click Run > Start Debugging.

Samples

You can view all the DCV Xamarin samples via the following links:

API References

View the API reference of DCV Xamarin Edition to explore the full feature of DCV:

Licensing

  • You can also request an extension for your trial license in the customer portal

Contact

https://www.dynamsoft.com/company/contact/

Product Compatible and additional computed target framework versions.
MonoAndroid monoandroid10.0 is compatible. 
Xamarin.iOS xamarinios10 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 3,857 3/31/2023
1.0.4 1,972 3/14/2023
1.0.3 1,409 2/14/2023
1.0.2 1,097 12/20/2022
1.0.1 2,156 10/17/2022
1.0.0 2,263 8/4/2022