Set Target Barcode Types

This guide provides an overview of setting target barcode types using the Aspose.BarCode.Cloud SDK for Node.js. Using this SDK, you can specify the barcode types you want to recognize when sending requests, including formats like Aztec, QR, Pdf417, and many others.

Overview of RecognizeApi

The RecognizeApi provides methods for recognizing barcodes from various data inputs. You can choose to send barcode recognition requests using either a file URL, a base64 encoded image, or a multipart form request. By specifying the barcode type and recognition settings, you can optimize the API to target the specific barcode formats your application needs.

Supported Barcode Types

The DecodeBarcodeType enum allows for a variety of barcode types, including:

  • Aztec
  • QR
  • Code128
  • DataMatrix
  • Pdf417
  • EAN13
  • …and many more.

For full supported barcode list see the decode type github page

Example: Specifying Target Barcode Types

Using recognize

This method sends a GET request with the file URL and barcode type in the query parameters.

const fs = require('fs');
const path = require('path');
const Barcode = require('aspose-barcode-cloud-node');

function makeConfiguration() {
    const envToken = process.env['TEST_CONFIGURATION_ACCESS_TOKEN'];
    if (!envToken) {
        return new Barcode.Configuration(
            'Client Id from https://dashboard.aspose.cloud/applications',
            'Client Secret from https://dashboard.aspose.cloud/applications',
            null,
            null
        );
    } else {
        return new Barcode.Configuration(
            null,
            null,
            null,
            envToken
        );
    }
}
const config = makeConfiguration();

async function recognizeBarcode(api) {
    const request = new Barcode.RecognizeRequestWrapper(
        Barcode.DecodeBarcodeType.MostCommonlyUsed,
        "https://products.aspose.app/barcode/scan/img/how-to/scan/step2.png"
    );
    const result = await api.recognize(request);
    return result.body.barcodes;
}

const recognizeApi = new Barcode.RecognizeApi(config);

recognizeBarcode(recognizeApi)
    .then(barcodes => {
        console.log(`File recognized, result: '${barcodes[0].barcodeValue}'`);
    })
    .catch(err => {
        console.error("Error: " + JSON.stringify(err, null, 2));
        process.exitCode = 1;
    });

In this example, most commonly used barcode symbologies will be recognized from the provided image URL.

Using recognizeBase64 with multiple Barcode Types

For base64-encoded images, use the recognizeBase64 method, which allows for flexible recognition across multiple barcode types.

const fs = require('fs');
const path = require('path');
const Barcode = require('aspose-barcode-cloud-node');

function makeConfiguration() {
    const envToken = process.env['TEST_CONFIGURATION_ACCESS_TOKEN'];
    if (!envToken) {
        return new Barcode.Configuration(
            'Client Id from https://dashboard.aspose.cloud/applications',
            'Client Secret from https://dashboard.aspose.cloud/applications',
            null,
            null
        );
    } else {
        return new Barcode.Configuration(
            null,
            null,
            null,
            envToken
        );
    }
}

async function recognizeBarcode(api, fileName) {
    const imageBytes = fs.readFileSync(fileName);
    const imageBase64 = Buffer.from(imageBytes).toString('base64');
    const recognizeBase64Request = new Barcode.RecognizeBase64Request();
    recognizeBase64Request.barcodeTypes = [Barcode.DecodeBarcodeType.Qr, Barcode.DecodeBarcodeType.Pdf417];
    recognizeBase64Request.fileBase64 = imageBase64;
    const RecognizeRequestWrapper = new Barcode.RecognizeBase64RequestWrapper(recognizeBase64Request);
    const result = await api.recognizeBase64(RecognizeRequestWrapper);
    return result.body.barcodes;
}

const recognizeApi = new Barcode.RecognizeApi(makeConfiguration());

const fileName = path.resolve('testdata','QR_and_Code128.png');

recognizeBarcode(recognizeApi, fileName)
    .then(barcodes => {
        console.log(`File '${fileName}' recognized, results:`);
        barcodes.forEach(result => {
            console.log(`Value: '${result.barcodeValue}', type: ${result.type}`);
        });
    })
    .catch(err => {
        console.error("Error: " + JSON.stringify(err, null, 2));
        process.exitCode = 1;
    });

In this example, the API will target both QR and Pdf417 barcode types in the base64 image string.

Using recognizeMultipart

For recognizing barcodes from files in a form upload, use recognizeMultipart. This method supports setting a single barcode type.

const fs = require('fs');
const path = require('path');
const Barcode = require('aspose-barcode-cloud-node');

function makeConfiguration() {
    const envToken = process.env['TEST_CONFIGURATION_ACCESS_TOKEN'];
    if (!envToken) {
        return new Barcode.Configuration(
            'Client Id from https://dashboard.aspose.cloud/applications',
            'Client Secret from https://dashboard.aspose.cloud/applications',
            null,
            null
        );
    } else {
        return new Barcode.Configuration(
            null,
            null,
            null,
            envToken
        );
    }
}
const config = makeConfiguration();

async function recognizeBarcode(api, fileName) {
    const imageBuffer = fs.readFileSync(fileName);

    const RecognizeRequestWrapper = new Barcode.RecognizeMultipartRequestWrapper(
        Barcode.DecodeBarcodeType.Pdf417,
        imageBuffer
    );
    RecognizeRequestWrapper.recognitionImageKind = Barcode.RecognitionImageKind.ClearImage;
  
    const result = await api.recognizeMultipart(RecognizeRequestWrapper);
    
    return result.body.barcodes;
}

const recognizeApi = new Barcode.RecognizeApi(config);

const fileName = path.resolve('testdata','Pdf417.png');

recognizeBarcode(recognizeApi, fileName)
    .then(barcodes => {
        console.log(`File '${fileName}' recognized, result: '${barcodes[0].barcodeValue}'`);
    })
    .catch(err => {
        console.error("Error: " + JSON.stringify(err, null, 2));
        process.exitCode = 1;
    });

This example sets the target type to Pdf417, recognizing only this type in the uploaded image file.

Conclusion

By specifying target barcode types in your RecognizeApi requests, you can optimize performance and precision based on your application’s needs. Setting a DecodeBarcodeType for your recognition tasks ensures that only specific barcode formats are detected, improving accuracy and efficiency.