Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BeforeGodKnows/QRCode2Matrices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QRCode2Matrices

这个工具可以将二维码图片转换为01矩阵,以及将01矩阵转换回可扫描的二维码。

功能特点

  1. 二维码 → 01矩阵

    • 自动识别二维码有效区域(去除白边)
    • 黑色像素转换为1,白色像素转换为0
    • 保存为易读的txt文件
  2. 01矩阵 → 二维码

    • 读取txt文件中的01矩阵
    • 重建为可扫描的二维码图片
    • 支持自定义模块大小和边框
  3. 二维码验证

    • 测试生成的二维码是否可读
    • 显示解码结果

项目结构

.
├── src/
│   └── qrcode_converter.py    # 核心转换模块
├── test/
│   └── test_converter.py      # 命令行测试工具
├── requirements.txt            # Python依赖
└── README.md                   # 项目文档

环境配置

Linux 环境(推荐使用 Conda,不想用 Conda 可以直接从第3条开始看)

在服务器环境中,强烈推荐使用 Conda ,避免与其他人的 Python 环境冲突。如果你是虚拟机环境测试的话,那这个就随意了。

1. 安装 Conda

如果还没有安装 Conda,可以安装 Miniconda:

# 下载 Miniconda 安装脚本
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

# 运行安装脚本
bash Miniconda3-latest-Linux-x86_64.sh

# 重新加载 shell 配置
source ~/.bashrc

2. 创建虚拟环境

# 创建名为 qrcode_env 的 Python 环境
conda create -n qrcode_env 

# 激活环境
conda activate qrcode_env

3. 安装系统依赖

# Ubuntu/Debian 系统
sudo apt-get update
sudo apt-get install -y libzbar0 libzbar-dev

4. 安装 Python 依赖

# 进入项目目录
cd /path/to/QRCode2Matrices

# 安装依赖
pip install -r requirements.txt

5. 验证安装

# 运行测试
python3 src/qrcode_converter.py

Windows 环境(默认已安装了 Python ,且能在命令行中使用。PS:我没测这个,可能会有问题)

1. 安装 zbar 库

下载并安装 zbar for Windows:

  1. 访问 zbar 下载页面
  2. 下载 Windows 版本的安装包
  3. 运行安装程序,记住安装路径

或者直接下载 DLL 文件:

# 使用 PowerShell 下载(以管理员权限运行)
Invoke-WebRequest -Uri "https://github.com/dani4/PyZBar/raw/master/libzbar-64.dll" -OutFile "C:\Windows\System32\libzbar-64.dll"

3. 安装 Python 依赖

打开命令提示符(cmd)或 PowerShell:

# 进入项目目录
cd C:\path\to\QRCode2Matrices

# 安装依赖
pip install -r requirements.txt

4. 验证安装

# 运行测试
python3 src\qrcode_converter.py

使用方法

命令行工具

示例使用

# 进入项目目录
cd /path/to/QRCode2Matrices
# 运行示例脚本
python3 src/qrcode_converter.py

将会在项目目录下生成示例二维码和矩阵文件,以及重建后的文件,仅供参考。如果你想转换自己的二维码就看下面的基本用法。

基本用法

# 二维码转矩阵(自动生成输出路径)
python3 test/test_converter.py -qr2m /path/to/input_qr.png

# 二维码转矩阵(指定输出路径)
python3 test/test_converter.py -qr2m /path/to/input_qr.png /path/to/output_matrix.txt

# 矩阵转二维码(自动生成输出路径)
python3 test/test_converter.py -m2qr /path/to/input_matrix.txt

# 矩阵转二维码(指定输出路径)
python3 test/test_converter.py -m2qr /path/to/input_matrix.txt /path/to/output_qr.png

高级选项

# 自定义二值化阈值
python3 test/test_converter.py -qr2m input_qr.png -t 150

# 自定义模块大小和边框
python3 test/test_converter.py -m2qr input_matrix.txt -s 15 -b 6

# 查看帮助信息
python3 test/test_converter.py -h

参数说明

模式选择(必选其一):

  • -qr2m, --qr-to-matrix: 二维码转矩阵模式

    • 第一个参数:二维码图片路径(必需)
    • 第二个参数:输出txt路径(可选,默认保存在同一文件夹)
  • -m2qr, --matrix-to-qr: 矩阵转二维码模式

    • 第一个参数:输入txt路径(必需)
    • 第二个参数:输出二维码路径(可选,默认保存在同一文件夹)

可选参数(基本不用管):

  • -t, --threshold: 表示决定灰度图转二值图时的阈值,像素值低于阈值视为黑,高于阈值视为白,默认 127
  • -s, --module-size: 表示生成二维码时每个小方块(模块)的像素宽高,默认 10
  • -b, --border: 表示在二维码四周加的白边宽度,用模块数量表示,默认 4

API 使用示例

from src.qrcode_converter import QRCodeConverter

# 创建转换器
converter = QRCodeConverter()

# 1. 二维码转01矩阵
matrix = converter.qrcode_to_matrix('input_qr.png', 'output_matrix.txt')

# 2. 01矩阵转二维码
converter.matrix_to_qrcode('output_matrix.txt', 'reconstructed_qr.png')

# 3. 测试二维码是否可读
converter.test_qrcode_readability('reconstructed_qr.png')

About

A simple tool for converting QR codes to matrices.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages