zxing-cpp c++版本的编译
一、动态库编译
工程地址:https://github.com/zxing-cpp/zxing-cpp
编译:
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
编译过程中会遇到找不到stb,qt的问题,解决方法为:
1)缺少stb
下载stb:
cd {你用来保存stb包的文件夹}
git clone https://github.com/nothings/stb.git
修改:zxing-cpp/zxing.cmake
macro(zxing_add_package_stb)unset (STB_FOUND CACHE)include_directories("{你用来保存stb包的文件夹}/stb")add_library(stb::stb INTERFACE IMPORTED)target_include_directories(stb::stb INTERFACE "{你用来保存stb包的文件夹}/stb")if (BUILD_DEPENDENCIES STREQUAL "AUTO")find_package(PkgConfig)# pkg_check_modules (STB IMPORTED_TARGET stb)elseif (BUILD_DEPENDENCIES STREQUAL "LOCAL")find_package(PkgConfig REQUIRED)# pkg_check_modules (STB REQUIRED IMPORTED_TARGET stb)endif()# if (NOT STB_FOUND)# include(FetchContent)# FetchContent_Declare (stb# GIT_REPOSITORY https://github.com/nothings/stb.git)# FetchContent_MakeAvailable (stb)# add_library(stb::stb INTERFACE IMPORTED)# target_include_directories(stb::stb INTERFACE ${stb_SOURCE_DIR})# else()# add_library(stb::stb ALIAS PkgConfig::STB)# endif()
endmacro()
2)缺少qt
将CMakeLists.txt文件中,87行到102行注释掉。这样不会影响到动态库的使用
3)编译
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
参考链接:
ubuntu编译zxing-cpp踩坑_autogen: no valid qt version found for target-CSDN博客
4)测试用例
#include "ZXing/ReadBarcode.h"
#include <iostream>int main(int argc, char** argv)
{int width, height;unsigned char* data;// load your image data from somewhere. ImageFormat::Lum assumes grey scale image data.auto image = ZXing::ImageView(data, width, height, ZXing::ImageFormat::Lum);auto options = ZXing::ReaderOptions().setFormats(ZXing::BarcodeFormat::Any);auto barcodes = ZXing::ReadBarcodes(image, options);for (const auto& b : barcodes)std::cout << ZXing::ToString(b.format()) << ": " << b.text() << "\n";return 0;
}