zxing解析二维码demo

 

源文件.cpp

//#include "funset.hpp"
#include <string>
#include <fstream>
//#include <Windows.h>

#include <zxing/LuminanceSource.h>
#include <zxing/common/Counted.h>
#include <zxing/Reader.h>
#include <zxing/aztec/AztecReader.h>
#include <zxing/common/GlobalHistogramBinarizer.h>
#include <zxing/DecodeHints.h>
#include <zxing/datamatrix/DataMatrixReader.h>
#include <zxing/MultiFormatReader.h>
#include <zxing/pdf417/PDF417Reader.h>
#include <zxing/qrcode/QRCodeReader.h>

#include <opencv2/opencv.hpp>

#include "zxing/MatSource.h"
using namespace std;
using namespace cv;

string test_QRCode_decode(string image_name)
{

    string content = "";
    cv::Mat matSrc = cv::imread(image_name, 1);
    if (!matSrc.data) {
        fprintf(stderr, "read image error: %s", image_name.c_str());
        return content;
    }

    cv::Mat matGray;
    cv::cvtColor(matSrc, matGray, COLOR_BGR2GRAY);

    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::qrcode::QRCodeReader);

    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::QR_CODE_HINT)));


    //获取解析结果
    content = result->getText()->getText();

    return content;
}

int main(){

    string path = "/home/leoxae/图片/qrcode1.png";
    string result = test_QRCode_decode(path);
    cout << "result==>" << result << endl;
}

 

cmakelist

cmake_minimum_required(VERSION 3.14)
project(ZxingQrcode)

set(CMAKE_CXX_STANDARD 14)

add_executable(ZxingQrcode main.cpp)



#找到opencv的包
set(TARGET_LIBS "/usr/local/include/opencv4/opencv2/")
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

#zbar
set(zbar_include_dir "/usr/local/include")
set(zbar_link_dir "/usr/local/lib")

include_directories(${zbar_include_dir})
link_directories(${zbar_link_dir})
link_libraries(zbar)

#zxing
set(zxing_include_dir "/usr/local/include")
set(zxing_link_dir "/usr/local/lib")

include_directories(${zxing_include_dir})
link_directories(${zxing_link_dir})
link_libraries(zxing)


#jsoncpp
set(jsoncpp_include_dir "/home/leoxae/apps/jsoncpp/1.8.4/include")
set(jsoncpp_link_dir "/home/leoxae/apps/jsoncpp/1.8.4/lib")

include_directories(${jsoncpp_include_dir})
link_directories(${jsoncpp_link_dir})
link_libraries(jsoncpp)

#动态链接
#target_link_libraries(pedestrian_detection ${OpenCV_LIBS} )
target_link_libraries(ZxingQrcode ${OpenCV_LIBS} )
target_link_libraries(ZxingQrcode jsoncpp)
target_link_libraries(ZxingQrcode zbar)
target_link_libraries(ZxingQrcode zxing)

 

上一篇:日志架构


下一篇:在WSL上使用systemctl命令启动服务