C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)

一、环境工具准备

  • VS2019
  • Cmake
  • zxing-cpp 源代码 https://gitee.com/asalmc/zxing-cpp
  • opencv4.2.0

二、zxing-cpp 编译

1. 源码目录下新建build文件夹存放构建目录

C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)_第1张图片

2. 使用cmake编译源码

C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)_第2张图片

  1. 源码路径
  2. 构建目录
  3. 点击configure 选择 vs2019-》X64
  4. 点击Generate 在build目录下生成VS工程

C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)_第3张图片

  1. 用VS2019打开编译项目 生成lib库文件

build\core\Debug\ZXingCore.libbuild\core\Realse\ZXingCore.lib

三、创建测试项目

1. VS2019创建控制台项目 TestZXing

2. 配置项目环境

  1. 准备zxing-cpp 头文件和lib库
  • 新建includelib
  • zxing-cpp项目中找出头文件:BarcodeFormat.h BitMatrix.h ByteMatrix.h CharacterSet.h MultiFormatWriter.h stb_image.h stb_image_write.h TextUtfEncoding.h ZXConfig.h ZXStrConvWorkaround.h
  • zxing-cpp中拷贝出生成的lib库:build\core\Debug\ZXingCore.libbuild\core\Realse\ZXingCore.lib

C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)_第4张图片

  1. 准备opencv4.2.0 头文件、dll、lib

opencv4.2.0 开发环境这里不做记录,有兴趣网上查找
C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)_第5张图片

  1. 项目配置
  • 头文件包含目录:右键-》属性-》C/C+±》常规-》附加包含目录

C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)_第6张图片

  • lib库目录:右键-》属性-》链接器-》常规-》附加库目录

C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)_第7张图片

  • lib库:右键-》属性-》链接器-》输入-》附加依赖项

C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)_第8张图片

3. 编写测试代码

#include "BarcodeFormat.h"
#include "MultiFormatWriter.h"
#include "BitMatrix.h"
#include "ByteMatrix.h"
#include "TextUtfEncoding.h"
#include "ZXStrConvWorkaround.h"

#include 
#include 
#include 
#include 
#include 
#include 

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
using namespace ZXing;

#include  
using namespace cv;


static inline std::string GetExtension(const std::string& path)
{
	auto fileNameStart = path.find_last_of("/\\");
	auto fileName = fileNameStart == std::string::npos ? path : path.substr(fileNameStart + 1);
	auto extStart = fileName.find_last_of('.');
	auto ext = extStart == std::string::npos ? "" : fileName.substr(extStart + 1);
	std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c) { return std::tolower(c); });
	return ext;
}

static inline void AddLogoToQRCodeImage(const std::string pngPath, const std::string logoPath)
{
	Mat srcQRCode = imread(pngPath);
	Mat srcLogo = imread(logoPath);
	Mat imageRoi = srcQRCode(Rect(srcQRCode.cols / 2 - srcLogo.cols / 2, srcQRCode.rows / 2 - srcLogo.rows / 2, srcLogo.cols, srcLogo.rows));
	Mat mask = imread(logoPath, 0);
	srcLogo.copyTo(imageRoi, mask);

	imwrite(pngPath, srcQRCode);
}

int main(int argc, char* argv[])
{
	int width = 500, height = 500;
	int margin = 10;    //空白
	int eccLevel = 8;	//纠错级别
	std::string format = "QR_CODE";
	std::string text = " {\"data\" : \"www.baidu.com\",\"type\" : \"1\"} ";
	std::string filePath = "./QrCode.png";
	std::string logoPath = "./logo.png";
	try {
		auto barcodeFormat = BarcodeFormatFromString(format);
		if (barcodeFormat == BarcodeFormat::FORMAT_COUNT)
			throw std::invalid_argument("Unsupported format: " + format);

		MultiFormatWriter writer(barcodeFormat);
		if (margin >= 0)
			writer.setMargin(margin);
		if (eccLevel >= 0)
			writer.setEccLevel(eccLevel);

		auto bitmap = writer.encode(TextUtfEncoding::FromUtf8(text), width, height).toByteMatrix();

		auto ext = GetExtension(filePath);
		int success = 0;
		if (ext == "" || ext == "png") {
			success = stbi_write_png(filePath.c_str(), bitmap.width(), bitmap.height(), 1, bitmap.data(), 0);
		}
		else if (ext == "jpg" || ext == "jpeg") {
			success = stbi_write_jpg(filePath.c_str(), bitmap.width(), bitmap.height(), 1, bitmap.data(), 0);
		}

		if (!success) {
			std::cerr << "Failed to write image: " << filePath << std::endl;
			return -1;
		}
	}
	catch (const std::exception& e) {
		std::cerr << e.what() << std::endl;
		return -1;
	}

	AddLogoToQRCodeImage(filePath, logoPath);
	return 0;
}

四、最后展示效果

C++桌面端使用 zxing-cpp 和 opencv 生成二维码(带中间logo)_第9张图片

五、完整源码

https://download.csdn.net/download/u011780419/87326616?spm=1001.2014.3001.5503

你可能感兴趣的:(二维码,c++,opencv,visual,studio)