Guideline for extracting VIN from a Barcode Result
This guide outlines how to extract a Vehicle Identification Number (VIN) from OCR barcode scanner results. It includes logic for handling both direct VIN results and barcode-encoded VINs, with appropriate post-processing based on the barcode type.
PLEASE NOTE:
JavaScript is used throughout this article as an example implementation language. The same logic can be adapted to other environments as needed.
VIN Characteristics
A VIN has the following properties:
Length: Exactly 17 characters
Invalid Characters: Must not contain the letters “O”, “I”, or “Q” (to avoid confusion with 0, 1, and 9)
VIN Extraction Processing Flow
Assuming some requirements, the VIN extraction can be done entirely in JavaScript on the app side, and the following should be considered:
1. Determine the Type of Result
Check whether the scan result contains a VIN or a barcode:
If a VIN is returned, no post-processing is necessary.
If a barcode is returned, apply the appropriate post-processing based on the barcode format.
CODE EXAMPLE:
const json = JSON.parse(scanResult);
let resultValue;
if (json && json[0] && json[0].barcodeResult && json[0].barcodeResult.barcodes && json[0].barcodeResult.barcodes[0]) {
resultValue = json[0].barcodeResult.barcodes[0].value;
} else if (json && json[0] && json[0].vinResult && json[0].vinResult.text) {
resultValue = json[0].vinResult.text;
} else {
console.log("JSON does not contain a barcodeResult or vinResult property");
// Handle the case where neither property is present
}
console.log(resultValue);
// Do something with resultValue here2. Validate VIN
If the result appears to be a VIN:
Confirm that it is exactly 17 characters long.
Ensure it does not contain the characters O, I, or Q (case-insensitive).
CODE EXAMPLE:
if (resultValue && resultValue.length === 17 && !/[OIQ]/.test(resultValue)) {
console.log("Validation successful!");
// Do something here if validation is successful
} else {
console.log("Validation failed!");
// Do something here if validation fails
}3. Barcode Postprocessing
If the result is from a barcode, post-processing is required to extract the VIN. VINs are often embedded within additional characters or metadata.
Code39 Barcodes
Code39 barcodes typically return between 17 and 20 characters. Apply the following logic:
Length | Action |
|---|---|
17 | Return and use as-is |
18 | Strip the first character → Use characters 2–18 (return indices 1-17) |
19 | Strip the first and last characters → Use characters 2–18 (return indices 1-17) |
20 | Strip the first two and the last character → Use characters 3–19 (return indices 2-18) |
CODE EXAMPLE:
if (json && json[0] && json[0].barcodeResult && json[0].barcodeResult.barcodes && json[0].barcodeResult.barcodes[0] && json[0].barcodeResult.barcodes[0].format === 'CODE_39') {
resultValue = processCode39String(json[0].barcodeResult.barcodes[0].value);
} else if (json && json[0] && json[0].barcodeResult && json[0].barcodeResult.barcodes && json[0].barcodeResult.barcodes[0] && json[0].barcodeResult.barcodes[0].format === 'QR_CODE') {
resultValue = processQrCodeString(json[0].barcodeResult.barcodes[0].value);
}
function processCode39String(inputString) {
if (inputString.length < 17 || inputString.length > 20) {
return "Error: Input string length should be between 17 and 20 characters long.";
}
if (inputString.length === 17) {
return inputString;
}
if (inputString.length === 18) {
return inputString.slice(1);
}
if (inputString.length === 19) {
return inputString.slice(1, -1);
}
if (inputString.length === 20) {
return inputString.slice(2, -1);
}
}
function processQrCodeString(inputString) {
return inputString.substring(0, 17);
}2D Barcodes (QR, DataMatrix)
Some 2D barcodes (e.g., QR codes, DataMatrix) may include additional data after the VIN. In these cases:
Extract only the first 17 characters (indices 0–16).
PLEASE NOTE:
These code snippets are intended as a starting point and may need to be adapted to your specific application or scanner output format.