Skip to content

yushulx/dotnet-document-scanner-sdk

Repository files navigation

.NET Document Scanner SDK

The .NET Document Scanner SDK is a C# wrapper for Dynamsoft Document Normalizer SDK. It is used to do document edge detection, image cropping, perspective correction and image enhancement.

License Activation

Click here to get a valid license key.

Supported Platforms

  • Windows (x64)
  • Linux (x64)
  • Android
  • iOS

API

  • public static void InitLicense(string license): Initialize the license key. It must be called before creating the document scanner object.
  • public static DocumentScanner Create(): Create the document scanner object.
  • public Result[]? DetectFile(string filename): Detect documents from an image file.
  • public Result[]? DetectBuffer(byte[] buffer, int width, int height, int stride, ImagePixelFormat format): Detect documents from a buffer.
  • public NormalizedImage NormalizeFile(string filename, int[] points): Normalize the detected documents from an image file.
  • public NormalizedImage NormalizeBuffer(byte[] buffer, int width, int height, int stride, ImagePixelFormat format, int[] points): Normalize the detected documents from a buffer.
  • public static string? GetVersionInfo(): Get SDK version number.
  • public void SetParameters(string parameters): Customize the parameters. Refer to Parameter Organization for more details.

Usage

  • Set the license key:

    DocumentScanner.InitLicense("LICENSE-KEY"); 
  • Initialize the document scanner object:

    DocumentScanner scanner = DocumentScanner.Create();
  • Detect documents from an image file:

    Result[]? resultArray = scanner.DetectFile(filename);
  • Detect documents from a buffer:

    Result[]? resultArray = scanner.DetectBuffer(bytes, width, height, stride, DocumentScanner.ImagePixelFormat.IPF_RGB_888);
  • Normalize the detected documents from an image file:

    if (resultArray != null)
    {
        foreach (Result result in resultArray)
        {
            if (result.Points != null)
            {
                NormalizedImage image = scanner.NormalizeFile(filename, result.Points);
                if (image != null)
                {
                    image.Save(DateTime.Now.ToFileTimeUtc() + ".png");
                }
            }
    
        }
    }
  • Normalize the detected documents from a buffer:

    if (resultArray != null)
    {
        foreach (DocumentScanner.Result result in resultArray)
        {
            if (result.Points != null)
            {
                int length = mat.Cols * mat.Rows * mat.ElemSize();
                byte[] bytes = new byte[length];
                Marshal.Copy(mat.Data, bytes, 0, length);
    
                DocumentScanner.NormalizedImage image = scanner.NormalizeBuffer(bytes, mat.Cols, mat.Rows, (int)mat.Step(), DocumentScanner.ImagePixelFormat.IPF_RGB_888, result.Points);
                if (image != null && image.Data != null)
                {
                    image.Save(DateTime.Now.ToFileTimeUtc() + ".png");
                }
            }
    
        }
    }
  • Get SDK version number:

    string? version = DocumentScanner.GetVersionInfo();
  • Customize the parameters:

    // Refer to https://www.dynamsoft.com/document-normalizer/docs/parameters/parameter-organization-structure.html?ver=latest
    scanner.SetParameters(DocumentScanner.Templates.color);

Quick Start

using System;
using System.Runtime.InteropServices;
using Dynamsoft;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            DocumentScanner.InitLicense("LICENSE-KEY"); // Get a license key from https://www.dynamsoft.com/customer/license/trialLicense?product=ddn
            DocumentScanner? scanner = null;
            try {
                scanner = DocumentScanner.Create();
                scanner.SetParameters(DocumentScanner.Templates.color);
                Console.WriteLine("Please enter an image file: ");
                string? filename = Console.ReadLine();
                if (filename != null) {
                    Result[]? resultArray = scanner.DetectFile(filename);
                    if (resultArray != null)
                    {
                        foreach (DocumentScanner.Result result in resultArray)
                        {
                            Console.WriteLine("Confidence: " + result.Confidence);
                            if (result.Points != null)
                            {
                                foreach (int point in result.Points)
                                {
                                    Console.WriteLine("Point: " + point);
                                }

                                DocumentScanner.NormalizedImage image = scanner.NormalizeFile("1.png", result.Points);
                                if (image != null)
                                {
                                    image.Save(DateTime.Now.ToFileTimeUtc() + ".png");
                                }
                            }

                        }
                    }
                    else {
                        Console.WriteLine("No document detected.");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

Example

Building NuGet Package from Source Code

# build dll for desktop
cd desktop
dotnet build --configuration Release

# build dll for android
cd android
dotnet build --configuration Release

# build dll for iOS
cd ios
dotnet build --configuration Release

# build nuget package
nuget pack .\DocumentScannerSDK.nuspec