Set Barcode Checksum Verification

Overview

In many 1D and postal symbologies, data integrity verification and decoding rely on checksum control mechanisms. The BarcodeSettings class has the ChecksumValidation property for managing checksum settings for data validation and decoding. Barcode standards can generally be divided into two groups: those that require a checksum and those that have an optional checksum. The ChecksumValidation field can be set to different values, affecting the barcode recognition process based on the checksum settings.

Checksum Validation for Barcodes with Obligatory Checksum

For symbologies with an obligatory checksum, checksum control is always performed when the ChecksumValidation property is set to ChecksumValidation.Default or ChecksumValidation.On. If set to ChecksumValidation.Off, checksum control is disabled, allowing data reading from incorrectly generated barcodes. However, this increases the likelihood of inaccurate recognition.

The following code snippet demonstrates how to configure checksum validation for symbologies with an obligatory checksum (e.g., the Code 11 barcode image shown below).

How to get BarCodeInstance

// Create a Code 11 barcode
var gen = new BarCodeInstance.BarcodeGenerator("Code11", "123456");
gen.Parameters.Barcode.XDimension = "2px";
document.getElementById("img").src = gen.GenerateBarCodeImage(); // Display barcode image

// Read barcode image with ChecksumValidation.Default being set
console.log("ReadChecksumCode11:");
console.log("ChecksumValidation: Default");
var reader = new BarCodeInstance.BarCodeReader(gen.GenerateBarCodeImage(), "Code11");
reader.BarcodeSettings.ChecksumValidation = BarCodeInstance.ChecksumValidation.Default;
reader.ReadBarCodes();
for (var i = 0; i < reader.FoundCount; i++) {
    const result = reader.FoundBarCodes(i);
    console.log(`CodeType: ${result.CodeType}`);
    console.log(`CodeText: ${result.CodeText}`);
    console.log(`1D Value: ${result.Extended.OneD.Value}`);
    console.log(`1D CheckSum: ${result.Extended.OneD.CheckSum}`);
}

// Read barcode image with ChecksumValidation.Off being set
console.log("ChecksumValidation: Off");
var reader = new BarCodeInstance.BarCodeReader(gen.GenerateBarCodeImage(), "Code11");
reader.BarcodeSettings.ChecksumValidation = BarCodeInstance.ChecksumValidation.Off;
reader.ReadBarCodes();
for (var i = 0; i < reader.FoundCount; i++) {
    const result = reader.FoundBarCodes(i);
    console.log(`CodeType: ${result.CodeType}`);
    console.log(`CodeText: ${result.CodeText}`);
    console.log(`1D Value: ${result.Extended.OneD.Value}`);
    console.log(`1D CheckSum: ${result.Extended.OneD.CheckSum}`);
}

gen.delete();
reader.delete();

Checksum Validation for Barcodes with Optional Checksum

For symbologies with optional checksum control, the ChecksumValidation property can be set to enable checksum checking by using the value ChecksumValidation.On. If the property is set to ChecksumValidation.Default or ChecksumValidation.Off, checksum validation will be skipped.

The following code sample demonstrates how to configure barcode reading with optional checksum (a Code 39 barcode image is used as an example).

How to get BarCodeInstance

// Create a Code 39 Extended barcode
var gen = new BarCodeInstance.BarcodeGenerator("Code39FullASCII", "123456");
gen.Parameters.Barcode.XDimension = "2px";
gen.Parameters.Barcode.IsChecksumEnabled = BarCodeInstance.EnableChecksum.Yes;
document.getElementById("img").src = gen.GenerateBarCodeImage(); // Display barcode image

// Read barcode image with ChecksumValidation.Default being set
console.log("ReadChecksumCode39:");
console.log("ChecksumValidation: Default");
var reader = new BarCodeInstance.BarCodeReader(gen.GenerateBarCodeImage(), "Code39FullASCII");
reader.BarcodeSettings.ChecksumValidation = BarCodeInstance.ChecksumValidation.Default;
reader.ReadBarCodes();
for (var i = 0; i < reader.FoundCount; i++) {
    const result = reader.FoundBarCodes(i);
    console.log(`CodeType: ${result.CodeType}`);
    console.log(`CodeText: ${result.CodeText}`);
    console.log(`1D Value: ${result.Extended.OneD.Value}`);
    console.log(`1D CheckSum: ${result.Extended.OneD.CheckSum}`);
}

// Read barcode image with ChecksumValidation.On being set
console.log("ChecksumValidation: On");
var reader = new BarCodeInstance.BarCodeReader(gen.GenerateBarCodeImage(), "Code39FullASCII");
reader.BarcodeSettings.ChecksumValidation = BarCodeInstance.ChecksumValidation.On;
reader.ReadBarCodes();
for (var i = 0; i < reader.FoundCount; i++) {
    const result = reader.FoundBarCodes(i);
    console.log(`CodeType: ${result.CodeType}`);
    console.log(`CodeText: ${result.CodeText}`);
    console.log(`1D Value: ${result.Extended.OneD.Value}`);
    console.log(`1D CheckSum: ${result.Extended.OneD.CheckSum}`);
}

gen.delete();
reader.delete();