avatar

目录
VS2015+OpenCV3.4.0+dmtx最新版联合集成开发

DPM(Direct Part Mark)是一种特殊的标识制作技术,而并不是一种条码标准,一般称之为“直接零部件标识”。该技术可以实现直接在零部件表面上做标识,而不需要纸张、标签一类的标识载体。采用DPM码进行标识的零部件,许多时候允许制作DPM码的表面面积比较小,所以DPM码需要选择编码容量大的二维码DataMatrix二维条码具有编码容量大、密度高、信息安全性高等特点,与其他二维条码相比,在相同尺寸与密度的情况下,可包含最多的数据信息。因此DataMatrix二维条码成为DPM码最常用的条码种类。

0、环境需求

需要搭载的环境

  • VS2015
  • opencv3.4.0
  • git
  • CMake

1、构建识别dm码的需求库

  • 用git获取dmtx最新源码
Code
1
git clone https://github.com/dmtx/libdmtx.git
  • 打开CMake构建工具,将clone的路径填写到工具中,并在dmtx根目录下面新建一个build文件夹:

image-20200510114708154

  • 之后弹出红色框,选择复选框,再次点击 【configure】

image-20200510114759370

  • 下图就表示完成

image-20200510114835514

  • 之后按顺序点击,直到打开VS2015

image-20200510114918397

  • 之后点击生成——>批生成,按如下配置

image-20200510115110320

  • 此时等待编译完成。完成之后就在release 文件夹里面看得到我们需要的 dmtx的二进制文件和动态链接库。

image-20200510133732399

  • 到这里我们就做好了动态库的基本准备,下面开始实现

2、实现DM码的识别

梳理一下,现在我们需要这几个文件

  • dmtx.dll
  • dmtx.lib
  • dmtx.h (在刚刚git下来的根目录下面)

现在将以上三个文件放在项目的执行目录下面,并且将dmtx,h引入到项目头文件中。在项目属性——>连接器——>附加依赖项里面加入dmtx.lib库。

在cpp函数中,放入以下代码:

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// opencvtest.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include "iostream"
#include "dmtx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;

int main()
{
DmtxMessage *msg;
DmtxRegion *reg;
Mat src = imread("C:/Users/Administrator/Desktop/test.bmp");
if (!src.data) {
cout << "Load image failed!" << endl;
return 0;
}
DmtxImage *img;
img = dmtxImageCreate(src.data, src.cols, src.rows, DmtxPack24bppRGB);
DmtxDecode *dec = dmtxDecodeCreate(img, 1);
reg = dmtxRegionFindNext(dec, NULL);
if (reg != NULL) {
msg = dmtxDecodeMatrixRegion(dec, reg, DmtxUndefined);
if (msg != NULL) {
cout << msg->output << endl;
dmtxMessageDestroy(&msg);
}
dmtxRegionDestroy(®);
}
dmtxDecodeDestroy(&dec);
dmtxImageDestroy(&img);
//waitKey(0);
system("pause");
return 0;
}

结果如下:

image-20200510134530130

3、专业级的生成DM码和解码

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 生成二维码
int test_data_matrix_encode()
{
std::string str = "10,10,10";

DmtxEncode* enc = dmtxEncodeCreate();
assert(enc != NULL);
int ret = dmtxEncodeDataMatrix(enc, strlen(str.c_str()), (unsigned char*)str.c_str());
assert(ret == 1);

int width = dmtxImageGetProp(enc->image, DmtxPropWidth);
int height = dmtxImageGetProp(enc->image, DmtxPropHeight);
int bytesPerPixel = dmtxImageGetProp(enc->image, DmtxPropBytesPerPixel);
fprintf(stderr, "image width: %d, image height: %d, channels: %d\n", width, height, bytesPerPixel);
assert(bytesPerPixel == 1 || bytesPerPixel == 3 || bytesPerPixel == 4);

cv::Mat mat;
if (bytesPerPixel == 1)
mat = cv::Mat(height, width, CV_8UC1);
else if (bytesPerPixel == 3)
mat = cv::Mat(height, width, CV_8UC3);
else
mat = cv::Mat(height, width, CV_8UC4);

mat.data = enc->image->pxl;

std::string image_name = "E:/data_matrix_encode.jpg";
cv::imwrite(image_name, mat);

dmtxEncodeDestroy(&enc);

return 0;
}


//解码
int test_data_matrix_decode()
{
std::string image_name = "E:/GitCode/BarCode_Test/test_images/data_matrix_encode.jpg";
cv::Mat mat = cv::imread(image_name, 1);
if (!mat.data) {
fprintf(stderr, "read image error\n");
return -1;
}

int width = mat.cols;
int height = mat.rows;
int channels = mat.channels();

DmtxImage* img = dmtxImageCreate(mat.data, width, height, DmtxPack24bppRGB);
if (!img) {
fprintf(stderr, "dmtx image create fail\n");
return -1;
}

DmtxDecode* dec = dmtxDecodeCreate(img, 1);
if (!dec) {
fprintf(stderr, "dmtx decode create fail\n");
return -1;
}

DmtxRegion* reg = dmtxRegionFindNext(dec, nullptr);
if (!reg) {
fprintf(stderr, "dmtx region fail\n");
return -1;
}

DmtxMessage* msg = dmtxDecodeMatrixRegion(dec, reg, DmtxUndefined);
if (!msg) {
fprintf(stderr, "dmtx decode matrix region fail\n");
return -1;
}

std::string str(reinterpret_cast<char*>(msg->output));
fprintf(stderr, "decode result: %s\n", str.c_str());

dmtxMessageDestroy(&msg);
dmtxRegionDestroy(®);
dmtxDecodeDestroy(&dec);
dmtxImageDestroy(&img);

return 0;
}

野心与责任一样重!

文章作者: Abraverman
文章链接: http://abraverman.gitee.io/2020/05/10/dmtx/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Abraverman
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论