zxing-cpp+OpenCV根据字符串生成条形码

编译构建

需要使用到 CMake、Git、GCC 或 MSVC。

github 链接:https://github.com/zxing-cpp/zxing-cpp

编译之前请确保:

  1. 确保安装了 CMake 版本 3.15 或更高版本。

  2. 确保安装了与 C++17 兼容的编译器(最低VS 2019 16.8 / gcc 7 / clang 5)。

编译构建很简单,如同官网:

txt 复制代码
git clone https://github.com/zxing-cpp/zxing-cpp.git --recursive --single-branch --depth 1
cmake -S zxing-cpp -B zxing-cpp.release -DCMAKE_BUILD_TYPE=Release
cmake --build zxing-cpp.release -j8 --config Release

如果出错那就只能自行解决,不过不用担心,我们已经提供了编译完成的静态库。

此库不支持动态库,我们提供了 debug 和 release 两种 .lib

之后就是正常的引入静态库的环境配置了,不再介绍。

OpenCV

ZXing 理论上不依赖 OpenCV 等其它库,不过涉及图形操作,我们的第一选择基本也就是 OpenCV 了。实测 OpenCV3 与 OpenCV4 都可使用。

OpenCV + Zxing 根据字符串生成 PDF417 条形码

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <BarcodeFormat.h>
#include <BitMatrix.h>
#include <TextUtfEncoding.h>
#include <MultiFormatWriter.h>

using namespace cv;
using namespace ZXing;

int main() {
    std::string input = "Hello, ZXing! This is a PDF417 barcode.";

    MultiFormatWriter writer(BarcodeFormat::PDF417);
    auto bitMatrix = writer.encode(input, 200, 100);

    int width = bitMatrix.width();
    int height = bitMatrix.height();
    Mat barcodeImage(height, width, CV_8UC1);

    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            barcodeImage.at<uint8_t>(y, x) = bitMatrix.get(x, y) ? 0 : 255;
        }
    }

    namedWindow("PDF417 Barcode", WINDOW_NORMAL);
    imshow("PDF417 Barcode", barcodeImage);
    waitKey(0);

    imwrite("pdf417_barcode.png", barcodeImage);
}

实测扫码枪可以识别。如果当前环境编码存在问题,可以考虑进行编码转换,转换到 UTF-8。

我们稍微详细的来解释一下上面的代码。

  1. 包含头文件
  • 包含 OpenCV 和 ZXing 库的头文件。以及引入对应的命名空间
cpp 复制代码
#include <opencv2/opencv.hpp>
#include <BarcodeFormat.h>
#include <BitMatrix.h>
#include <TextUtfEncoding.h>
#include <MultiFormatWriter.h>

using namespace cv;
using namespace ZXing;
  1. 输入字符串并生成 PDF417 条形码

    • 定义一个要编码为 PDF417 条形码的字符串:

      cpp 复制代码
      std::string input = "Hello, ZXing! This is a PDF417 barcode.";
    • 生成 PDF417 条形码:

      创建一个 MultiFormatWriter 对象,指定条形码格式为PDF417。

      使用 encode 方法将输入字符串编码为一个位矩阵(bit matrix),并指定图像的宽度和高度(200x100)。

      cpp 复制代码
      MultiFormatWriter writer(BarcodeFormat::PDF417);
      auto bitMatrix = writer.encode(input, 200, 100); // 宽度和高度可以根据需要调整
  2. 将条形码数据转换为 OpenCV Mat 对象:

    • 获取位矩阵的宽度和高度。

    • 创建一个 OpenCV 的灰度图像矩阵。

    • 遍历位矩阵的每个像素点,如果为真则设为黑色(0),否则设为白色(255)

    cpp 复制代码
    int width = bitMatrix.width();
    int height = bitMatrix.height();
    Mat barcodeImage(height, width, CV_8UC1);
    
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            barcodeImage.at<uint8_t>(y, x) = bitMatrix.get(x, y) ? 0 : 255;
        }
    }
  3. 显示条形码图像

    创建窗口并显示,等待用户按下任意键关闭窗口:

    cpp 复制代码
    namedWindow("PDF417 Barcode", WINDOW_NORMAL);
    imshow("PDF417 Barcode", barcodeImage);
    waitKey(0);
  4. 保存条形码为本地图像

    cpp 复制代码
    imwrite("pdf417_barcode.png", barcodeImage);

总结

最大的问题不在于代码的理解,而是在于环境的构建。

代码本身是简单的,根据这个小例子足以修改、增加,完成许多的需求。

相关推荐
雾江流3 分钟前
肉包 1.4.0 | 豆包AI手机平替,开源免费,AI自动化
运维·人工智能·自动化·软件工程
光锥智能3 分钟前
昆仑芯冲刺IPO,百度押中了一枚国产AI芯片
人工智能·百度
再睡一夏就好4 分钟前
深入解析Linux页表:从虚拟地址到物理内存的映射艺术
linux·运维·服务器·c语言·c++·页表·缺页异常
沫儿笙5 分钟前
发那科弧焊机器人保护气节气设备
人工智能·机器人
有Li15 分钟前
AGFS-Tractometry:一种新型图谱引导的精细尺度束测量方法,用于增强扩散MRI束描记术的沿束组统计比较|文献速递-医疗影像分割与目标检测最新技术
人工智能
roman_日积跬步-终至千里19 分钟前
【计算机视觉(19)】语义理解-CNN应用_目标检测_语义分割
目标检测·计算机视觉·cnn
中科米堆20 分钟前
中科米堆CASAIM自动化三维检测-0.02mm计量级精度产品尺寸快速检测
人工智能·3d·3d全尺寸检测
张拭心28 分钟前
为什么说 AI 视频模型不能用来做教育?Sora-2 Veo-3 来了也不行
前端·人工智能
百***074529 分钟前
【保姆级教程】GPT-5.2极速接入指南:3步上手专家级多模态AI能力
人工智能·gpt
Starry_hello world31 分钟前
C++ 线程 (3)
c++