You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用ZXing.Net跨平台生成带分隔符的EAN18 SVG码?

跨平台生成带起始/分隔/终止符的EAN18 SVG条码解决方案

方案1:基于ZXing.Net的跨平台SVG生成

ZXing.Net本身是跨平台的,你之前接触的Windows专属方案是依赖了Windows特定组件,换用纯跨平台的核心库+SVG输出绑定即可解决:

  • 安装NuGet包:ZXing.Net(核心条码生成逻辑)和ZXing.Net.Bindings.Svg(SVG格式输出支持),二者均支持Linux、macOS、Android、iOS等所有主流平台。
  • 代码示例:
using ZXing;
using ZXing.Svg;

var barcodeWriter = new BarcodeWriterSvg
{
    Format = BarcodeFormat.ITF_18, // 对应EAN18/ITF-18标准
    Options = new EncodingOptions
    {
        Height = 100,
        Width = 300,
        Margin = 10,
        ITF = new ITFEncodingOptions
        {
            ShowStartStop = true, // 显示起始/终止符
            UseGuardBars = true   // 启用分隔符
        }
    }
};

// 生成SVG内容
var svgContent = barcodeWriter.Write("123456789012345678");
// 保存到文件或直接输出
File.WriteAllText("ean18.svg", svgContent.Content);

方案2:使用BarcodeLib.NetStandard

如果ZXing.Net的输出样式不符合需求,可尝试BarcodeLib的跨平台版本,它原生支持EAN18和SVG输出:

  • 安装NuGet包:BarcodeLib.NetStandard
  • 代码示例:
using BarcodeLib;

var barcode = new Barcode();
// 生成SVG格式的EAN18条码,包含所需符号
var svgString = barcode.EncodeAsSvg(
    TYPE.EAN18, 
    "123456789012345678", 
    Color.Black, 
    Color.White, 
    300, 
    100
);
File.WriteAllText("ean18.svg", svgString);

方案3:手动生成SVG(完全自定义样式)

如果现有库的输出样式无法匹配你的需求,可手动编写SVG代码:

  1. 按照EAN18的编码规则,计算每个条码条的宽度、位置
  2. 用SVG的<rect>元素绘制条码主体,再添加起始符(窄-宽-窄)、分隔符(宽-窄-宽)、终止符(窄-宽-窄)
  3. 基础SVG结构示例:
<svg width="300" height="100" xmlns="http://www.w3.org/2000/svg">
    <!-- 起始符 -->
    <rect x="10" y="10" width="2" height="80" fill="black"/>
    <rect x="14" y="10" width="4" height="80" fill="black"/>
    <rect x="20" y="10" width="2" height="80" fill="black"/>
    <!-- 左侧条码区域(根据编码生成对应条) -->
    <!-- ... -->
    <!-- 分隔符 -->
    <rect x="150" y="10" width="4" height="80" fill="black"/>
    <rect x="156" y="10" width="2" height="80" fill="black"/>
    <rect x="160" y="10" width="4" height="80" fill="black"/>
    <!-- 右侧条码区域(根据编码生成对应条) -->
    <!-- ... -->
    <!-- 终止符 -->
    <rect x="280" y="10" width="2" height="80" fill="black"/>
    <rect x="284" y="10" width="4" height="80" fill="black"/>
    <rect x="290" y="10" width="2" height="80" fill="black"/>
</svg>

这种方式完全可控,适配所有平台,只需输出字符串即可。

内容的提问来源于stack exchange,提问作者Melon NG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 21:23:12