本文作者:icy

ZXing.Delphi:Delphi/Pascal平台上的全能条码处理库

icy 今天 12 抢沙发
ZXing.Delphi:Delphi/Pascal平台上的全能条码处理库摘要: ZXing.Delphi:Delphi/Pascal平台上的全能条码处理库 项目概述 ZXing.Delphi 是著名开源条码处理库 ZXing(”Zebra Crossing”)...

ZXing.Delphi:Delphi/Pascal平台上的全能条码处理库

ZXing.Delphi:Delphi/Pascal平台上的全能条码处理库

项目概述

ZXing.Delphi 是著名开源条码处理库 ZXing(”Zebra Crossing”)的 Delphi/Pascal 移植版本。该项目将 Java 原版的强大条码生成与识别功能完整地带到了 Delphi 和 Free Pascal 开发环境中,让 Pascal 开发者能够轻松地在应用程序中集成一维码和二维码处理能力。

核心特性

1. 广泛的条码格式支持

  • 一维码:UPC-A、UPC-E、EAN-8、EAN-13、Code 39、Code 93、Code 128、ITF、Codabar

  • 二维码:QR Code、Data Matrix、Aztec、PDF417、MaxiCode

  • 其他格式:RSS-14、RSS-Expanded

2. 跨平台兼容性

  • 支持 Delphi 7 及以上版本

  • 兼容 Free Pascal/Lazarus

  • 可在 Windows、macOS、Linux 等多平台运行

  • 支持 VCL、FMX 和 LCL 等多种 UI 框架

3. 双向功能

  • 条码识别:从图像文件、位图、摄像头流中读取和解码条码

  • 条码生成:将文本数据编码为各种格式的条码图像

安装与配置

通过 GetIt 安装(Delphi 10.3+)

  1. 打开 Delphi IDE

  2. 选择 Tools → GetIt Package Manager

  3. 搜索 “ZXing.Delphi”

  4. 点击安装

手动安装

  1. 克隆项目到本地:

text
git clone https://github.com/Spelt/ZXing.Delphi.git
  1. 将源码目录添加到 Delphi 的库路径

  2. 在项目中添加必要的单元引用

实用示例

示例 1:生成二维码

text
uses
  ZXing.BarcodeFormat, ZXing.EncodeHintType, ZXing.QrCode.Internal,
  ZXing.Renderer, Vcl.Graphics;

procedure GenerateQRCode(const Text: string; Bitmap: TBitmap);
var
  QRCodeWriter: TQRCodeWriter;
  EncodeHints: TDictionary<TEncodeHintType, TObject>;
  BitMatrix: IBitMatrix;
  Width, Height: Integer;
begin
  // 创建二维码编码器
  QRCodeWriter := TQRCodeWriter.Create;
  try
    // 设置编码参数
    EncodeHints := TDictionary<TEncodeHintType, TObject>.Create;
    try
      EncodeHints.Add(TEncodeHintType.CHARACTER_SET, 'UTF-8');
      EncodeHints.Add(TEncodeHintType.ERROR_CORRECTION, 
        TErrorCorrectionLevel.Create(TErrorCorrectionLevel.M));
      
      // 生成位矩阵
      BitMatrix := QRCodeWriter.encode(Text, 
        TBarcodeFormat.QR_CODE, 300, 300, EncodeHints);
      
      // 渲染到位图
      Width := BitMatrix.Width;
      Height := BitMatrix.Height;
      Bitmap.SetSize(Width, Height);
      
      // 绘制二维码
      for var y := 0 to Height - 1 do
        for var x := 0 to Width - 1 do
        begin
          if BitMatrix[x, y] then
            Bitmap.Canvas.Pixels[x, y] := clBlack
          else
            Bitmap.Canvas.Pixels[x, y] := clWhite;
        end;
    finally
      EncodeHints.Free;
    end;
  finally
    QRCodeWriter.Free;
  end;
end;

示例 2:识别图像中的条码

text
uses
  ZXing.ScanManager, ZXing.BarcodeFormat, ZXing.ResultPoint,
  Vcl.Graphics, System.SysUtils;

function DecodeBarcodeFromFile(const FileName: string): string;
var
  Bitmap: TBitmap;
  ScanManager: TScanManager;
  Result: TReadResult;
  Formats: TBarcodeFormatSet;
begin
  Result := '';
  
  if not FileExists(FileName) then
    Exit;
    
  Bitmap := TBitmap.Create;
  try
    Bitmap.LoadFromFile(FileName);
    
    // 设置要识别的条码格式
    Formats := [TBarcodeFormat.QR_CODE, 
                TBarcodeFormat.CODE_128,
                TBarcodeFormat.EAN_13];
    
    // 创建扫描管理器
    ScanManager := TScanManager.Create(Formats, nil);
    try
      // 尝试解码
      Result := ScanManager.Scan(Bitmap);
      
      if Assigned(Result) then
      begin
        // 获取解码结果
        Result := Result.Text;
        
        // 可选:获取条码位置点
        if Result.ResultPoints <> nil then
        begin
          for var Point in Result.ResultPoints do
          begin
            // 处理每个识别点
            WriteLn(Format('Point: (%.2f, %.2f)', 
              [Point.x, Point.y]));
          end;
        end;
      end;
    finally
      ScanManager.Free;
      if Assigned(Result) then
        Result.Free;
    end;
  finally
    Bitmap.Free;
  end;
end;

示例 3:生成带Logo的二维码

text
procedure GenerateQRCodeWithLogo(const Text: string; 
  LogoFile: string; OutputBitmap: TBitmap);
var
  QRCodeBitmap: TBitmap;
  LogoBitmap: TBitmap;
  X, Y: Integer;
begin
  // 先生成基础二维码
  QRCodeBitmap := TBitmap.Create;
  try
    GenerateQRCode(Text, QRCodeBitmap);
    
    // 加载Logo
    LogoBitmap := TBitmap.Create;
    try
      if FileExists(LogoFile) then
      begin
        LogoBitmap.LoadFromFile(LogoFile);
        
        // 调整Logo大小(约为二维码的1/5)
        LogoBitmap.Width := QRCodeBitmap.Width div 5;
        LogoBitmap.Height := QRCodeBitmap.Height div 5;
        
        // 计算Logo位置(居中)
        X := (QRCodeBitmap.Width - LogoBitmap.Width) div 2;
        Y := (QRCodeBitmap.Height - LogoBitmap.Height) div 2;
        
        // 合并图像
        OutputBitmap.Assign(QRCodeBitmap);
        OutputBitmap.Canvas.Draw(X, Y, LogoBitmap);
      end
      else
      begin
        OutputBitmap.Assign(QRCodeBitmap);
      end;
    finally
      LogoBitmap.Free;
    end;
  finally
    QRCodeBitmap.Free;
  end;
end;

高级功能

1. 摄像头实时扫描

ZXing.Delphi 支持通过摄像头进行实时条码扫描,特别适合开发零售、库存管理等应用:

text
uses
  ZXing.ScanManager, ZXing.DecodeHintType;

procedure SetupCameraScanner;
var
  Camera: TCameraComponent;
  ScanManager: TScanManager;
  Hints: TDecodeHintType;
begin
  Camera := TCameraComponent.Create(nil);
  ScanManager := TScanManager.Create(TBarcodeFormat.QR_CODE, Hints);
  
  Camera.OnSampleBufferReady := 
    procedure(Sender: TObject; const ATime: TMediaTime)
    begin
      // 处理摄像头帧并尝试解码
      var Frame := Camera.SampleBufferToBitmap(nil);
      var Result := ScanManager.Scan(Frame);
      if Assigned(Result) then
      begin
        // 处理解码结果
        ProcessBarcodeResult(Result.Text);
      end;
    end;
end;

2. 批量条码生成

text
procedure GenerateBarcodeBatch(const DataList: TStringList; 
  Format: TBarcodeFormat; OutputDir: string);
var
  Writer: IBarcodeWriter;
  Bitmap: TBitmap;
begin
  Writer := TBarcodeWriter.Create;
  Writer.Format := Format;
  
  for var i := 0 to DataList.Count - 1 do
  begin
    Bitmap := Writer.Write(DataList[i]);
    try
      Bitmap.SaveToFile(
        IncludeTrailingPathDelimiter(OutputDir) + 
        'barcode_' + IntToStr(i) + '.bmp');
    finally
      Bitmap.Free;
    end;
  end;
end;

性能优化建议

  1. 图像预处理:在识别前对图像进行灰度化、二值化处理

  2. 区域限制:如果知道条码的大致位置,可以限制扫描区域

  3. 格式过滤:只启用需要的条码格式以提升识别速度

  4. 缓存机制:对频繁生成的相同内容条码使用缓存

实际应用场景

企业应用

  • 库存管理系统:使用 PDA 或手机扫描商品条码

  • 文档管理:为文档生成唯一标识二维码

  • 门禁系统:二维码访客通行证

移动应用

  • 电子票务:生成和验证活动门票

  • 支付系统:生成支付二维码

  • 产品溯源:扫描产品二维码查看详细信息

桌面应用

  • 标签打印软件:批量生成商品标签

  • 数据采集系统:通过扫描枪输入数据

  • 资产管理系统:为设备生成资产标签

项目优势

  1. 成熟稳定:基于久经考验的 ZXing 库

  2. 完全开源:遵循 Apache License 2.0 协议

  3. 活跃维护:定期更新,修复问题

  4. 文档完善:提供详细的使用示例和 API 文档

  5. 社区支持:拥有活跃的用户社区和问题讨论区

结语

ZXing.Delphi 为 Pascal 开发者提供了一个强大、可靠的条码处理解决方案。无论是需要简单的二维码生成,还是复杂的多格式条码识别系统,这个库都能满足需求。其跨平台特性和丰富的功能集使其成为 Delphi/Pascal 生态系统中条码处理的首选工具。

通过本文介绍的示例代码,开发者可以快速上手并集成到自己的项目中,为应用程序增添专业的条码处理能力。随着物联网和移动应用的普及,条码技术的应用场景只会越来越广泛,掌握 ZXing.Delphi 的使用将为你的项目带来重要价值。

ZXing.Delphi.zip
类型:压缩文件|已下载:1|下载方式:免费下载
立即下载
文章版权及转载声明

作者:icy本文地址:https://zelig.cn/2026/04/466.html发布于 今天
文章转载或复制请以超链接形式并注明出处软角落-SoftNook

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

阅读
分享

发表评论

快捷回复:

验证码

评论列表 (暂无评论,12人围观)参与讨论

还没有评论,来说两句吧...