diff --git a/examples/10.x/command-line/CMakeLists.txt b/examples/10.x/command-line/CMakeLists.txt new file mode 100644 index 0000000..0c41e51 --- /dev/null +++ b/examples/10.x/command-line/CMakeLists.txt @@ -0,0 +1,79 @@ +cmake_minimum_required (VERSION 3.8) +project (main) +MESSAGE( STATUS "PROJECT_NAME: " ${PROJECT_NAME} ) + +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.cpp) +if(WINDOWS) + if(CMAKE_CL_64) + target_link_libraries (${PROJECT_NAME} "DynamsoftCorex64" "DynamsoftLicensex64" "DynamsoftCaptureVisionRouterx64" "DynamsoftUtilityx64" ) + endif() +else() + target_link_libraries (${PROJECT_NAME} "DynamsoftCore" "DynamsoftLicense" "DynamsoftCaptureVisionRouter" "DynamsoftUtility" pthread) +endif() + +if(WINDOWS) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + "${PROJECT_SOURCE_DIR}/../sdk/platforms/win/bin/" + $) +else() + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + "${PROJECT_SOURCE_DIR}/../sdk/platforms/linux/" + $) +endif() + diff --git a/examples/10.x/command-line/main.cpp b/examples/10.x/command-line/main.cpp new file mode 100644 index 0000000..f86a39a --- /dev/null +++ b/examples/10.x/command-line/main.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +#if defined(_WIN32) || defined(_WIN64) +#include +#include +#include +#else +#include +#include +#include +#endif + +#include +#include +#include +#include + +#include "DynamsoftCaptureVisionRouter.h" +#include "DynamsoftUtility.h" +#include "template.h" + +using namespace std; +using namespace dynamsoft::license; +using namespace dynamsoft::cvr; +using namespace dynamsoft::dbr; +using namespace dynamsoft::basic_structures; + +bool GetImagePath(char *pImagePath) +{ + std::string input; + while (true) + { + std::cout << "\n>> Step 1: Input your image file's full path:\n"; + std::getline(std::cin, input); + + // Trim whitespace and remove surrounding quotes if present + input.erase(0, input.find_first_not_of(" \t\n\r\"\'")); // Trim leading + input.erase(input.find_last_not_of(" \t\n\r\"\'") + 1); // Trim trailing + + // Exit if user inputs 'q' or 'Q' + if (input == "q" || input == "Q") + { + return true; // Exit flag + } + + // Copy input to pImagePath ensuring not to exceed buffer size + std::strncpy(pImagePath, input.c_str(), 511); + pImagePath[511] = '\0'; // Ensure null-termination + + // Check if file exists using std::ifstream + std::ifstream file(pImagePath); + if (file.good()) + { + file.close(); + return false; // File path is valid + } + + std::cout << "Please input a valid path.\n"; + } +} + +int main(int argc, char *argv[]) +{ + printf("*************************************************\r\n"); + printf("Welcome to Dynamsoft Barcode Demo\r\n"); + printf("*************************************************\r\n"); + printf("Hints: Please input 'Q' or 'q' to quit the application.\r\n"); + + int iRet = -1; + char szErrorMsg[256]; + // Initialize license. + // Request a trial from https://www.dynamsoft.com/customer/license/trialLicense/?product=dcv&package=cross-platform + iRet = CLicenseManager::InitLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==", szErrorMsg, 256); + if (iRet != EC_OK) + { + cout << szErrorMsg << endl; + } + int errorCode = 1; + char errorMsg[512] = {0}; + + CCaptureVisionRouter *cvr = new CCaptureVisionRouter; + errorCode = cvr->InitSettings(jsonString.c_str(), errorMsg, 512); + if (errorCode != EC_OK) + { + cout << "error:" << errorMsg << endl; + return -1; + } + + char pszImageFile[512] = {0}; + bool bExit = false; + while (1) + { + bExit = GetImagePath(pszImageFile); + if (bExit) + break; + float costTime = 0.0; + int errorCode = 0; + + CCapturedResult *result = cvr->Capture(pszImageFile, CPresetTemplate::PT_READ_BARCODES); + + if (result->GetErrorCode() != 0) + { + cout << "Error: " << result->GetErrorCode() << "," << result->GetErrorString() << endl; + } + CDecodedBarcodesResult *barcodeResult = result->GetDecodedBarcodesResult(); + if (barcodeResult == nullptr || barcodeResult->GetItemsCount() == 0) + { + cout << "No barcode found." << endl; + } + else + { + int barcodeResultItemCount = barcodeResult->GetItemsCount(); + cout << "Decoded " << barcodeResultItemCount << " barcodes" << endl; + + for (int j = 0; j < barcodeResultItemCount; j++) + { + const CBarcodeResultItem *barcodeResultItem = barcodeResult->GetItem(j); + cout << "Result " << j + 1 << endl; + cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << endl; + cout << "Barcode Text: " << barcodeResultItem->GetText() << endl; + } + } + if (barcodeResult) + barcodeResult->Release(); + + result->Release(); + } + + delete cvr, cvr = NULL; + return 0; +} \ No newline at end of file diff --git a/examples/10.x/command-line/template.h b/examples/10.x/command-line/template.h new file mode 100644 index 0000000..b9916ea --- /dev/null +++ b/examples/10.x/command-line/template.h @@ -0,0 +1,362 @@ +#include + +#ifndef TEMPLATE_H +#define TEMPLATE_H + +const std::string jsonString = R"( + { + "CaptureVisionTemplates": [ + { + "Name": "ReadBarcodes_Default", + "ImageROIProcessingNameArray": [ + "roi-read-barcodes" + ] + }, + { + "Name": "ReadBarcodes_ReadRateFirst", + "ImageROIProcessingNameArray": [ + "roi-read-barcodes-read-rate" + ], + "Timeout": 100000 + }, + { + "Name": "ReadBarcodes_SpeedFirst", + "ImageROIProcessingNameArray": [ + "roi-read-barcodes-speed-first" + ] + }, + { + "Name": "ReadSingleBarcode", + "ImageROIProcessingNameArray": [ + "roi-read-single-barcode" + ] + } + ], + "TargetROIDefOptions": [ + { + "Name": "roi-read-barcodes", + "TaskSettingNameArray": [ + "task-read-barcodes" + ] + }, + { + "Name": "roi-read-barcodes-read-rate", + "TaskSettingNameArray": [ + "task-read-barcodes-read-rate" + ] + }, + { + "Name": "roi-read-barcodes-speed-first", + "TaskSettingNameArray": [ + "task-read-barcodes-speed-first" + ] + }, + { + "Name": "roi-read-single-barcode", + "TaskSettingNameArray": [ + "task-read-single-barcode" + ] + } + ], + "BarcodeReaderTaskSettingOptions": [ + { + "Name": "task-read-barcodes", + "ExpectedBarcodesCount": 0, + "BarcodeFormatIds": [ + "BF_DEFAULT" + ], + "BarcodeFormatSpecificationNameArray": [ + "bfs1", + "bfs2" + ], + "SectionImageParameterArray": [ + { + "Section": "ST_REGION_PREDETECTION", + "ImageParameterName": "ip-read-barcodes" + }, + { + "Section": "ST_BARCODE_LOCALIZATION", + "ImageParameterName": "ip-read-barcodes" + }, + { + "Section": "ST_BARCODE_DECODING", + "ImageParameterName": "ip-read-barcodes" + } + ] + }, + { + "Name": "task-read-barcodes-read-rate", + "ExpectedBarcodesCount": 999, + "BarcodeFormatIds": [ + "BF_DEFAULT" + ], + "LocalizationModes": [ + { + "Mode": "LM_CONNECTED_BLOCKS" + }, + { + "Mode": "LM_LINES" + }, + { + "Mode": "LM_STATISTICS" + } + ], + "DeblurModes": null, + "BarcodeFormatSpecificationNameArray": [ + "bfs1-read-rate-first", + "bfs2-read-rate-first" + ], + "SectionImageParameterArray": [ + { + "Section": "ST_REGION_PREDETECTION", + "ImageParameterName": "ip-read-barcodes-read-rate" + }, + { + "Section": "ST_BARCODE_LOCALIZATION", + "ImageParameterName": "ip-read-barcodes-read-rate" + }, + { + "Section": "ST_BARCODE_DECODING", + "ImageParameterName": "ip-read-barcodes-read-rate" + } + ] + }, + { + "Name": "task-read-barcodes-speed-first", + "ExpectedBarcodesCount": 0, + "BarcodeFormatIds": [ + "BF_DEFAULT" + ], + "LocalizationModes": [ + { + "Mode": "LM_CONNECTED_BLOCKS" + } + ], + "DeblurModes": [ + { + "Mode": "DM_BASED_ON_LOC_BIN" + }, + { + "Mode": "DM_THRESHOLD_BINARIZATION" + }, + { + "Mode": "DM_DIRECT_BINARIZATION" + } + ], + "BarcodeFormatSpecificationNameArray": [ + "bfs1-speed-first", + "bfs2-speed-first" + ], + "SectionImageParameterArray": [ + { + "Section": "ST_REGION_PREDETECTION", + "ImageParameterName": "ip-read-barcodes-speed-first" + }, + { + "Section": "ST_BARCODE_LOCALIZATION", + "ImageParameterName": "ip-read-barcodes-speed-first" + }, + { + "Section": "ST_BARCODE_DECODING", + "ImageParameterName": "ip-read-barcodes-speed-first" + } + ] + }, + { + "Name": "task-read-single-barcode", + "ExpectedBarcodesCount": 1, + "BarcodeFormatIds": [ + "BF_DEFAULT" + ], + "LocalizationModes": [ + { + "Mode": "LM_SCAN_DIRECTLY" + }, + { + "Mode": "LM_CONNECTED_BLOCKS" + } + ], + "DeblurModes": [ + { + "Mode": "DM_BASED_ON_LOC_BIN" + }, + { + "Mode": "DM_THRESHOLD_BINARIZATION" + } + ], + "BarcodeFormatSpecificationNameArray": [ + "bfs1-single-barcode", + "bfs2-single-barcode" + ], + "SectionImageParameterArray": [ + { + "Section": "ST_REGION_PREDETECTION", + "ImageParameterName": "ip-read-single-barcode" + }, + { + "Section": "ST_BARCODE_LOCALIZATION", + "ImageParameterName": "ip-read-single-barcode" + }, + { + "Section": "ST_BARCODE_DECODING", + "ImageParameterName": "ip-read-single-barcode" + } + ] + } + ], + "BarcodeFormatSpecificationOptions": [ + { + "Name": "bfs1", + "BarcodeFormatIds": [ + "BF_PDF417", + "BF_QR_CODE", + "BF_DATAMATRIX", + "BF_AZTEC", + "BF_MICRO_QR", + "BF_MICRO_PDF417", + "BF_DOTCODE" + ], + "MirrorMode": "MM_BOTH" + }, + { + "Name": "bfs2", + "BarcodeFormatIds": [ + "BF_ALL" + ], + "MirrorMode": "MM_NORMAL" + }, + { + "Name": "bfs1-read-rate-first", + "BarcodeFormatIds": [ + "BF_PDF417", + "BF_QR_CODE", + "BF_DATAMATRIX", + "BF_AZTEC", + "BF_MICRO_QR", + "BF_MICRO_PDF417", + "BF_DOTCODE" + ], + "MirrorMode": "MM_BOTH" + }, + { + "Name": "bfs2-read-rate-first", + "BarcodeFormatIds": [ + "BF_ALL" + ], + "MirrorMode": "MM_NORMAL" + }, + { + "Name": "bfs1-speed-first", + "BarcodeFormatIds": [ + "BF_PDF417", + "BF_QR_CODE", + "BF_DATAMATRIX", + "BF_AZTEC", + "BF_MICRO_QR", + "BF_MICRO_PDF417", + "BF_DOTCODE" + ], + "MirrorMode": "MM_BOTH" + }, + { + "Name": "bfs2-speed-first", + "BarcodeFormatIds": [ + "BF_ALL" + ], + "MirrorMode": "MM_NORMAL" + }, + { + "Name": "bfs1-single-barcode", + "BarcodeFormatIds": [ + "BF_PDF417", + "BF_QR_CODE", + "BF_DATAMATRIX", + "BF_AZTEC", + "BF_MICRO_QR", + "BF_MICRO_PDF417", + "BF_DOTCODE" + ], + "MirrorMode": "MM_BOTH" + }, + { + "Name": "bfs2-single-barcode", + "BarcodeFormatIds": [ + "BF_ALL" + ], + "MirrorMode": "MM_NORMAL" + } + ], + "ImageParameterOptions": [ + { + "Name": "ip-read-barcodes", + "TextDetectionMode": { + "Mode": "TTDM_LINE", + "Direction": "UNKNOWN", + "Sensitivity": 3 + }, + "IfEraseTextZone": 1 + }, + { + "Name": "ip-read-barcodes-read-rate", + "TextDetectionMode": { + "Mode": "TTDM_LINE", + "Direction": "UNKNOWN", + "Sensitivity": 3 + }, + "IfEraseTextZone": 1, + "GrayscaleTransformationModes": [ + { + "Mode": "GTM_ORIGINAL" + }, + { + "Mode": "GTM_INVERTED" + } + ], + "ScaleDownThreshold": 100000 + }, + { + "Name": "ip-read-barcodes-speed-first", + "TextDetectionMode": { + "Mode": "TTDM_LINE", + "Direction": "UNKNOWN", + "Sensitivity": 3 + }, + "IfEraseTextZone": 1, + "BinarizationModes": [ + { + "Mode": "BM_LOCAL_BLOCK", + "BlockSizeX": 0, + "BlockSizeY": 0, + "EnableFillBinaryVacancy": 0 + } + ] + }, + { + "Name": "ip-read-single-barcode", + "TextDetectionMode": { + "Mode": "TTDM_LINE", + "Direction": "UNKNOWN", + "Sensitivity": 3 + }, + "IfEraseTextZone": 1, + "BinarizationModes": [ + { + "Mode": "BM_LOCAL_BLOCK", + "BlockSizeX": 71, + "BlockSizeY": 71, + "EnableFillBinaryVacancy": 0 + } + ], + "GrayscaleTransformationModes": [ + { + "Mode": "GTM_ORIGINAL" + }, + { + "Mode": "GTM_INVERTED" + } + ] + } + ] + })"; + +#endif \ No newline at end of file