Barcode Xpress for Java v13.9 - Updated
Try the Barcode Scanner REST API
Getting Started > Try the Barcode Scanner REST API

You can also use the Barcode Scanner REST API to read barcodes on an image without explicitly including Barcode Xpress 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.

Scanning barcodes with the Barcode Scanner REST API is quite simple and requires just three steps:

  1. Upload the image file which contains the barcodes you would like to decode to Accusoft's cloud.
  2. Make a POST request to https://api.accusoft.com/barcode/api/v1/scanners to start the barcode scanner process. This request will provide the ID of the uploaded file to scan, plus any other barcode scanning parameters you wish to specify.
  3. Finally, 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" and any decoded barcodes will be presented as output data.

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", "YourAPIKey"); // <-- 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 both the fileId and affinityToken fields in this response to create a barcode scanner process for your image.

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

Start the Barcode Scanner Process

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

Optionally, parameters for barcode scanning can be set to customize the scanning process.

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

// Extract fileId and Affinity Token from the upload response
string responseBody = await response.Content.ReadAsStringAsync();
var responseData = JsonSerializer.Deserialize<Dictionary<string, string>>(responseBody);
string fileId = responseData["fileId"];
string affinityToken = responseData["affinityToken"];
client.DefaultRequestHeaders.Add("Accusoft-Afinity-Token", affinityToken);

// Build JSON for our process request
var requestJson = new
{
    input = new
    {
        source = new
        {
            fileId = fileId
        }
    }
};

// Start barcode scanner process
var api = "/barcode/api/v1/scanners";
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();
string processId = JsonSerializer.Deserialize<Dictionary<string, dynamic>>(processRequestResponse)["processId"].ToString();

// Wait for the process to be complete
string processStatusResponse = await client.GetStringAsync(api + "/" + processId);
while (JsonSerializer.Deserialize<Dictionary<string, dynamic>>(processStatusResponse)["state"].ToString() == "processing")
{
    Thread.Sleep(1000);
    processStatusResponse = await client.GetStringAsync(api + "/" + processId);
}

Complete Code Example

Below is the complete code example.

using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

// 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", "YourAPIKey"); // <-- 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 and Affinity Token from the upload response
string responseBody = await response.Content.ReadAsStringAsync();
var responseData = JsonSerializer.Deserialize<Dictionary<string, string>>(responseBody);
string fileId = responseData["fileId"];
string affinityToken = responseData["affinityToken"];
client.DefaultRequestHeaders.Add("Accusoft-Afinity-Token", affinityToken);

// Build JSON for our process request
var requestJson = new
{
    input = new
    {
        source = new
        {
            fileId = fileId
        }
    }
};

// Start barcode scanner process
var api = "/barcode/api/v1/scanners";
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();
string processId = JsonSerializer.Deserialize<Dictionary<string, dynamic>>(processRequestResponse)["processId"].ToString();

// Wait for the process to be complete
string processStatusResponse = await client.GetStringAsync(api + "/" + processId);
while (JsonSerializer.Deserialize<Dictionary<string, dynamic>>(processStatusResponse)["state"].ToString() == "processing")
{
    Thread.Sleep(1000);
    processStatusResponse = await client.GetStringAsync(api + "/" + processId);
}

Console.Write(processStatusResponse);


Is this page helpful?
Yes No
Thanks for your feedback.