美文网首页
Ubuntu环境下使用Zxing开源包识别二维码(Data Ma

Ubuntu环境下使用Zxing开源包识别二维码(Data Ma

作者: 闪电侠悟空 | 来源:发表于2019-12-15 14:58 被阅读0次

    1.安装Zxing

    mkdir build
    cd ./build
    
    • cmake一下
    cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-std=c++11"  ..
    

    说明:不采用c++11标准,好像有错.

    • make一下, make install
    make
    sudo make install
    

    说明:make过程中可能有错,仔细看了下是Opencv的版本问题,把报错的位置"CV_"去掉,把"CV_BGR2GRAY"换成"COLOR_BGR2GRAY"后,继续"make".
    所有命令成功执行,说明zxing安装成功;
    2. C++调用zxing

    • CMakeLists.txt文件的编写
    project(threads)
    
    set(CMAKE_CXX_STANDARD 11)
    set(Zxing_INCLUDE_DIRS /usr/local/include/zxing) #指定*.h文件目录位置
    set(Zxing_LIBS /usr/local/lib/libzxing.a)  # 指定*.a文件(居然是静态连接库)
    
    include_directories(${OpenCV_INCLUDE_DIRS})
    include_directories(${Zxing_INCLUDE_DIRS}) # 添加头文件*.h
    find_package(OpenCV REQUIRED)
    
    add_executable(threads test_threads.cpp)
    target_link_libraries(threads ${OpenCV_LIBS} ${Zxing_LIBS} ) # 添加库文件*.a(很重要的)
    
    • cpp文件的#include编写
    #include <zxing/common/Counted.h>
    #include <zxing/Binarizer.h>
    #include <zxing/MultiFormatReader.h>
    #include <zxing/Result.h>
    #include <zxing/ReaderException.h>
    #include <zxing/common/GlobalHistogramBinarizer.h>
    #include <zxing/Exception.h>
    #include <zxing/common/IllegalArgumentException.h>
    #include <zxing/BinaryBitmap.h>
    #include <zxing/DecodeHints.h>
    #include <zxing/qrcode/QRCodeReader.h>
    #include <zxing/MultiFormatReader.h>
    #include <zxing/MatSource.h>
    #include <zxing/datamatrix/DataMatrixReader.h>
    

    好像没有什么问题!说明一切顺利,那么下面将用一张实际图像进行测试!

    3. zxing识别Data Matrix的实例
    参考博客 如何在visual studio下编译zxing cpp,以及zxing c++的使用

    #include <iostream>
    #include <string>
    
    #include <zxing/common/Counted.h>
    #include <zxing/Binarizer.h>
    #include <zxing/MultiFormatReader.h>
    #include <zxing/Result.h>
    #include <zxing/ReaderException.h>
    #include <zxing/common/GlobalHistogramBinarizer.h>
    #include <zxing/Exception.h>
    #include <zxing/common/IllegalArgumentException.h>
    #include <zxing/BinaryBitmap.h>
    #include <zxing/DecodeHints.h>
    #include <zxing/qrcode/QRCodeReader.h>
    #include <zxing/MultiFormatReader.h>
    #include <zxing/MatSource.h>
    #include <zxing/datamatrix/DataMatrixReader.h>
    
    #include <opencv2/opencv.hpp>
    
    using namespace cv;
    using namespace std;
    
    int main() {
        // step 1.读图并转换为灰度图
        std::string image_name = "../test3.png";
        cv::Mat matSrc = cv::imread(image_name, 1);
        if (!matSrc.data) {
            fprintf(stderr, "read image error: %s", image_name.c_str());
            return -1;
        }
    
        cv::Mat matGray;
        cv::cvtColor(matSrc, matGray, COLOR_BGR2GRAY);
        try {
            zxing::Ref<zxing::LuminanceSource> source = MatSource::create(matGray);
            int width = source->getWidth();
            int height = source->getHeight();
            fprintf(stderr, "image width: %d, height: %d\n", width, height);
    
            zxing::Ref<zxing::Reader> reader;
            reader.reset(new zxing::datamatrix::DataMatrixReader);
    
            zxing::Ref<zxing::Binarizer> binarizer(new zxing::GlobalHistogramBinarizer(source));
            zxing::Ref<zxing::BinaryBitmap> bitmap(new zxing::BinaryBitmap(binarizer));
            //开始解码
            zxing::Ref<zxing::Result> result(
                    reader->decode(bitmap, zxing::DecodeHints(zxing::DecodeHints::DATA_MATRIX_HINT)));
    
            std::string str2 = result->getText()->getText();
    
            cout << str2.data();
        }
        catch (zxing::Exception &e) {
            //Export your failure to read the code here
            cerr << "Read Error: " << e.what() << endl;
        }
    }
    

    相关文章

      网友评论

          本文标题:Ubuntu环境下使用Zxing开源包识别二维码(Data Ma

          本文链接:https://www.haomeiwen.com/subject/yhbyrqtx.html