opencv call nu-book / zxing-cpp two-dimensional code recognition

Installation Environment:

1. zxing-cpp after the formation of the source code and compile source code lib, my version is V1.0.8, compilation methods, see: https://www.cnblogs.com/zhenjin-huaxiazongdai/p/12545959.html . zxing-cpp project path: https://github.com/nu-book/zxing-cpp

2. Visual Studio 2017 installation https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/ , after installation tool - Get the tools and support to add "Using C ++ desktop development" support.

3. Install windows opencv and adding environment variables, for example, I installed in D: \ opencv \ opencv folder, add environment variables D: \ opencv \ opencv \ build \ x64 \ vc15 \ bin. Observation opencv folder we can know, opencv default only supports x64, x86 need to download the source code to compile their own. I installed version 4.1.2, download path:

image

Demo Institute Add : Https://Github.Com/SourceCode-Farmer/ZxingDemo

 

The process of recording:

First, add dependencies

1. Create a new Visual C ++ Windows console application ZxingDemo, open the project folder with the following documents:

image

2. Open opencv install the build file in the directory folder, copy the folder to include ZxingDemo project folder, rename opencv_header.

image

3. ZxingDemo project folder New opencv_lib \ lib64 folder, open opencv install the build directory \ x64 \ vc15 \ lib folder, copy the two files to opencv_world412.lib and opencv_world412d.lib ZxingDemo project folder ( If the lib file name changes, move all lib files copied to fall).

image

4. In ZxingDemo new project file folder zxing_lib, copy lib after zxing-cpp compiled into a folder (note Zxing-cpp and ZxingDemo solutions compile time configuration, the solution must be the same platform).

image

5. Open the core zxing-cpp source file in the folder, copy src ZxingDemo to the project directory, rename the folder to zxing_header.

image

Zxing_header 6. Open the folder, delete the current directory and all subdirectories cpp file.

ps: If only Qr code recognition, you can delete the directory aztec, maxicode, oned, pdf417 and textcodec, reserved under zxing_header h file and datamatrix, under qrcode file h file.

ps: Group By modifying the file, you can delete the cpp files fast

image

 

Second, the configuration dependent

1. vs project right property, adjust the configuration to Release, adjust the platform for x64.

image

2. Properties - Configuration Properties - c / c ++ - General - Additional Include Directories, add the header file path (path picture the actual make adjustments, Yihuhuhuapiao).

image

3. Properties - Configuration Properties - Linker - General - Additional Library Directories, add lib library directory (the actual image path to make adjustments, Yihuhuhuapiao).

image

4. Properties - Configuration Properties - Linker - General - Additional Dependencies, add all lib file names (actual file name adjusted lib) ZxingDemo project.

image

 

Third, coding

1. Write code ZxingDemo.cpp file.

//按项目实际路径调整
#pragma comment(lib,"zxing_lib/ZXingCore.lib")

#include "stdafx.h"

#include "zxing_header\HybridBinarizer.h"
#include "zxing_header\LuminanceSource.h"
#include "zxing_header\GenericLuminanceSource.h"
#include "zxing_header\DecodeHints.h"
#include "zxing_header\BinaryBitmap.h"
#include "zxing_header\ReadBarcode.h"
#include "zxing_header\TextUtfEncoding.h"
#include "zxing_header\MultiFormatReader.h"

#include <opencv2\opencv.hpp>
#include <opencv2\core\types_c.h>


static std::string WstringToString(const std::wstring &wstr) {
    std::string str;
    std::mbstate_t state = {};
    const wchar_t *data = wstr.data();
    size_t len = std::wcsrtombs(nullptr, &data, 0, &state);
    if (static_cast<size_t>(-1) != len) {
        std::unique_ptr<char[]> buff(new char[len + 1]);
        len = std::wcsrtombs(buff.get(), &data, len, &state);
        if (static_cast<size_t>(-1) != len) {
            str.assign(buff.get(), len);
        }
    }
    return str;
}

static void Scan() {
    cv::Mat mat, gray_mat;
    //打开图片
    mat = cv::imread("test.PNG");
    if (mat.empty()) {
        std::cout << "not found image" << std::endl;
        return;
    }
    //转为灰度图
    cv::cvtColor(mat, gray_mat, cv::COLOR_RGBA2GRAY);
    //宽高
    int height = gray_mat.rows;
    int width = gray_mat.cols;
    auto *pixels = new unsigned char[height * width];
    int index = 0;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            pixels[index++] = gray_mat.at<unsigned char>(i, j);
        }
    }
    //识别
    std::shared_ptr<ZXing::GenericLuminanceSource> luminance = std::make_shared<ZXing::GenericLuminanceSource>(0, 0, width, height, pixels, width * sizeof(unsigned char));
    std::shared_ptr<ZXing::BinaryBitmap> bitmap = std::make_shared<ZXing::HybridBinarizer>(luminance);
    ZXing::DecodeHints hints;
    //根据需要添加format
    std::vector<ZXing::BarcodeFormat> formats = { ZXing::BarcodeFormat(ZXing::BarcodeFormat::QR_CODE) };
    hints.setPossibleFormats(formats);
    auto reader = new ZXing::MultiFormatReader(hints);
    ZXing::Result result = reader->read(*bitmap);
    if (result.status() == ZXing::DecodeStatus::NoError) {
        //识别成功,打印结果
        std::cout << WstringToString(result.text()) << std::endl;
    }
    else {
        std::cout << "Fail" << std::endl;
    }
}

int main()
{
    Scan();
    std::cin.get();
    return 0;
}

 

2. engage in a two-dimensional code picture test.PNG into the compiler output directory x64 \ Release in.

 

Fourth, start

1. adjust the configuration to Release, adjust the platform for x64, click on the "local windows debugger," startup test.

image

2. The successful output.

image

V. Frequently Asked Questions

1. 编译异常:错误    C4996    'wcsrtombs': This function or variable may be unsafe. Consider using wcsrtombs_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    ZxingDemo    H:\cpp\ZxingDemo\ZxingDemo\ZxingDemo.cpp    23   
image

Solution: Right Item Properties - Configuration Properties - c / c ++ - Preprocessor - Preprocessor definitions, add pre-defined as follows.

_CRT_SECURE_NO_WARNINGS
_CRT_NONSTDC_NO_WARNINGS
_CRT_SECURE_NO_WARNINGS_GLOBALS

image

2. After execution of the abnormality to start the test error: Can not start this program, because the computer lost opencv_world412.dll. Try reinstalling the program has to resolve this problem.

image

Cause: The program can not find opencv_world412.dll, general: 1 is not installed or not added opencv opencv environment variable 2. Open vs2017 after adding opencv environment variable is not in effect.

Solution: install opencv, add the Path environment variable build \ x64 \ vc15 \ bin (or build \ x64 \ vc14 \ bin). Close Vs2017 reopen the project.

Guess you like

Origin www.cnblogs.com/zhenjin-huaxiazongdai/p/12571824.html