Skip to content

Barcode 条形码 4.4.0

条形码组件,支持多种条形码格式,可以生成条形码图片或Canvas,本插件使用canvas原生实现,不依赖第三方插件,体积较小。 支持CODE128/EAN13/EAN8/EAN5/EAN2/UPC/UPCA,ITF、pharmacode、codabar暂未支持,后期会增加支持。

基本使用

通过value设置条形码内容,通过format设置条形码格式。

设置条形码尺寸

通过widthheight设置条形码尺寸。

vue
<template>
  <view>
    <up-barcode 
      value="1234567890" 
      :width="300"
      :height="100"
    />
  </view>
</template>

<script setup>
</script>

设置条形码颜色

通过lineColor设置条形码线条颜色,通过[background]设置背景色。

vue
<template>
  <view>
    <up-barcode 
      value="1234567890" 
      line-color="#ff0000"
      background="#f5f5f5"
    />
  </view>
</template>

<script setup>
</script>

显示文本

通过displayValue控制是否显示文本,通过text设置文本内容。

vue
<template>
  <view>
    <up-barcode 
      value="1234567890" 
      :display-value="true"
      text="商品条码"
    />
  </view>
</template>

<script setup>
</script>

禁用Canvas渲染

通过useCanvas控制是否使用Canvas渲染,默认为true。

vue
<template>
  <view>
    <up-barcode 
      value="1234567890" 
      :use-canvas="false"
    />
  </view>
</template>

<script setup>
</script>

Props

参数说明类型默认值可选值
value条码值string | number--
format条码格式stringautoCODE128/CODE128A/CODE128B/CODE128C/EAN13/EAN8/EAN5/EAN2/UPC/UPCA/UPCE/CODE39/ITF/ITF14/MSI/MSI10/MSI11/MSI1010/MSI1110/pharmacode/codabar
width宽度number200-
height高度number80-
displayValue是否显示文本booleantruefalse
text文本内容string--
fontOptions字体选项string--
font字体stringmonospace-
textAlign文本对齐方式stringcenterleft/center/right
textPosition文本位置stringbottomtop/bottom
textMargin文本边距number2-
fontSize字体大小number14-
background背景色string#ffffff-
lineColor条码颜色string#000000-
margin边距number10-
marginTop上边距numberundefined-
marginBottom下边距numberundefined-
marginLeft左边距numberundefined-
marginRight右边距numberundefined-
useCanvas使用canvas还是生成图片booleantruefalse

Events

事件名说明回调参数
rendered渲染完成时触发{ type: 'canvas' | 'image', id?: string, value?: string, path?: string }
error渲染出错时触发error

支持格式说明

格式说明
CODE128最常用的条形码格式,可编码所有ASCII字符
EAN13国际商品条形码,13位数字
EAN8国际商品条形码,8位数字
CODE3939字符条形码,常用于工业领域
UPC统一产品代码

注意事项

  1. 条形码内容需要符合对应格式的规范,否则会报错
  2. 使用Canvas渲染时性能更好,但生成图片可以保存到相册
  3. 文本显示时会自动根据条码尺寸调整位置
  4. 不同格式的条码对内容要求不同,请根据实际需求选择格式