介绍
生成条形码编码数据和布局信息。
使用
ts
import { barcode, calculateBarcodeLayout } from 'sard-uniapp'
const encodings = barcode('123456789012', {
format: 'EAN13',
})
const layout = calculateBarcodeLayout(encodings, () => 0)接口
barcode
ts
function barcode(value: string, options?: BarcodeOptions): BarcodeEncoding[]根据条形码类型返回编码后的条纹数据。不同格式可能会拆成多个编码段,例如 EAN13 和 UPC-A。
calculateBarcodeLayout
ts
function calculateBarcodeLayout(
encodings: BarcodeEncoding[],
measureText: (text: string, options: BarcodeRenderOptions) => number,
): BarcodeLayout根据编码结果和文本测量函数,计算条形码渲染所需的宽高及各段布局。
BarcodeOptions
ts
export interface BarcodeOptions {
format?: BarcodeFormat
width?: number
height?: number
color?: string
background?: string
displayValue?: boolean
text?: string
textPosition?: BarcodeTextPosition
textAlign?: CanvasTextAlign
textMargin?: number
fontStyle?: CanvasFontStyle
fontWeight?: CanvasFontWeight
fontSize?: number
fontFamily?: string
margin?: number
marginTop?: number
marginBottom?: number
marginLeft?: number
marginRight?: number
ean128?: boolean
flat?: boolean
lastChar?: string
}BarcodeEncoding
ts
export interface BarcodeEncoding {
data: string
text: string
options: BarcodeRenderOptions
}BarcodeRenderOptions
ts
export interface BarcodeRenderOptions {
format: BarcodeFormat
width: number
height: number
lineColor: string
background: string
displayValue: boolean
text?: string
textPosition: BarcodeTextPosition
textAlign: CanvasTextAlign
textMargin: number
fontStyle: CanvasFontStyle
fontWeight: CanvasFontWeight
fontSize: number
fontFamily: string
margin: number
marginTop: number
marginBottom: number
marginLeft: number
marginRight: number
ean128?: boolean
flat?: boolean
lastChar?: string
}BarcodeLayout
ts
export interface BarcodeLayout {
width: number
height: number
segments: BarcodeLayoutSegment[]
}BarcodeLayoutSegment
ts
export interface BarcodeLayoutSegment extends BarcodeEncoding {
width: number
height: number
textWidth: number
barcodeWidth: number
barcodePadding: number
}BarcodeFormat
ts
type BarcodeFormat = 'CODE128' | 'EAN13' | 'UPC-A' | 'ITF14'BarcodeTextPosition
ts
type BarcodeTextPosition = 'top' | 'bottom'CanvasTextAlign
ts
type CanvasTextAlign = 'center' | 'end' | 'left' | 'right' | 'start'CanvasTextAlign
ts
type CanvasFontStyle = 'normal' | 'italic' | 'oblique' | (string & {})CanvasFontWeight
ts
type CanvasFontWeight =
| 'normal'
| 'bold'
| number
| 'lighter'
| 'bolder'
| (string & {})