下载zxing,zxing-cpp-master
https://github.com/zxing-cpp/zxing-cpp
Cmake生成项目,点Generate,把OpenCV_DIR修改了,NameValue没有报红就点Generate。然后点Open Project打开项目。
打开项目后,右击解决方案属性页,勾选后再生成解决方案。
全部生成成功后,新建一个文件夹zxing-cpp,下面有include和lib文件夹,include下再放个zxing文件夹。
复制D:/zxing-cpp-master-build/core/Debug/ZXing.lib到D:\zxing-cpp\lib文件夹下。
复制D:\zxing-cpp-master\core\src下的所有文件到D:\zxing-cpp\include\zxing文件夹下。
新建一个项目,配置属性页
C++ ->常规-> 附加包含目录 D:\zxing-cpp\include
链接器 ->常规 -> 附加库目录 D:\zxing-cpp\lib
链接器 ->输入-> 附加依赖项 ZXing.lib
复制D:\zxing-cpp-master-build\core\Debug\ZXing.dll到 自己项目的Debug目录下( …\TestZxing\x64\Debug)。
已有opencv、zxing的环境下:
#include
#include
using namespace ZXing;
using namespace cv;
struct decodeResStruct {
Point pt1, pt2, pt3, pt4;
String strQRDecode;
decodeResStruct() {
}
decodeResStruct(Point pt1_, Point pt2_, Point pt3_, Point pt4_, String strQRDecode_) {
pt1 = pt1_;
pt2 = pt2_;
pt3 = pt3_;
pt4 = pt4_;
strQRDecode = strQRDecode_;
}
};
std::vector<decodeResStruct> resDecoded;//结果集
int main() {
Mat src = imread("D:/test.bmp");
auto fmt = ImageFormat::None;
switch (src.channels()) {
case 1: fmt = ImageFormat::Lum; break;
case 3: fmt = ImageFormat::BGR; break;
case 4: fmt = ImageFormat::BGRX; break;
}
ZXing::DecodeHints hints;
ZXing::BarcodeFormats format = BarcodeFormat::QRCode;
hints.setFormats(format);
ImageView image{ src.data, src.cols, src.rows, fmt};
ZXing::Results z_results = ReadBarcodes(image, hints);
for (auto&& result : z_results) {
const ZXing::Position pos = result.position();
Point pt1 = { (int)pos[0].x,(int)pos[0].y };
Point pt2 = { (int)pos[1].x,(int)pos[1].y };
Point pt3 = { (int)pos[2].x,(int)pos[2].y };
Point pt4 = { (int)pos[3].x,(int)pos[3].y };
decodeResStruct stru = decodeResStruct{ pt1, pt2, pt3, pt4, result.text()};
resDecoded.push_back(stru);
}
for (auto one : resDecoded) {
line(src, one.pt1, one.pt2, Scalar(0, 255, 0), 2);
line(src, one.pt2, one.pt3, Scalar(0, 255, 0), 2);
line(src, one.pt3, one.pt4, Scalar(0, 255, 0), 2);
line(src, one.pt4, one.pt1, Scalar(0, 255, 0), 2);
putText(src, one.strQRDecode, one.pt1, 0, 0.5, Scalar(255, 0, 0), 2);
}
imshow("res", src);
waitKey();
}