生活随笔
收集整理的這篇文章主要介紹了
基于 libdmtx和zxing的DM二维码识别总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基于 libdmtx和zxing的DM二維碼識別總結
- 1.基于libdmtx的DM二維碼識別
- 2. 基于zxing的DM二維碼識別
1.基于libdmtx的DM二維碼識別
1.1 python實現
python識別DM二維碼比較簡單,只需要pylibdmtx 庫即可,pylibdmtx 庫包含了libdmtx的功能,python代碼如下。
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++實現代碼如下:
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
(®
);}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
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
());
}
總結
以上是生活随笔為你收集整理的基于 libdmtx和zxing的DM二维码识别总结的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。