zxingデコードバーコードC++

2348 ワード

#include "funset.hpp"

#include 

#include 

#include 

 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

 

#include 

 

#include "zxing/MatSource.h"

 

int test_Code128_decode()

{

	std::string image_name = "E:/GitCode/BarCode_Test/test_images/Code128.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, CV_BGR2GRAY);

 

	zxing::Ref<:luminancesource> source = MatSource::create(matGray);

	int width = source->getWidth();

	int height = source->getHeight();

	fprintf(stderr, "image width: %d, height: %d
", width, height); zxing::Ref<:reader> reader; reader.reset(new zxing::oned::Code128Reader); zxing::Ref<:binarizer> binarizer(new zxing::GlobalHistogramBinarizer(source)); zxing::Ref<:binarybitmap> bitmap(new zxing::BinaryBitmap(binarizer)); zxing::Ref<:result> result(reader->decode(bitmap, zxing::DecodeHints(zxing::DecodeHints::CODE_128_HINT))); std::string txt = "E:/GitCode/BarCode_Test/test_images/Code128.txt"; std::ifstream in(txt); if (!in.is_open()) { fprintf(stderr, "fail to open file: %s
", txt.c_str()); return -1; } std::string str1; std::getline(in, str1); fprintf(stderr, "actual result: %s
", str1.c_str()); std::string str2 = result->getText()->getText(); fprintf(stdout, "recognization result: %s
", str2.c_str()); if (str1.compare(str2) == 0) { fprintf(stderr, "===== recognition is correct =====
"); } else { fprintf(stderr, "===== recognition is wrong =====
"); return -1; } in.close(); return 0; }