Skip to content

Commit 4afea1e

Browse files
add VideoDecoding sample
1 parent 5613b57 commit 4afea1e

File tree

126 files changed

+72326
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+72326
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This repository contains multiple samples that demonstrates how to use the <a hr
1717
|---|---|
1818
| [`ReadAnImage`](Samples/HelloWorld/ReadAnImage) | This sample demonstrates the simplest way to read barcodes from an image file and output barcode format and text. |
1919
| [`ReadMultipleImages`](Samples/HelloWorld/ReadMultipleImages) | This sample demonstrates the simplest way to read barcodes from directory with image files and output barcode format and text. |
20+
| [`VideoDecoding`](Samples/VideoDecoding) | This sample demonstrates how to read barcodes from video frames. |
2021

2122
## License
2223

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.6)
2+
project(VideoDecoding)
3+
4+
set(DBRLIB ${CMAKE_CURRENT_SOURCE_DIR}/../../Distributables/Lib/Linux/x64)
5+
6+
set (CMAKE_CXX_STANDARD 11)
7+
add_compile_options(-O2 -fPIC)
8+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O2 -fvisibility=hidden -fvisibility-inlines-hidden -L ${DBRLIB} -Wl,-rpath,${DBRLIB} -Wl,-rpath,'$ORIGIN' -static-libgcc -static-libstdc++ -s")
9+
find_package(OpenCV 3.4.5 REQUIRED)
10+
11+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/thirdLib/Include)
12+
13+
file(GLOB FILE_SRCS
14+
VideoDecoding.cpp
15+
)
16+
add_executable(VideoDecoding ${FILE_SRCS})
17+
set_target_properties(VideoDecoding PROPERTIES SKIP_BUILD_RPATH TRUE)
18+
target_link_libraries(VideoDecoding DynamsoftCaptureVisionRouter DynamsoftLicense DynamsoftUtility DynamsoftCore opencv_core opencv_videoio opencv_highgui)
19+
File(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../../Distributables/DBR-PresetTemplates.json DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/../../Distributables/Lib/Linux/x64)
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Release|Win32">
5+
<Configuration>Release</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
</ItemGroup>
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>{6BAC218D-AD85-4749-9694-0B8A8547596B}</ProjectGuid>
11+
<Keyword>Win32Proj</Keyword>
12+
<RootNamespace>VideoDecoding</RootNamespace>
13+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
14+
<ProjectName>VideoDecoding</ProjectName>
15+
</PropertyGroup>
16+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
18+
<ConfigurationType>Application</ConfigurationType>
19+
<UseDebugLibraries>false</UseDebugLibraries>
20+
<PlatformToolset>v110</PlatformToolset>
21+
<WholeProgramOptimization>true</WholeProgramOptimization>
22+
<CharacterSet>Unicode</CharacterSet>
23+
</PropertyGroup>
24+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
25+
<ImportGroup Label="ExtensionSettings">
26+
</ImportGroup>
27+
<ImportGroup Label="Shared">
28+
</ImportGroup>
29+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
30+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
31+
</ImportGroup>
32+
<PropertyGroup Label="UserMacros" />
33+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
34+
<LinkIncremental>false</LinkIncremental>
35+
<OutDir>$(SolutionDir)$(Platform)\</OutDir>
36+
<IntDir>$(Platform)\$(Configuration)\</IntDir>
37+
</PropertyGroup>
38+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
39+
<ClCompile>
40+
<WarningLevel>Level3</WarningLevel>
41+
<PrecompiledHeader>
42+
</PrecompiledHeader>
43+
<Optimization>MaxSpeed</Optimization>
44+
<FunctionLevelLinking>true</FunctionLevelLinking>
45+
<IntrinsicFunctions>true</IntrinsicFunctions>
46+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
47+
<AdditionalIncludeDirectories>thirdLib\Include;</AdditionalIncludeDirectories>
48+
</ClCompile>
49+
<Link>
50+
<SubSystem>Console</SubSystem>
51+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
52+
<OptimizeReferences>true</OptimizeReferences>
53+
<GenerateDebugInformation>true</GenerateDebugInformation>
54+
<AdditionalDependencies>opencv_core345.lib;opencv_videoio345.lib;opencv_highgui345.lib;%(AdditionalDependencies)</AdditionalDependencies>
55+
<AdditionalLibraryDirectories>thirdLib\lib\$(Configuration)</AdditionalLibraryDirectories>
56+
</Link>
57+
<PostBuildEvent>
58+
<Command>copy "thirdLib\bin\$(Configuration)\*.*" "$(OutputPath)"
59+
60+
xcopy /ys "$(SolutionDir)..\..\Distributables\Lib\Windows\$(PlatformTarget)\*.dll" "$(OutputPath)"
61+
xcopy /ys "$(SolutionDir)..\..\Distributables\*.json" "$(OutputPath)"</Command>
62+
</PostBuildEvent>
63+
</ItemDefinitionGroup>
64+
<ItemGroup>
65+
<ClCompile Include="VideoDecoding.cpp" />
66+
</ItemGroup>
67+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
68+
<ImportGroup Label="ExtensionTargets">
69+
</ImportGroup>
70+
</Project>

0 commit comments

Comments
 (0)