C# Halcon SDK

Getting started with the C# Halcon SDK

Note
The code samples in this tutorial can be copy pasted into VS Code Polyglot Notebooks.

Prerequisites

The C# Halcon SDK is currently published for Halcon 22.11 and 23.05 Progress, both for XL and non-XL versions.

Authorization

  1. First select and install the appropriate DataVision.BeeYard.Sdk.Halcon NuGet package for your version of Halcon.

    #r "nuget:DataVision.BeeYard.Sdk.Halcon-22.11-Progress"
  2. Create a HiveClient to interact with Hive (see the C Sharp SDK getting started section for other options) that uses client credentials to authorize.

    using System.Reflection;
    using DataVision.BeeYard.Sdk;
    using DataVision.BeeYard.Sdk.Shared;
    using DataVision.BeeYard.Sdk.Tokens;
    using System.Text.Json;
    
    var uri = new Uri("https://demo.beeyard.ai/hive/");
    var version = typeof(HiveClient).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
    Console.WriteLine($"Hive client version: {version}");
    var agent = new UserAgent("csharp-halcon-app", version);
    
    var clientCredentials = new ClientCredentials("{client id}", "{client secret}");
    var tokenUri = new Uri("https://demo.beeyard.ai/id/oauth/token");
    var client = HiveClient.CreateWithClientCredentials(uri, tokenUri, clientCredentials, userAgent: agent);

Adding and reading HImage

  1. Let’s first create a cell in the default workspace. We let the backend to generate the cell ID.

    Guid id;
    using (var response = await client.CreateCellAsync(description: "halcon"))
    {
        response.EnsureSuccessStatusCode();
        var cellOutput = await response.ReadPayloadAsync();
        id = cellOutput.Descriptor.Id;
    }
  2. We will load a Halcon example image and add it to the just created cell.

    using HalconDotNet;
    using DataVision.BeeYard.Sdk.Halcon;
    using DataVision.BeeYard.Sdk.Halcon.Images;
    
    var image = new HImage("autobahn");
    var format = new HImageFormat("png fastest");
    using (var response = await client.AddHImageAsync(id, "autobahn.png", image, format))
    {
        response.EnsureSuccessStatusCode();
    }
  3. We can then read the image back.

    using DataVision.BeeYard.Sdk.Halcon.Shared.Responses;
    
    using (var response = await client.ReadFileAsync(id, imageName))
    {
        response.EnsureSuccessStatusCode();
        var image2 = await response.ReadHImageAsync();
        image2.GetImageSize(out int width, out int height);
        Console.WriteLine($"{imageName}: {width} x {height}");
    }