//zxing
#include "zxing/LuminanceSource.h"
#include "zxing/Reader.h"
#include "zxing/common/GlobalHistogramBinarizer.h"
#include "zxing/DecodeHints.h"
#include "zxing/datamatrix/DataMatrixReader.h"
#include "zxing/MatSource.h"
#include "zxing/result.h"
#include "zxing/BinaryBitmap.h"
#include "zxing/Binarizer.h"
#include "zxing/Exception.h"
//opencv
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
/******************************************************************************************
Function: DataMatrixDecoder
Description: data matrix二维码解析
Input: image_path:二维码图片路径
Return: 解析结果*******************************************************************************************/
std::string DataMatrixDecoder(std::string image_path)
{
//初始化解析结果
std::string content = "";
//读取图片
cv::Mat matSrc = cv::imread(image_path, 1);
//读取失败则返回
if (!matSrc.data)
{
fprintf(stderr, "read image error: %s", image_path.c_str());
return content;
}
try
{
//转化为灰度图
cv::Mat matGray;cv::cvtColor(matSrc, matGray, CV_BGR2GRAY);
zxing::Refsource = MatSource::create(matGray);
/*int width = source->getWidth();
int height = source->getHeight();
*/
/*fprintf(stderr, "image width: %d, height: %d\n", width, height);
*/
zxing::Refreader;reader.reset(new zxing::datamatrix::DataMatrixReader);
zxing::Refbinarizer(new zxing::GlobalHistogramBinarizer(source));
zxing::Refbitmap(new zxing::BinaryBitmap(binarizer));
//开始解码
zxing::Ref result(reader->decode(bitmap, zxing::DecodeHints(zxing::DecodeHints::DATA_MATRIX_HINT)));
//获取解析结果
content = result->getText()->getText();
}
catch (zxing::Exception e)
{
}
return content;
}
网友评论