|
| 1 | +#include <opencv2/core.hpp> |
| 2 | +#include <opencv2/videoio.hpp> |
| 3 | +#include <opencv2/highgui.hpp> |
| 4 | +#include <iostream> |
| 5 | +#include <vector> |
| 6 | +#include <chrono> |
| 7 | +// Include headers of DynamsoftCaptureVisionRouter SDK |
| 8 | +#include <iostream> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +#include "../../Include/DynamsoftCaptureVisionRouter.h" |
| 12 | +#include "../../Include/DynamsoftUtility.h" |
| 13 | +using namespace std; |
| 14 | +using namespace dynamsoft::license; |
| 15 | +using namespace dynamsoft::cvr; |
| 16 | +using namespace dynamsoft::dbr; |
| 17 | +using namespace dynamsoft::utility; |
| 18 | +using namespace cv; |
| 19 | +#if defined(_WIN64) || defined(_WIN32) |
| 20 | +#ifdef _WIN64 |
| 21 | +#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftCorex64.lib") |
| 22 | +#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftLicensex64.lib") |
| 23 | +#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftCaptureVisionRouterx64.lib") |
| 24 | +#pragma comment(lib, "../../Distributables/Lib/Windows/x64/DynamsoftUtilityx64.lib") |
| 25 | +#else |
| 26 | +#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftCorex86.lib") |
| 27 | +#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftLicensex86.lib") |
| 28 | +#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftCaptureVisionRouterx86.lib") |
| 29 | +#pragma comment(lib, "../../Distributables/Lib/Windows/x86/DynamsoftUtilityx86.lib") |
| 30 | +#endif |
| 31 | +#endif |
| 32 | + |
| 33 | + |
| 34 | +class MyCapturedResultReceiver : public CCapturedResultReceiver { |
| 35 | + |
| 36 | + virtual void OnDecodedBarcodesReceived(CDecodedBarcodesResult* pResult)override |
| 37 | + { |
| 38 | + if (pResult->GetErrorCode() != EC_OK) |
| 39 | + { |
| 40 | + cout << "Error: " << pResult->GetErrorString() << endl; |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + auto tag = pResult->GetOriginalImageTag(); |
| 45 | + if (tag) |
| 46 | + cout << "ImageID:" << tag->GetImageId() << endl; |
| 47 | + int count = pResult->GetItemsCount(); |
| 48 | + cout << "Decoded " << count << " barcodes" << endl; |
| 49 | + for (int i = 0; i < count; i++) { |
| 50 | + const CBarcodeResultItem* barcodeResultItem = pResult->GetItem(i); |
| 51 | + if (barcodeResultItem != NULL) |
| 52 | + { |
| 53 | + cout << "Result " << i + 1 << endl; |
| 54 | + cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << endl; |
| 55 | + cout << "Barcode Text: " << barcodeResultItem->GetText() << endl; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + cout << endl; |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +class MyVideoFetcher : public CImageSourceAdapter |
| 65 | +{ |
| 66 | +public: |
| 67 | + MyVideoFetcher(){}; |
| 68 | + ~MyVideoFetcher(){}; |
| 69 | + bool HasNextImageToFetch()const override |
| 70 | + { |
| 71 | + return true; |
| 72 | + } |
| 73 | + void MyAddImageToBuffer(const CImageData* img, bool bClone = true) |
| 74 | + { |
| 75 | + AddImageToBuffer(img, bClone); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +int main() |
| 80 | +{ |
| 81 | + cout << "Opening camera..." << endl; |
| 82 | + VideoCapture capture(0); // open the first camera |
| 83 | + if (!capture.isOpened()) |
| 84 | + { |
| 85 | + cerr << "ERROR: Can't initialize camera capture" << endl; |
| 86 | + cout << "Press any key to quit..." << endl; |
| 87 | + cin.ignore(); |
| 88 | + return 1; |
| 89 | + } |
| 90 | + int iRet = -1; |
| 91 | + char szErrorMsg[256]; |
| 92 | + // Initialize license. |
| 93 | + // The string "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9" here is a free public trial license. Note that network connection is required for this license to work. |
| 94 | + // If you don't have a license yet, you can request a trial from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=samples&package=c_cpp |
| 95 | + iRet = CLicenseManager::InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", szErrorMsg, 256); |
| 96 | + if (iRet != EC_OK) |
| 97 | + { |
| 98 | + cout << szErrorMsg << endl; |
| 99 | + } |
| 100 | + int errorCode = 1; |
| 101 | + char errorMsg[512] = { 0 }; |
| 102 | + |
| 103 | + CCaptureVisionRouter *cvr = new CCaptureVisionRouter; |
| 104 | + |
| 105 | + MyVideoFetcher *fetcher = new MyVideoFetcher(); |
| 106 | + fetcher->SetMaxImageCount(100); |
| 107 | + fetcher->SetBufferOverflowProtectionMode(BOPM_UPDATE); |
| 108 | + fetcher->SetColourChannelUsageType(CCUT_AUTO); |
| 109 | + cvr->SetInput(fetcher); |
| 110 | + |
| 111 | + CMultiFrameResultCrossFilter* filter = new CMultiFrameResultCrossFilter; |
| 112 | + filter->EnableResultCrossVerification(CRIT_BARCODE, true); |
| 113 | + filter->EnableResultDeduplication(CRIT_BARCODE, true); |
| 114 | + filter->SetDuplicateForgetTime(CRIT_BARCODE, 5000); |
| 115 | + cvr->AddResultFilter(filter); |
| 116 | + |
| 117 | + CCapturedResultReceiver *capturedReceiver = new MyCapturedResultReceiver; |
| 118 | + cvr->AddResultReceiver(capturedReceiver); |
| 119 | + |
| 120 | + errorCode = cvr->StartCapturing(CPresetTemplate::PT_READ_BARCODES, false, errorMsg, 512); |
| 121 | + if (errorCode != EC_OK) |
| 122 | + { |
| 123 | + cout << "error:" << errorMsg << endl; |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + for (int i = 1;; ++i) |
| 128 | + { |
| 129 | + auto start = std::chrono::high_resolution_clock::now(); |
| 130 | + Mat frame; |
| 131 | + capture.read(frame); |
| 132 | + if (frame.empty()) |
| 133 | + { |
| 134 | + cerr << "ERROR: Can't grab camera frame." << endl; |
| 135 | + break; |
| 136 | + } |
| 137 | + CFileImageTag tag(nullptr, 0, 0); |
| 138 | + tag.SetImageId(i); |
| 139 | + CImageData data(frame.rows * frame.step.p[0], |
| 140 | + frame.data, |
| 141 | + capture.get(CAP_PROP_FRAME_WIDTH), |
| 142 | + capture.get(CAP_PROP_FRAME_HEIGHT), |
| 143 | + frame.step.p[0], |
| 144 | + IPF_RGB_888, |
| 145 | + 0, |
| 146 | + &tag); |
| 147 | + fetcher->MyAddImageToBuffer(&data); |
| 148 | + imshow("Frame", frame); |
| 149 | + auto finish = std::chrono::high_resolution_clock::now(); |
| 150 | + auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start); |
| 151 | + int usingTime = elapsed.count(); |
| 152 | + int key = 1; |
| 153 | + if (usingTime < 40) |
| 154 | + key = 40 - usingTime; |
| 155 | + key = waitKey(key); |
| 156 | + if (key == 27/*ESC*/) |
| 157 | + break; |
| 158 | + } |
| 159 | + cvr->StopCapturing(); |
| 160 | + } |
| 161 | + |
| 162 | + capture.release(); |
| 163 | + |
| 164 | + delete cvr, cvr = NULL; |
| 165 | + delete fetcher, fetcher = NULL; |
| 166 | + delete filter, filter = NULL; |
| 167 | + delete capturedReceiver, capturedReceiver = NULL; |
| 168 | + |
| 169 | + cout << "Press any key to quit..." << endl; |
| 170 | + cin.ignore(); |
| 171 | + return 0; |
| 172 | + |
| 173 | +} |
0 commit comments