Skip to content

Commit

Permalink
Integrated barcode scanning into the camera lib example
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Jun 18, 2024
1 parent a71653a commit 716e42c
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 13 deletions.
35 changes: 30 additions & 5 deletions camera_lib/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ cmake_minimum_required(VERSION 3.10)
project(camera_example)

if(WIN32)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../lib/windows)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../lib/windows ${CMAKE_CURRENT_SOURCE_DIR}/../../examples/10.x/sdk/platforms/win/lib)
file(GLOB DLL_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../lib/windows/*.dll")
set(DBR_LIBS "DynamsoftCorex64" "DynamsoftLicensex64" "DynamsoftCaptureVisionRouterx64" "DynamsoftUtilityx64")
elseif(UNIX)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../lib/linux)
SET(CMAKE_CXX_FLAGS "-std=c++11 -O3 -Wl,-rpath=$ORIGIN")
SET(CMAKE_INSTALL_RPATH "$ORIGIN")
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../lib/linux ${CMAKE_CURRENT_SOURCE_DIR}/../../examples/10.x/sdk/platforms/linux)
set(DBR_LIBS "DynamsoftCore" "DynamsoftLicense" "DynamsoftCaptureVisionRouter" "DynamsoftUtility" pthread)
endif()

# Create the executable
add_executable(camera_example main.cpp)
target_include_directories(camera_example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include)
target_link_libraries(camera_example camera_lib)
target_include_directories(camera_example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../../examples/10.x/sdk/include)
target_link_libraries(camera_example camera_lib ${DBR_LIBS})

if(WIN32)
add_custom_command(TARGET camera_example POST_BUILD
Expand All @@ -24,9 +28,30 @@ if(WIN32)
${DLL_FILE}
$<TARGET_FILE_DIR:camera_example>)
endforeach()

add_custom_command(TARGET camera_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_SOURCE_DIR}/../../examples/10.x/sdk/platforms/win/bin/
$<TARGET_FILE_DIR:camera_example>)
endif()

if(UNIX)
add_custom_command(TARGET camera_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:camera_lib> $<TARGET_FILE_DIR:camera_example>)
endif()
endif()

add_custom_command(TARGET camera_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/../../examples/10.x/sdk/DBR-PresetTemplates.json
$<TARGET_FILE_DIR:camera_example>/DBR-PresetTemplates.json)

add_custom_command(TARGET camera_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/../../examples/10.x/sdk/DLR-PresetTemplates.json
$<TARGET_FILE_DIR:camera_example>/DLR-PresetTemplates.json)

add_custom_command(TARGET camera_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:camera_lib>/CharacterModel
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_SOURCE_DIR}/../../examples/10.x/sdk/CharacterModel
$<TARGET_FILE_DIR:camera_example>/CharacterModel)
157 changes: 152 additions & 5 deletions camera_lib/example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,168 @@
#include <iostream>
#include <vector>
#include <mutex>
#include "camera_lib.h"

#include "DynamsoftCaptureVisionRouter.h"
#include "DynamsoftUtility.h"

using namespace dynamsoft::license;
using namespace dynamsoft::cvr;
using namespace dynamsoft::dbr;
using namespace dynamsoft::utility;
using namespace dynamsoft::basic_structures;

struct BarcodeResult
{
std::string type;
std::string value;
int x1, y1, x2, y2, x3, y3, x4, y4;
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)
{
std::cout << "Error: " << pResult->GetErrorString() << std::endl;
}
else
{
auto tag = pResult->GetOriginalImageTag();
if (tag)
std::cout << "ImageID:" << tag->GetImageId() << std::endl;
int count = pResult->GetItemsCount();
std::cout << "Decoded " << count << " barcodes" << std::endl;

barcodeResults.clear();
for (int i = 0; i < count; i++)
{
const CBarcodeResultItem *barcodeResultItem = pResult->GetItem(i);
if (barcodeResultItem != NULL)
{
std::cout << "Result " << i + 1 << std::endl;
std::cout << "Barcode Format: " << barcodeResultItem->GetFormatString() << std::endl;
std::cout << "Barcode Text: " << barcodeResultItem->GetText() << std::endl;
CPoint *points = barcodeResultItem->GetLocation().points;

BarcodeResult result;
result.type = barcodeResultItem->GetFormatString();
result.value = barcodeResultItem->GetText();
result.frameId = tag->GetImageId();
result.x1 = points[0][0];
result.y1 = points[0][1];
result.x2 = points[1][0];
result.y2 = points[1][1];
result.x3 = points[2][0];
result.y3 = points[2][1];
result.x4 = points[3][0];
result.y4 = points[3][1];

barcodeResults.push_back(result);
}
}
}

std::cout << std::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()
{
if (open_camera(0) == 0)
{
std::cout << "Camera opened successfully." << std::endl;
while (true)

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)
{
std::cout << szErrorMsg << std::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 | CRIT_TEXT_LINE, true);
cvr->AddResultFilter(filter);

CCapturedResultReceiver *capturedReceiver = new MyCapturedResultReceiver;
cvr->AddResultReceiver(capturedReceiver);

errorCode = cvr->StartCapturing(CPresetTemplate::PT_READ_BARCODES, false, errorMsg, 512);
if (errorCode != EC_OK)
{
std::cout << "error:" << errorMsg << std::endl;
}

for (int i = 1;; ++i)
{
ImageData frame = get_frame();
if (frame.data)
{
draw_line(&frame, 50, 50, 200, 200, 2, 255, 0, 0);
draw_line(&frame, 50, 200, 200, 50, 2, 0, 255, 0);
draw_text(&frame, "Hello, OpenCV!", 50, 50, 1, 2, 0, 255, 0);
CFileImageTag tag(nullptr, 0, 0);
tag.SetImageId(i);
printf("width: %d, height: %d, stride: %d\n", frame.width, frame.height, frame.stride);
CImageData data(frame.height * frame.stride,
frame.data,
frame.width,
frame.height,
frame.stride,
IPF_RGB_888,
0,
&tag);
fetcher->MyAddImageToBuffer(&data);

{
std::lock_guard<std::mutex> lock(barcodeResultsMutex);
for (const auto &result : barcodeResults)
{
// Draw the bounding box
draw_line(&frame, result.x1, result.y1, result.x2, result.y2, 2, 0, 255, 0);
draw_line(&frame, result.x2, result.y2, result.x3, result.y3, 2, 0, 255, 0);
draw_line(&frame, result.x3, result.y3, result.x4, result.y4, 2, 0, 255, 0);
draw_line(&frame, result.x4, result.y4, result.x1, result.y1, 2, 0, 255, 0);

// Draw the barcode type and value
std::string text = result.type + ": " + result.value;
draw_text(&frame, text.c_str(), result.x1, result.y1 - 10, 1, 2, 0, 255, 0);
}
}

display_image(&frame);
display_image("1D/2D Barcode Scanner", &frame);
if (wait_key(30) >= 0)
{ // Add a delay and check for key press
release_image(&frame);
Expand Down
2 changes: 1 addition & 1 deletion camera_lib/include/camera_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern "C"
CAMERA_LIB_API void close_camera();
CAMERA_LIB_API ImageData get_frame();
CAMERA_LIB_API void release_image(ImageData *image);
CAMERA_LIB_API void display_image(const ImageData *image);
CAMERA_LIB_API void display_image(const char *name, const ImageData *image);
CAMERA_LIB_API void draw_line(ImageData *image, int x1, int y1, int x2, int y2, int thickness, int r, int g, int b);
CAMERA_LIB_API void draw_text(ImageData *image, const char *text, int x, int y, int font_scale, int thickness, int r, int g, int b);
CAMERA_LIB_API int wait_key(int delay);
Expand Down
4 changes: 2 additions & 2 deletions camera_lib/src/camera_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ void release_image(ImageData *image)
}
}

void display_image(const ImageData *image)
void display_image(const char *name, const ImageData *image)
{
if (image && image->data)
{
int type = image->pixel_format == PIXEL_FORMAT_BGR ? CV_8UC3 : CV_8UC1;
cv::Mat frame(image->height, image->width, type, image->data, image->stride);
cv::imshow("Frame", frame);
cv::imshow(name, frame);
}
}

Expand Down

0 comments on commit 716e42c

Please sign in to comment.