IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 基于 libdmtx和zxing的DM二维码识别总结 -> 正文阅读

[C++知识库]基于 libdmtx和zxing的DM二维码识别总结

1.基于libdmtx的DM二维码识别

1.1 python实现

python识别DM二维码比较简单,只需要pylibdmtx 库即可,pylibdmtx 库包含了libdmtx的功能,python代码如下。

# -*-coding:utf-8 -*-
import time
import cv2
from pylibdmtx import pylibdmtx
# 加载图片
image = cv2.imread('1.bmp')
t0 = time.time()
# 解析二维码
all_barcode_info = pylibdmtx.decode(image, timeout=500, max_count=1)
print(all_barcode_info)
print(time.time() - t0)
print(all_barcode_info[0].data.decode("utf-8"))

1.2 C++实现

用c++实现DM二维码识别相对复杂,需要用到libdmtx.lib 和dmtx.h头文件。编译libdmtx.lib比较复杂,所以我很贴心的附上X86和X64环境下的libdmtx.lib、libdmtx.dll链接库,以及dmtx.h。百度网盘链接为:https://pan.baidu.com/s/1e0DK7PiAFAIzLwempsNacg 提取码:ckux

C++实现代码如下:

#include <opencv2/opencv.hpp>
#include <iostream>
#include "dmtx.h"

using namespace cv;
using namespace std;

int main()
{
	DmtxMessage* msg;
	DmtxRegion* reg;
	Mat dst;
	double time = getTickCount();
	Mat src = imread("1.jpg");
	if (!src.data)
	{
		cout << "Load image failed!" << endl;
	}
	cvtColor(src, src, COLOR_BGR2GRAY);
	DmtxImage* image;
	image = dmtxImageCreate(src.data, src.cols, src.rows, DmtxPack8bppK);//注意图片类型
	DmtxDecode* dec = dmtxDecodeCreate(image, 1);//解码
	reg = dmtxRegionFindNext(dec, NULL);        //获取二维码位置,第二个参数表示扫描时间上限,达到时间上限退出扫描
	if (reg != NULL) {
		msg = dmtxDecodeMatrixRegion(dec, reg, 1);//解码信息
		if (msg != NULL)
		{
			cout << msg->output << endl;
			dmtxMessageDestroy(&msg);
		}
		dmtxRegionDestroy(&reg);
	}
	else
	{
		cout << "Get region failed!" << endl;
	}
	dmtxDecodeDestroy(&dec);
	dmtxImageDestroy(&image);
	time = (getTickCount() - time) / getTickFrequency();
	cout << "the processing time is :" << time << endl;
	cin.get();
	return 0;
}

2. 基于zxing的DM二维码识别

2.1 C++实现

zxing是一个比较知名的二维码识别开源库。直接抄作业吧,VS工程以及所有和zxing相关的依赖项均在此链接:
链接:https://pan.baidu.com/s/1_aQ-EqaQ4TZdEefO0AF9Qg
提取码:o72j


#include "ReadBarcode.h"
#include "TextUtfEncoding.h"

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include<ctime>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

using namespace ZXing;

static void PrintUsage(const char* exePath)
{
	std::cout << "Usage: " << exePath << " [-fast] [-norotate] [-format <FORMAT[,...]>] [-ispure] <png image path>\n"
		<< "    -fast      Skip some lines/pixels during detection (faster)\n"
		<< "    -norotate  Don't try rotated image during detection (faster)\n"
		<< "    -format    Only detect given format(s) (faster)\n"
		<< "    -ispure    Assume the image contains only a 'pure'/perfect code (faster)\n"
		<< "\n"
		<< "Supported formats are:\n";
	for (auto f : BarcodeFormats::all()) {
		std::cout << "    " << ToString(f) << "\n";
	}
	std::cout << "Formats can be lowercase, with or without '-', separated by ',' and/or '|'\n";
}

static bool ParseOptions(int argc, char* argv[], DecodeHints* hints, std::string* filePath)
{
	for (int i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "-fast") == 0) {
			hints->setTryHarder(false);
		}
		else if (strcmp(argv[i], "-norotate") == 0) {
			hints->setTryRotate(false);
		}
		else if (strcmp(argv[i], "-ispure") == 0) {
			hints->setIsPure(true);
			hints->setBinarizer(Binarizer::FixedThreshold);
		}
		else if (strcmp(argv[i], "-format") == 0) {
			if (++i == argc)
				return false;
			try {
				hints->setFormats(BarcodeFormatsFromString(argv[i]));
			}
			catch (const std::exception& e) {
				std::cerr << e.what() << "\n";
				return false;
			}
		}
		else {
			*filePath = argv[i];
		}
	}

	return !filePath->empty();
}

std::ostream& operator<<(std::ostream& os, const Position& points) {
	for (const auto& p : points)
		os << p.x << "x" << p.y << " ";
	return os;
}

int main(int argc, char* argv[])
{
	DecodeHints hints;
        //自己修改二维码路径
	std::string filePath = "C:/Users/admin/Desktop/DM_Rec/imgs/7.jpg";

	if (!ParseOptions(argc, argv, &hints, &filePath)) {
		PrintUsage(argv[0]);
		return -1;
	}

	int width, height, channels;

	clock_t startTime, endTime;
	
	std::unique_ptr<stbi_uc, void(*)(void*)> buffer(stbi_load(filePath.c_str(), &width, &height, &channels, 4), stbi_image_free);
	if (buffer == nullptr) {
		std::cerr << "Failed to read image: " << filePath << "\n";
		return -1;
	}
	startTime = clock();//计时开始
	auto result = ReadBarcode({ buffer.get(), width, height, ImageFormat::RGBX }, hints);
	endTime = clock();//计时结束
	std::cout << "Text:     \"" << TextUtfEncoding::ToUtf8(result.text()) << "\"\n"
		<< "Format:   " << ToString(result.format()) << "\n"
		<< "Position: " << result.position() << "\n"
		<< "Rotation: " << result.orientation() << " deg\n"
		<< "Error:    " << ToString(result.status()) << "\n";
	std::cout << "The run time is: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << std::endl;
	std::map<ResultMetadata::Key, const char*> keys = { {ResultMetadata::ERROR_CORRECTION_LEVEL, "EC Level: "},
													   {ResultMetadata::SUGGESTED_PRICE, "Price:    "},
													   {ResultMetadata::ISSUE_NUMBER, "Issue #:  "},
													   {ResultMetadata::POSSIBLE_COUNTRY, "Country:  "},
													   {ResultMetadata::UPC_EAN_EXTENSION, "Extension:"} };

	for (auto key : keys) {
		auto value = TextUtfEncoding::ToUtf8(result.metadata().getString(key.first));
		if (value.size())
			std::cout << key.second << value << "\n";
	}

	return static_cast<int>(result.status());
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-27 17:12:03  更:2022-05-27 17:13:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/14 0:52:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码