ImageGear v26.3 - Updated
Getting Started / Try the Image Editor REST API
In This Topic
    Try the Image Editor REST API
    In This Topic

    You can also use the Image Editor REST API to perform some common operations on images without explicitly including ImageGear in your application. To do so, you will need an Accusoft Cloud account. Free trial accounts start off with 300 free transactions allowing you to start uploading files and searching for barcodes right away.

    Processing images with the Image Editor REST API is quite simple and requires just three steps:

    1. Upload the image file you would like to edit to Accusoft's cloud.
    2. Make a POST request to https://api.accusoft.com/imageGear/api/v1/imageEditors to start the image editor process. This request will provide the ID of the uploaded image file, the operations to be performed, and any other parameters you wish to specify.
    3. Make a GET requests to check the status of the barcode scanner process. Once the process has been completed, the "state" field returned in the response body will be "complete" or "error". Provided the process completed successfully, the response will contain the fileId of the output image created by the operations in your process.
    4. Download the resulting output image from Accusoft's cloud.

    Below, you will find C# code snippets carrying out these steps which illustrate how to use the Accusoft Image Editor REST API. You can also find complete code for this sample at the bottom of the page.

    Upload a Workfile

    Before scanning an image for barcodes, you must first upload that image to the Accusoft Cloud and get some identifiers which will be required for submitting the image to the Barcode Scanner for processing.

    // Create a HTTP Client
    var client = new HttpClient();
    var baseUrl = @"https://api.accusoft.com";
    client.BaseAddress = new Uri(baseUrl);
    client.DefaultRequestHeaders.Add("acs-api-key", "YourAPIKeyHere..."); // <-- API Key Here
    
    // Upload image which contains the barcodes we wish to scan
    var workfileApi = @"/PCCIS/V1/WorkFile?FileExtension=bmp";
    var content = new StreamContent(File.OpenRead(@"./yourimage.bmp")); // <-- Image path here
    content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");
    var response = await client.PostAsync(workfileApi, content);
    

    If your file was uploaded successfully, you will recieve back a 200 status code and a JSON response that contains fields that we will need for subsequent requests. It should look something like this:

    {
        "fileId":"qH3ZfEYx8nmYa1jfITU7TA",
        "fileExtension":"bmp",
        "affinityToken":"CKHiMTSA/uIa0VemKX9Yz6nksNSKS3WGsVEiI6G3ZKV3f2pZfeEsmgW333JUvIyExD7E+ga/0m6LBixT8gGZFtCJtusgy01tUhrQBghQq9ERnhf1hEfsjUWSytGmjqhB"
    }
    

    You will need the fileId value in this response to create an image editor process.

    For further information on using work files, see our How To Use Work Files and Work File API topics.

    Start the Image Editor Process

    You can now start the image editor process with a POST request to https://api.accusoft.com/barcode/api/v1/scanners which includes the work file ID of the image you just uploaded, the operations to be performed, and the same API key you used in your previous request.

    If this request is successful, it will return an ID for the image editor process. Use this ID in the proceeding GET request.

    // Extract fileId from the upload response.
    string responseBody = await response.Content.ReadAsStringAsync();
    Console.WriteLine(responseBody);
    var responseData = JsonNode.Parse(responseBody)!;
    string fileId = responseData["fileId"]!.ToString();
    
    // Build JSON for our process request
    var requestJson = new
    {
        input = new
        {
            source = new
            {
                fileId
            },
            operations = new[]
            {
                new 
                {
                    type = "crop",
                    top = 0,
                    left = 0,
                    width = 200,
                    height = 200
                }
            }
        }
    };
    
    // Start barcode scanner process
    var api = "/imageGear/api/v1/imageEditors"; 
    var processRequestContent = new StringContent(JsonSerializer.Serialize(requestJson), Encoding.UTF8, "application/json");
    var processRequest = await client.PostAsync(api, processRequestContent); 
    
    

    Obtain the Barcode Scanner Results

    To obtain the results of the barcode scanner process, make a GET request to https://api.accusoft.com/barcode/api/v1/scanners/{processId}. The response body will contain the state of the barcode scanner process. The state field can be one of three values: "processing", "complete", or "error".

    If the state is "processing", it means that the image has not yet been fully scanned and decoded, and you must wait until it is either "complete" or "error" before sending the request again.

    Once the state is "complete", the response body will also contain JSON results of the barcode scanner process.

    // After our POST request, the response should contain a processId that we will use later.
    string processRequestResponse = await processRequest.Content.ReadAsStringAsync();
    Console.WriteLine(processRequestResponse);
    string processId = JsonNode.Parse(processRequestResponse)!["processId"]!.ToString();
    
    // Wait for the process to be complete
    string processStatusResponse = await client.GetStringAsync(api + "/" + processId);
    while (JsonNode.Parse(processStatusResponse)!["state"]!.ToString() == "processing")
    {
        Thread.Sleep(1000);
        processStatusResponse = await client.GetStringAsync(api + "/" + processId);
    }
    

    Download Resulting Image

    Just like how we extracted the fileId of the image we originally uploaded to the work file service, we can now take the fileId of our output image and use it to download our newly processed file:

    // Get Output File
    var responseObject = JsonNode.Parse(processStatusResponse);
    var outputWorkfileId = responseObject!["output"]!["workfileId"]!.ToString();
    HttpResponseMessage downloadRequest = await client.GetAsync(@"/PCCIS/V1/WorkFile/" + outputWorkfileId);
    using (var filestream = new FileStream(outputWorkfileId + ".bmp", FileMode.Create))
    {
        await downloadRequest.Content.CopyToAsync(filestream);
    }
    

    Complete Code Example

    Below is the complete code example.

    using System.Net.Http.Headers;
    using System.Text;
    using System.Text.Json;
    using System.Text.Json.Nodes;
    
    // Create a HTTP Client
    var client = new HttpClient();
    var baseUrl = @"https://api.accusoft.com";
    client.BaseAddress = new Uri(baseUrl);
    client.DefaultRequestHeaders.Add("acs-api-key", "YourAPIKeyHere..."); // <-- API Key Here
    
    // Upload image which contains the barcodes we wish to scan
    var workfileApi = @"/PCCIS/V1/WorkFile?FileExtension=bmp";
    var content = new StreamContent(File.OpenRead(@"./yourimage.bmp")); // <-- Image path here
    content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");
    var response = await client.PostAsync(workfileApi, content);
    
    // Extract fileId from the upload response.
    string responseBody = await response.Content.ReadAsStringAsync();
    Console.WriteLine(responseBody);
    var responseData = JsonNode.Parse(responseBody)!;
    string fileId = responseData["fileId"]!.ToString();
    
    // Build JSON for our process request
    var requestJson = new
    {
        input = new
        {
            source = new
            {
                fileId
            },
            operations = new[]
            {
                new 
                {
                    type = "crop",
                    top = 0,
                    left = 0,
                    width = 200,
                    height = 200
                }
            }
        }
    };
    
    // Start barcode scanner process
    var api = "/imageGear/api/v1/imageEditors"; 
    var processRequestContent = new StringContent(JsonSerializer.Serialize(requestJson), Encoding.UTF8, "application/json");
    var processRequest = await client.PostAsync(api, processRequestContent); 
    
    // After our POST request, the response should contain a processId that we will use later.
    string processRequestResponse = await processRequest.Content.ReadAsStringAsync();
    Console.WriteLine(processRequestResponse);
    string processId = JsonNode.Parse(processRequestResponse)!["processId"]!.ToString();
    
    // Wait for the process to be complete
    string processStatusResponse = await client.GetStringAsync(api + "/" + processId);
    while (JsonNode.Parse(processStatusResponse)!["state"]!.ToString() == "processing")
    {
        Thread.Sleep(1000);
        processStatusResponse = await client.GetStringAsync(api + "/" + processId);
    }
    
    // Get Output File
    var responseObject = JsonNode.Parse(processStatusResponse);
    var outputWorkfileId = responseObject!["output"]!["workfileId"]!.ToString();
    HttpResponseMessage downloadRequest = await client.GetAsync(@"/PCCIS/V1/WorkFile/" + outputWorkfileId);
    using (var filestream = new FileStream(outputWorkfileId + ".bmp", FileMode.Create))
    {
        await downloadRequest.Content.CopyToAsync(filestream);
    }