UPC-A barcodes
UPC-A encodes 12 digits: 11 data digits and a check digit. Open Barcode computes the check digit, or verifies it when you pass all 12 digits.
Input rules
- Pass 11 digits to have the check digit calculated, or 12 digits to have it verified.
- Check digit: multiply the digits in odd positions (1st, 3rd, ... 11th) by 3, add the digits in even positions, and take the amount needed to reach the next multiple of 10.
- A wrong check digit raises an error instead of producing a barcode.
Example
Input 03600029145 encodes as 036000291452. The barcode is 95 modules wide.
- The example 03600029145 becomes 036000291452 with check digit 2.
- The output is 95 modules, including the start, centre and end guard patterns.
Generate it in JavaScript
The same pattern applies in every language. Here is JavaScript as an example.
import { UPC } from "@openbarcode/javascript/linear";
const modules = new UPC("03600029145").barcode();Draw it as SVG
The library returns the module pattern, so you draw it yourself. This example writes one SVG bar for each module of a UPC-A barcode.
import { UPC } from "@openbarcode/javascript/linear";
const modules = new UPC("03600029145").barcode();
const quiet = 10;
const width = modules.length + quiet * 2;
const bars = modules
.map((m, i) => (m ? `<rect x="${i + quiet}" width="1" height="100"/>` : ""))
.join("");
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} 100">${bars}</svg>`;Try it in the live generator, which renders this way in your browser.