-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a Dynamsoft Barcode Reader v10.x example
- Loading branch information
Showing
39 changed files
with
9,137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
/.vscode | ||
/dist | ||
dist | ||
build | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
cmake_minimum_required (VERSION 3.8) | ||
project (main) | ||
MESSAGE( STATUS "PROJECT_NAME: " ${PROJECT_NAME} ) | ||
find_package(OpenCV REQUIRED) | ||
|
||
if (CMAKE_HOST_WIN32) | ||
set(WINDOWS 1) | ||
elseif(CMAKE_HOST_UNIX) | ||
set(LINUX 1) | ||
endif() | ||
|
||
# Check compiler architecture | ||
if(CMAKE_CL_64) | ||
MESSAGE( STATUS ">>>>>>>> 64-bit") | ||
else() | ||
MESSAGE( STATUS ">>>>>>>> 32-bit") | ||
endif() | ||
|
||
# Check compilers | ||
MESSAGE( STATUS ">>>>>>>> ${CMAKE_CXX_COMPILER_ID}") | ||
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") | ||
MESSAGE( STATUS "Using Clang" ) | ||
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | ||
MESSAGE( STATUS "Using GNU" ) | ||
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel") | ||
MESSAGE( STATUS "Using Intel" ) | ||
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") | ||
MESSAGE( STATUS "Using MSVC" ) | ||
endif() | ||
|
||
# Set RPATH | ||
if(CMAKE_HOST_UNIX) | ||
if(CMAKE_HOST_APPLE) | ||
SET(CMAKE_CXX_FLAGS "-std=c++11 -O3 -Wl,-rpath,@loader_path") | ||
SET(CMAKE_INSTALL_RPATH "@loader_path") | ||
else() | ||
SET(CMAKE_CXX_FLAGS "-std=c++11 -O3 -Wl,-rpath=$ORIGIN") | ||
SET(CMAKE_INSTALL_RPATH "$ORIGIN") | ||
endif() | ||
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) | ||
endif() | ||
|
||
# Add search path for include and lib files | ||
MESSAGE( STATUS "CPU architecture ${CMAKE_SYSTEM_PROCESSOR}" ) | ||
if(WINDOWS) | ||
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | ||
link_directories("${PROJECT_SOURCE_DIR}/../sdk/platforms/win/bin/") | ||
else() | ||
link_directories("${PROJECT_SOURCE_DIR}/../sdk/platforms/win/lib/") | ||
endif() | ||
elseif(LINUX) | ||
if (CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) | ||
MESSAGE( STATUS "Link directory: ${PROJECT_SOURCE_DIR}/../sdk/platforms/linux/" ) | ||
link_directories("${PROJECT_SOURCE_DIR}/../sdk/platforms/linux/") | ||
endif() | ||
endif() | ||
include_directories("${PROJECT_BINARY_DIR}" "${PROJECT_SOURCE_DIR}/../sdk/include/") | ||
|
||
# Add the executable | ||
add_executable(${PROJECT_NAME} main.cxx) | ||
if(WINDOWS) | ||
if(CMAKE_CL_64) | ||
target_link_libraries (${PROJECT_NAME} "DynamsoftCorex64" "DynamsoftLicensex64" "DynamsoftCaptureVisionRouterx64" "DynamsoftUtilityx64" ${OpenCV_LIBS}) | ||
endif() | ||
else() | ||
target_link_libraries (${PROJECT_NAME} "DynamsoftCore" "DynamsoftLicense" "DynamsoftCaptureVisionRouter" "DynamsoftUtility" pthread ${OpenCV_LIBS}) | ||
endif() | ||
|
||
if(WINDOWS) | ||
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E copy_directory | ||
"${PROJECT_SOURCE_DIR}/../sdk/platforms/win/bin/" | ||
$<TARGET_FILE_DIR:main>) | ||
|
||
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E copy | ||
"${PROJECT_SOURCE_DIR}/../sdk/DBR-PresetTemplates.json" | ||
$<TARGET_FILE_DIR:main>/DBR-PresetTemplates.json) | ||
endif() | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
// Refer to https://github.com/Dynamsoft/barcode-reader-c-cpp-samples/blob/main/Samples/VideoDecoding/VideoDecoding.cpp | ||
#include "opencv2/core.hpp" | ||
#include "opencv2/imgproc.hpp" | ||
#include "opencv2/highgui.hpp" | ||
#include "opencv2/videoio.hpp" | ||
#include "opencv2/core/utility.hpp" | ||
#include "opencv2/imgcodecs.hpp" | ||
#include <iostream> | ||
#include <vector> | ||
#include <chrono> | ||
// Include headers of DynamsoftCaptureVisionRouter SDK | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include "DynamsoftCaptureVisionRouter.h" | ||
#include "DynamsoftUtility.h" | ||
using namespace std; | ||
using namespace dynamsoft::license; | ||
using namespace dynamsoft::cvr; | ||
using namespace dynamsoft::dbr; | ||
using namespace dynamsoft::utility; | ||
using namespace cv; | ||
|
||
struct BarcodeResult | ||
{ | ||
std::string type; | ||
std::string value; | ||
std::vector<cv::Point> localizationPoints; | ||
int frameId; | ||
}; | ||
|
||
std::vector<BarcodeResult> barcodeResults; | ||
std::mutex barcodeResultsMutex; | ||
|
||
class MyCapturedResultReceiver : public CCapturedResultReceiver | ||
{ | ||
|
||
virtual void OnDecodedBarcodesReceived(CDecodedBarcodesResult *pResult) override | ||
{ | ||
std::lock_guard<std::mutex> lock(barcodeResultsMutex); | ||
|
||
if (pResult->GetErrorCode() != EC_OK) | ||
{ | ||
cout << "Error: " << pResult->GetErrorString() << endl; | ||
} | ||
else | ||
{ | ||
auto tag = pResult->GetOriginalImageTag(); | ||
if (tag) | ||
cout << "ImageID:" << tag->GetImageId() << endl; | ||
int count = pResult->GetItemsCount(); | ||
cout << "Decoded " << count << " barcodes" << endl; | ||
|
||
barcodeResults.clear(); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
const CBarcodeResultItem *barcodeResultItem = pResult->GetItem(i); | ||
if (barcodeResultItem != NULL) | ||
{ | ||
cout << "Result " << i + 1 << endl; | ||
cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << endl; | ||
cout << "Barcode Text: " << barcodeResultItem->GetText() << endl; | ||
CPoint *points = barcodeResultItem->GetLocation().points; | ||
|
||
BarcodeResult result; | ||
result.type = barcodeResultItem->GetFormatString(); | ||
result.value = barcodeResultItem->GetText(); | ||
result.frameId = tag->GetImageId(); | ||
result.localizationPoints.push_back(cv::Point(points[0][0], points[0][1])); | ||
result.localizationPoints.push_back(cv::Point(points[1][0], points[1][1])); | ||
result.localizationPoints.push_back(cv::Point(points[2][0], points[2][1])); | ||
result.localizationPoints.push_back(cv::Point(points[3][0], points[3][1])); | ||
|
||
barcodeResults.push_back(result); | ||
} | ||
} | ||
} | ||
|
||
cout << endl; | ||
} | ||
}; | ||
|
||
class MyVideoFetcher : public CImageSourceAdapter | ||
{ | ||
public: | ||
MyVideoFetcher(){}; | ||
~MyVideoFetcher(){}; | ||
bool HasNextImageToFetch() const override | ||
{ | ||
return true; | ||
} | ||
void MyAddImageToBuffer(const CImageData *img, bool bClone = true) | ||
{ | ||
AddImageToBuffer(img, bClone); | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
cout << "Opening camera..." << endl; | ||
VideoCapture capture(0); // open the first camera | ||
if (!capture.isOpened()) | ||
{ | ||
cerr << "ERROR: Can't initialize camera capture" << endl; | ||
cout << "Press any key to quit..." << endl; | ||
cin.ignore(); | ||
return 1; | ||
} | ||
int iRet = -1; | ||
char szErrorMsg[256]; | ||
// Initialize license. | ||
// Request a trial from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr | ||
iRet = CLicenseManager::InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==", szErrorMsg, 256); | ||
if (iRet != EC_OK) | ||
{ | ||
cout << szErrorMsg << endl; | ||
} | ||
int errorCode = 1; | ||
char errorMsg[512] = {0}; | ||
|
||
CCaptureVisionRouter *cvr = new CCaptureVisionRouter; | ||
|
||
MyVideoFetcher *fetcher = new MyVideoFetcher(); | ||
fetcher->SetMaxImageCount(4); | ||
fetcher->SetBufferOverflowProtectionMode(BOPM_UPDATE); | ||
fetcher->SetColourChannelUsageType(CCUT_AUTO); | ||
cvr->SetInput(fetcher); | ||
|
||
CMultiFrameResultCrossFilter *filter = new CMultiFrameResultCrossFilter; | ||
filter->EnableResultCrossVerification(CRIT_BARCODE, true); | ||
// filter->EnableResultDeduplication(CRIT_BARCODE, true); | ||
filter->SetDuplicateForgetTime(CRIT_BARCODE, 5000); | ||
cvr->AddResultFilter(filter); | ||
|
||
CCapturedResultReceiver *capturedReceiver = new MyCapturedResultReceiver; | ||
cvr->AddResultReceiver(capturedReceiver); | ||
|
||
errorCode = cvr->StartCapturing(CPresetTemplate::PT_READ_BARCODES, false, errorMsg, 512); | ||
if (errorCode != EC_OK) | ||
{ | ||
cout << "error:" << errorMsg << endl; | ||
} | ||
else | ||
{ | ||
int width = (int)capture.get(CAP_PROP_FRAME_WIDTH); | ||
int height = (int)capture.get(CAP_PROP_FRAME_HEIGHT); | ||
|
||
for (int i = 1;; ++i) | ||
{ | ||
Mat frame; | ||
capture.read(frame); | ||
if (frame.empty()) | ||
{ | ||
cerr << "ERROR: Can't grab camera frame." << endl; | ||
break; | ||
} | ||
CFileImageTag tag(nullptr, 0, 0); | ||
tag.SetImageId(i); | ||
CImageData data(frame.rows * frame.step.p[0], | ||
frame.data, | ||
width, | ||
height, | ||
frame.step.p[0], | ||
IPF_RGB_888, | ||
0, | ||
&tag); | ||
fetcher->MyAddImageToBuffer(&data); | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(barcodeResultsMutex); | ||
for (const auto &result : barcodeResults) | ||
{ | ||
// Draw the bounding box | ||
if (result.localizationPoints.size() == 4) | ||
{ | ||
for (size_t i = 0; i < result.localizationPoints.size(); ++i) | ||
{ | ||
cv::line(frame, result.localizationPoints[i], | ||
result.localizationPoints[(i + 1) % result.localizationPoints.size()], | ||
cv::Scalar(0, 255, 0), 2); | ||
} | ||
} | ||
|
||
// Draw the barcode type and value | ||
if (!result.localizationPoints.empty()) | ||
{ | ||
cv::putText(frame, result.type + ": " + result.value, | ||
result.localizationPoints[0], cv::FONT_HERSHEY_SIMPLEX, | ||
0.5, cv::Scalar(0, 255, 0), 2); | ||
} | ||
} | ||
} | ||
|
||
imshow("1D/2D Barcode Scanner", frame); | ||
int key = waitKey(1); | ||
if (key == 27 /*ESC*/) | ||
break; | ||
} | ||
cvr->StopCapturing(); | ||
} | ||
|
||
delete cvr, cvr = NULL; | ||
delete fetcher, fetcher = NULL; | ||
delete filter, filter = NULL; | ||
delete capturedReceiver, capturedReceiver = NULL; | ||
|
||
return 0; | ||
} |
Oops, something went wrong.