# 条形码生成器 — 开发者库

18 种条形码（Code 128、Code 39、EAN、UPC、ITF、MSI、Pharmacode、Codabar），支持标签文字、校验位、批量编号、CSV 和可打印标签页。可导出为 SVG、PNG、JPEG、WebP、PDF 和 ZIP。

## 安装

```sh
npm install @openlibsdev/barcode-generator
```

```js
import BarcodeGenerator from "https://cdn.jsdelivr.net/npm/@openlibsdev/barcode-generator@1.0.0/+esm";
```

## 用法

```js
import BarcodeGenerator from "@openlibsdev/barcode-generator";

const s = BarcodeGenerator.normalize({ format: "EAN13", data: "590123412345" });
console.log(BarcodeGenerator.isValid(s, s.data));   // true
const svg = BarcodeGenerator.svgFor(s, s.data);     // SVG markup as a string
document.body.insertAdjacentHTML("beforeend", svg);
await BarcodeGenerator.exportFile(s, "png");        // downloads the file
```

## API

### `BarcodeGenerator.normalize(input)`

由部分设置对象生成完整设置：缺少的字段使用默认值。

### `BarcodeGenerator.isValid(s, value)`

按所选条码类型校验数值（长度、字符、校验位）。

### `BarcodeGenerator.svgFor(s, value)`

带标签文字的条形码，SVG 标记形式。

### `BarcodeGenerator.drawSVG(s, value, svg)`

在已有的 <svg> 元素中绘制条形码。

### `BarcodeGenerator.canvasFor(s, value, opts)`

<canvas> 形式的条形码。

### `BarcodeGenerator.exportFile(s, kind, opts)`

下载单个条码或整批：svg、png、jpeg、webp、pdf、zip。

### `BarcodeGenerator.printLayout(s)`

生成用于打印的标签页。

### `BarcodeGenerator.values(s)`

整批的所有数值：单个值、列表或自动编号。

### `BarcodeGenerator.autonumber(o)`

从起始值按步长和前缀生成编号。

### `BarcodeGenerator.csvValues(text)`

从 CSV 文本读取数值。

### `BarcodeGenerator.toCsv(vals)`

将数值写为 CSV 文本。

### `BarcodeGenerator.contrastOk(s)`

条和背景的颜色对比度是否足以被扫描器识别。

### `BarcodeGenerator.FORMATS`

条形码类型及其规则。

### `BarcodeGenerator.PRESETS`

现成的标签样式。

### `BarcodeGenerator.DEFAULTS`

默认设置。

### `BarcodeGenerator.findNamedColor(text, lang)`

按指定语言的颜色名称查找命名颜色（“深蓝色”→ #00008b）。

### `BarcodeGenerator.listNamedColors(lang)`

列出命名颜色及其在指定语言中的名称。

### `BarcodeGenerator.APP`

库的名称、版本和发布日期：{name, version, created}。

### `BarcodeGenerator.BACKDROPS`

按 id 排列的现成背景，为 SVG 图像。

### `BarcodeGenerator.BACKDROP_GROUPS`

按组排列的背景：[组, [[id, 英文名, 设置], …]]。

### `BarcodeGenerator.FORMAT_GROUPS`

按组排列的条码类型：通用、零售、其他。

### `BarcodeGenerator.LH`

比例为 1 时标签的高度（像素）。

### `BarcodeGenerator.LW`

比例为 1 时标签的宽度（像素）。

### `BarcodeGenerator.MAX_VALUES`

一批中最多的值数（200）。

### `BarcodeGenerator.backdropSrc(s)`

所选背景的图像（现成的或上传的图片），或空字符串。

### `BarcodeGenerator.cardOn(s)`

是否在码下绘制白色卡片（在背景上时取决于开关，在标签上始终绘制）。

### `BarcodeGenerator.contrastRatio(a, b)`

两种颜色的对比度（1–21，与 WCAG 相同）。

### `BarcodeGenerator.fileName(s, value, ext)`

某个值的文件名：类型_值.扩展名，文件名中不允许的字符会被替换。

### `BarcodeGenerator.hasBackdrop(s)`

是否已选择背景。

### `BarcodeGenerator.hasLabel(s)`

标签是否有标题、价格或徽标。

### `BarcodeGenerator.isPicture(s)`

结果是否为图片（背景或标签），而不只是码本身。

### `BarcodeGenerator.namedColorTexts(lang)`

指定语言的颜色字段标签（取色器的文本），缺少该语言时为英文。

### `BarcodeGenerator.textFontData`

文本和字体区块的数据：字体列表、加载字形样式、添加字体文件、某种语言的区块标签以及默认值。

### `BarcodeGenerator.validValues(s)`

该批中对所选类型有效的值。

## 依赖

- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @imagemagick/magick-wasm 0.0.35 (Apache-2.0)
- @simonwep/pickr 1.10.2 (MIT)
- @simonwep/pickr 1.10.2 (MIT)
- dejavu-fonts-ttf 2.37.3 (Bitstream Vera Fonts License (free, redistributable); DejaVu changes are public domain)
- imagetracerjs 1.2.6 (Unlicense)
- jsbarcode 3.11.6 (MIT)
- jspdf 2.5.2 (MIT)
- jszip 3.10.1 ((MIT OR GPL-3.0-or-later))

- GitHub: https://github.com/OpenLibsDev/barcode-generator
- npm: https://www.npmjs.com/package/@openlibsdev/barcode-generator
- 打开应用: https://openlibs.dev/zh-Hans/app/barcode-generator/
