ExtractBarcode - C# .NET 6

This tutorial shows how to detect and extract a barcode from a file using the LEADTOOLS Cloud Services in a C# .NET 6 console application.

Overview  
Summary This tutorial covers how to make ExtractBarcode requests and process the results using the LEADTOOLS Cloud Services in a C# .NET 6 console application.
Completion Time 30 minutes
Project Download tutorial project (2 KB)
Platform LEADTOOLS Cloud Services API
IDE Visual Studio 2022
Language C# .NET 6 or higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Be sure to review the following sites for information about LEADTOOLS Cloud Services API.

Application ID and Password

Create an Account with LEADTOOLS Hosted Cloud Services to obtain both Application ID and Password strings.

Service Plans

LEADTOOLS Service Plan offerings:

Service Plan Description
Free Trial Free Evaluation
Page Packages Prepaid Page Packs
Subscriptions Prepaid Monthly Processed Pages

To further explore the offerings, refer to the LEADTOOLS Hosted Cloud Services page.

To obtain the necessary Application ID and Application Password, refer to Create an Account and Application with the LEADTOOLS Hosted Cloud Services.

Create the Project and Add the Required Dependencies

In Visual Studio, create a new C# .NET 6 Console project, and add the following required NuGet package:

Add the ExtractBarcode Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top.

C#
// Using block at the top 
using System; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using System.IO; 
using Newtonsoft.Json.Linq; 

Add the class string variable called hostedServicesUrl and an async Task called ExtractBarcode(). This sends an ExtractBarcode request to the LEADTOOLS Cloud Services API, if successful a unique identifier (GUID) will be returned and then a query using this GUID will be made.

C#
private static string hostedServicesUrl = "https://azure.leadtools.com/api/"; 
public static async Task ExtractBarcode() 
{ 
    var client = InitClient(); 
 
    //The first page in the file to mark for processing 
    int firstPage = 1; 
 
    //Sending a value of -1 will indicate to the service that all pages in the file should be processed. 
    int lastPage = -1; 
 
    // If using URL to the file 
    string fileURL = "http://demo.leadtools.com/images/cloud_samples/barcode_sample.tif"; 
    string recognitionUrl = string.Format("Recognition/ExtractBarcode?firstPage={0}&lastPage={1}&fileurl={2}", firstPage, lastPage, fileURL); 
    var result = await client.PostAsync(recognitionUrl, null); 
 
    /* 
    //If uploading a file as multi-part content: 
    HttpContent byteContent = new ByteArrayContent(File.ReadAllBytes(@"path/to/file")); 
    byteContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") 
    { 
        Name = "attachment", 
        FileName = "file-name" 
    }; 
    var formData = new MultipartFormDataContent(); 
    formData.Add(byteContent, "formFieldName"); 
 
    string recognitionUrl = string.Format("Recognition/ExtractBarcode?firstPage={0}&lastPage={1}", firstPage, lastPage); 
    var result = await client.PostAsync(recognitionUrl, formData); 
    formData.Dispose(); 
    */ 
 
    if (result.StatusCode == HttpStatusCode.OK) 
    { 
        //Unique ID returned by the services 
        string id = await result.Content.ReadAsStringAsync(); 
        Console.WriteLine("Unique ID returned by the services: " + id); 
        await Query(id, client); 
    } 
    else 
        Console.WriteLine("Request failed with the following response: " + result.StatusCode); 
} 

Next, create an async Task called Query(string id, HttpClient client) that utilizes the GUID provided by the ExtractBarcode task. If successful the response body will contain all the request data in JSON format.

C#
private static async Task Query(string id, HttpClient client) 
{ 
    string queryUrl = string.Format("Query?id={0}", id.ToString()); 
    HttpResponseMessage result; 
    string returnedContent; 
    JObject returnedData; 
    int fileStatus; 
    do 
    { 
        result = await client.PostAsync(queryUrl, null); 
        returnedContent = await result.Content.ReadAsStringAsync(); 
        returnedData = JObject.Parse(returnedContent); 
        fileStatus = (int)returnedData.SelectToken("FileStatus")!; 
        //The file is still being processed -- we will sleep the current thread for 5 seconds before trying again. 
        await Task.Delay(5000); 
    } while (fileStatus == 100 || fileStatus == 123); 
 
    Console.WriteLine("File has finished processing with return code: " + returnedData.SelectToken("FileStatus")); 
    if ((int)returnedData.SelectToken("FileStatus")! != 200) 
        return; 
 
    ParseJson(returnedData.SelectToken("RequestData")!.ToString()); 
} 

Then, create the function ParseJson(string json) to process the returned JSON data.

C#
private static void ParseJson(string json) 
{ 
    JArray array = JArray.Parse(json); 
    foreach (var requestReturn in array) 
    { 
        Console.WriteLine("Service Type: " + requestReturn.SelectToken("ServiceType")); 
        var subArray = JArray.Parse(requestReturn.SelectToken("data")!.ToString()); 
        Console.WriteLine("Returned Data:"); 
 
        foreach (var obj in subArray) 
        { 
            var subObj = (JObject)obj; 
            foreach (var entry in subObj) 
                Console.WriteLine(entry.Key.ToString() + ": " + entry.Value!.ToString()); 
            Console.WriteLine("------------------------------------"); 
        } 
    } 
} 

Finally, create the function InitClient() to create a client connection to request the GUID and JSON data through. Where it states Replace with Application ID and Replace with Application Password be sure to place your Application ID and Password accordingly.

C#
private static HttpClient InitClient() 
{ 
    string AppId = "Replace with Application ID"; 
    string Password = "Replace with Application Password"; 
 
    HttpClient client = new HttpClient(); 
    client.BaseAddress = new Uri(hostedServicesUrl); 
    client.DefaultRequestHeaders.Accept.Clear(); 
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
 
    string authData = string.Format("{0}:{1}", AppId, Password); 
    string authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData)); 
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue); 
 
    return client; 
} 

Run the Project

In order to test run this code be sure to add ExtractBarcode().GetAwaiter().GetResults(); to the static void Main section.

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application displays the extracted barcode information from the returned JSON data.

Extracted Barcode Information

Wrap-up

This tutorial showed how to detect and extract a barcode from a file via the LEADTOOLS Cloud Services API.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.