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

如何让ZXing.NET识别UPC_A扩展码并返回完整条码内容?

问题描述

我尝试解码杂志和漫画上的条码,这类条码有时带有分离的2位或5位扩展码,用来编码期号等信息。我有一张示例图片:
漫画UPC条码示例

我用ASP.NET Core写了一段验证代码:

[HttpPost]
public string Post([FromForm] IFormFile file)
{
    string barcode = string.Empty;
    using (var stream = file.OpenReadStream())
    {
        try
        {
            stream.Seek(0, SeekOrigin.Begin);
            using var coreCompatImage = (Bitmap)Bitmap.FromStream(stream);
            var coreCompatReader = new ZXing.Windows.Compatibility.BarcodeReader();
            coreCompatReader.Options.PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.UPC_A };
            var coreCompatResult = coreCompatReader.Decode(coreCompatImage);
            var multipleCodes = coreCompatReader.DecodeMultiple(coreCompatImage);
            barcode = coreCompatResult?.Text ?? string.Empty;
        }
        catch (Exception exc)
        {
            return "Error processing the image: ";
        }
    }
    return barcode;
}

原本期望multipleCodes能识别两个条码,但目前只能获取到前12位,怎么让ZXing返回完整内容75966066620843200131而非759660666208432?

解决方案
  • 补充条码格式支持:
    漫画的扩展码属于UPC-EAN体系的扩展码,你当前只指定了BarcodeFormat.UPC_A,需要把BarcodeFormat.EAN_13也加入格式列表,让ZXing能识别分离的扩展码:

    coreCompatReader.Options.PossibleFormats = new List<BarcodeFormat> 
    { 
        BarcodeFormat.UPC_A, 
        BarcodeFormat.EAN_13 
    };
    
  • 拼接多条码结果:
    DecodeMultiple会返回图片中所有识别到的条码对象集合,你需要遍历集合并拼接每个条码的文本,才能得到完整的组合内容。修改结果处理逻辑:

    var multipleCodes = coreCompatReader.DecodeMultiple(coreCompatImage);
    if (multipleCodes != null && multipleCodes.Any())
    {
        barcode = string.Join("", multipleCodes.Select(r => r.Text));
    }
    
  • 优化识别精度:
    如果扩展码识别困难,可以启用TryHarder选项提升识别精度:

    coreCompatReader.Options.TryHarder = true;
    

完整修改后的代码示例:

[HttpPost]
public string Post([FromForm] IFormFile file)
{
    string barcode = string.Empty;
    using (var stream = file.OpenReadStream())
    {
        try
        {
            stream.Seek(0, SeekOrigin.Begin);
            using var coreCompatImage = (Bitmap)Bitmap.FromStream(stream);
            var coreCompatReader = new ZXing.Windows.Compatibility.BarcodeReader();
            // 配置支持的条码格式
            coreCompatReader.Options.PossibleFormats = new List<BarcodeFormat> 
            { 
                BarcodeFormat.UPC_A, 
                BarcodeFormat.EAN_13 
            };
            // 启用高精度识别模式
            coreCompatReader.Options.TryHarder = true;
            // 获取所有条码并拼接内容
            var multipleCodes = coreCompatReader.DecodeMultiple(coreCompatImage);
            if (multipleCodes != null && multipleCodes.Any())
            {
                barcode = string.Join("", multipleCodes.Select(r => r.Text));
            }
        }
        catch (Exception exc)
        {
            return $"Error processing the image: {exc.Message}";
        }
    }
    return barcode;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 23:25:55