Skip to content

Commit

Permalink
Updated to DBR v9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Mar 14, 2022
1 parent ddadbca commit 53cb73b
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 947 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

branches:
only:
- master
- main

script:
- mkdir build
Expand Down
170 changes: 98 additions & 72 deletions BarcodeReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,98 +9,121 @@ using namespace std;
using namespace dynamsoft::dbr;

#if defined(LINUX) || defined(MACOS)
#include <sys/time.h>
#include <sys/time.h>

int gettime()
int gettime()
{
struct timeval time;
struct timeval time;
gettimeofday(&time, NULL);
return (int)(time.tv_sec * 1000 * 1000 + time.tv_usec) / 1000;
}
return (int)(time.tv_sec * 1000 * 1000 + time.tv_usec) / 1000;
}
#else
int gettime()
int gettime()
{
return (int)(GetTickCount());
}
#endif

char* read_file_text(const char* filename) {
FILE *fp = fopen(filename, "r");
size_t size;
char *text = NULL;
char *read_file_text(const char *filename)
{
FILE *fp = fopen(filename, "r");
size_t size;
char *text = NULL;
if (fp)
{
fseek(fp, 0, SEEK_END);
size = ftell(fp);
}
else {
else
{
cout << "Fail to open file" << endl;
return NULL;
}

rewind(fp);
text = (char *)calloc((size + 1), sizeof(char));
if (text == NULL) {fputs ("Memory error",stderr); return NULL;}

if (text == NULL)
{
fputs("Memory error", stderr);
return NULL;
}

char c;
char *tmp = text;
do {
c = fgetc (fp);
*tmp = c;
tmp++;
} while (c != EOF);
fclose (fp);
do
{
c = fgetc(fp);
*tmp = c;
tmp++;
} while (c != EOF);
fclose(fp);
return text;
}

unsigned char * read_file_binary(const char* filename, int* out_size) {
FILE *fp = fopen(filename, "rb");
size_t size;
unsigned char *buffer = NULL;
unsigned char *read_file_binary(const char *filename, int *out_size)
{
FILE *fp = fopen(filename, "rb");
size_t size;
unsigned char *buffer = NULL;
if (fp)
{
fseek(fp, 0, SEEK_END);
size = ftell(fp);
}
else {
else
{
cout << "Fail to open file" << endl;
return NULL;
}

rewind(fp);
buffer = ( unsigned char *)malloc(sizeof( unsigned char) * size);
if (buffer == NULL) {fputs ("Memory error",stderr); return NULL;}
buffer = (unsigned char *)malloc(sizeof(unsigned char) * size);
if (buffer == NULL)
{
fputs("Memory error", stderr);
return NULL;
}

size_t result = fread(buffer, 1, size, fp);
*out_size = size;
if (result != size) {fputs ("Reading error",stderr); return NULL;}
fclose (fp);
if (result != size)
{
fputs("Reading error", stderr);
return NULL;
}
fclose(fp);
return buffer;
}

int barcode_decoding(const unsigned char* buffer, int size, int formats, int threadcount, char* license, char* config)
int barcode_decoding(const unsigned char *buffer, int size, int formats, int threadcount, char *license, char *config)
{
std::thread::id thread_id = std::this_thread::get_id();

if (license)
{
char errorMsgBuffer[512];
// Click https://www.dynamsoft.com/customer/license/trialLicense/?product=dbr to get a trial license.
DBR_InitLicense(license, errorMsgBuffer, 512);
printf("DBR_InitLicense: %s\n", errorMsgBuffer);
}

// Initialize Dynamsoft Barcode Reader
void* reader = DBR_CreateInstance();
const char* version = DBR_GetVersion();
void *reader = DBR_CreateInstance();
const char *version = DBR_GetVersion();
printf("DBR version: %s\n", version);


if (license) {DBR_InitLicense (reader, license);}

// Load the configuration from a template file
if (config)
{
char szErrorMsg[256];
int ret = DBR_InitRuntimeSettingsWithString(reader, config, CM_OVERWRITE, szErrorMsg, 256);
if (ret) printf("Template status: %s\n\n", szErrorMsg);
if (ret)
printf("Template status: %s\n\n", szErrorMsg);
}

// Update the parameters
char sError[512];
PublicRuntimeSettings* runtimeSettings = new PublicRuntimeSettings();
PublicRuntimeSettings *runtimeSettings = new PublicRuntimeSettings();
DBR_GetRuntimeSettings(reader, runtimeSettings);
runtimeSettings->maxAlgorithmThreadCount = threadcount;
runtimeSettings->barcodeFormatIds = formats;
Expand All @@ -112,7 +135,7 @@ int barcode_decoding(const unsigned char* buffer, int size, int formats, int thr
int iRet = DBR_DecodeFileInMemory(reader, buffer, (int)size, "");
int endtime = gettime();
int timecost = endtime - starttime;

// Output barcode result
if (iRet != DBR_OK && iRet != DBRERR_MAXICODE_LICENSE_INVALID && iRet != DBRERR_AZTEC_LICENSE_INVALID && iRet != DBRERR_LICENSE_EXPIRED && iRet != DBRERR_QR_LICENSE_INVALID && iRet != DBRERR_GS1_COMPOSITE_LICENSE_INVALID &&
iRet != DBRERR_1D_LICENSE_INVALID && iRet != DBRERR_PDF417_LICENSE_INVALID && iRet != DBRERR_DATAMATRIX_LICENSE_INVALID && iRet != DBRERR_GS1_DATABAR_LICENSE_INVALID && iRet != DBRERR_PATCHCODE_LICENSE_INVALID)
Expand All @@ -123,94 +146,97 @@ int barcode_decoding(const unsigned char* buffer, int size, int formats, int thr

TextResultArray *paryResult = NULL;
DBR_GetAllTextResults(reader, &paryResult);

if (paryResult->resultsCount == 0)
{
printf("No barcode found.\n");
DBR_FreeTextResults(&paryResult);
return -1;
}

printf("Thread id: %d. Total barcode(s) found: %d. Time cost: %d ms\n\n", thread_id, paryResult->resultsCount, timecost);
// for (int index = 0; index < paryResult->resultsCount; index++)
// {
// printf("Barcode %d:\n", index + 1);
// printf(" Type: %s\n", paryResult->results[index]->barcodeFormatString);
// printf(" Text: %s\n", paryResult->results[index]->barcodeText);
// }

for (int index = 0; index < paryResult->resultsCount; index++)
{
printf("Barcode %d:\n", index + 1);
printf(" Type: %s\n", paryResult->results[index]->barcodeFormatString);
printf(" Text: %s\n", paryResult->results[index]->barcodeText);
}

DBR_FreeTextResults(&paryResult);

DBR_DestroyInstance(reader);
return timecost;
}

void ToHexString(unsigned char* pSrc, int iLen, char* pDest)
void ToHexString(unsigned char *pSrc, int iLen, char *pDest)
{
const char HEXCHARS[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
const char HEXCHARS[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

int i;
char* ptr = pDest;
char *ptr = pDest;

for(i = 0; i < iLen; ++i)
for (i = 0; i < iLen; ++i)
{
snprintf(ptr, 4, "%c%c ", HEXCHARS[ ( pSrc[i] & 0xF0 ) >> 4 ], HEXCHARS[ ( pSrc[i] & 0x0F ) >> 0 ]);
snprintf(ptr, 4, "%c%c ", HEXCHARS[(pSrc[i] & 0xF0) >> 4], HEXCHARS[(pSrc[i] & 0x0F) >> 0]);
ptr += 3;
}
}

void multi_thread_performance(int processor_count, unsigned char *buffer, int size, int formats, char* license, char* config)
void multi_thread_performance(int processor_count, unsigned char *buffer, int size, int formats, char *license, char *config)
{
int minimum_count = 1, minimum_timecost = 0;
for (int i = 0; i < processor_count; i++)
for (int i = 0; i < processor_count; i++)
{
printf("Thread count: %d. ", i + 1);
int timecost = barcode_decoding(buffer, size, formats, i, license, config);
if (i == 0)
if (i == 0)
{
minimum_count = 1;
minimum_count = 1;
if (timecost > 0)
{
minimum_timecost = timecost;
minimum_timecost = timecost;
}
}
else {
else
{
if (timecost < minimum_timecost)
{
minimum_count = i + 1;
minimum_timecost = timecost;
minimum_timecost = timecost;
}
}
}
printf("Multi-thread best performance: thread_count = %d, timecost = %d \n\n", minimum_count, minimum_timecost);
}

int main(int argc, const char* argv[])
int main(int argc, const char *argv[])
{
const auto processor_count = std::thread::hardware_concurrency();
printf("CPU threads: %d\n\n", processor_count);
printf("Barcode Reader Version %d.%d\n\n",
BarcodeReader_VERSION_MAJOR, BarcodeReader_VERSION_MINOR);
BarcodeReader_VERSION_MAJOR, BarcodeReader_VERSION_MINOR);

if (argc < 2) {
if (argc < 2)
{
printf("Usage: BarcodeReader [image-file] [optional: license-file] [optional: template-file] \n");
return 0;
return 0;
}

char* license = NULL;
char* config = NULL;
switch(argc) {
case 4:
char *license = NULL;
char *config = NULL;
switch (argc)
{
case 4:
config = read_file_text(argv[3]);
case 3:
case 3:
license = read_file_text(argv[2]);
}

int size = 0;
unsigned char* buffer = read_file_binary(argv[1], &size);
if (!buffer) return 0;

unsigned char *buffer = read_file_binary(argv[1], &size);
if (!buffer)
return 0;

// Call decoding methods on the main thread
printf("---------------- Single thread decoding performance ----------------\n\n");
Expand All @@ -227,7 +253,7 @@ int main(int argc, const char* argv[])
// Call decoding methods on worker threads
printf("---------------- Decoding barcodes on worker threads ----------------\n\n");
int starttime = gettime();
// thread t1(barcode_decoding, buffer, size, BF_ONED);
// thread t1(barcode_decoding, buffer, size, BF_ONED);
thread t2(barcode_decoding, buffer, size, BF_QR_CODE, 1, license, config);
thread t3(barcode_decoding, buffer, size, BF_PDF417, 1, license, config);
thread t4(barcode_decoding, buffer, size, BF_DATAMATRIX, 1, license, config);
Expand All @@ -248,7 +274,7 @@ int main(int argc, const char* argv[])
// All
printf("-------------------------------- All --------------------------------\n\n");
multi_thread_performance((int)processor_count, buffer, size, BF_ALL, license, config);

free(license);
free(config);
free(buffer);
Expand Down
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Building C/C++ Barcode Reader with CMake

[![release](https://img.shields.io/github/release/Dynamsoft/cmake.svg)](https://github.com/Dynamsoft/cmake/releases/latest)
[![Travis CI status](https://img.shields.io/travis/dynamsoft/cmake/master?label=Travis%20CI&logo=travis)](https://travis-ci.com/github/Dynamsoft/cmake)

## What You Should Know
- [![](https://img.shields.io/badge/Download-Offline%20SDK-orange)](https://www.dynamsoft.com/barcode-reader/downloads)
- [![](https://img.shields.io/badge/Get-30--day%20FREE%20Trial%20License-blue)](https://www.dynamsoft.com/customer/license/trialLicense/?product=dbr)
## SDK Version
[v9.0](https://www.dynamsoft.com/barcode-reader/downloads)

## SDK Activation
Click [here](https://www.dynamsoft.com/customer/license/trialLicense/?product=dbr) to get a trial license.

## Supported Platforms
- Windows x86/x64
Expand All @@ -17,13 +18,9 @@
Set the license key in `BarcodeReader.cxx`:

```cpp
DBR_InitLicense(reader, "LICENSE-KEY");
DBR_InitLicense(license, errorMsgBuffer, 512);
```
## Contact Us
- [email protected]
- https://www.dynamsoft.com/Company/Contact.aspx
## Windows
1. Create a **build** folder:
```
Expand All @@ -48,7 +45,7 @@ DBR_InitLicense(reader, "LICENSE-KEY");
```
3. Run the app:
```
Release\BarcodeReader [image-file] [optional: license-file] [optional: template-file]
.\Release\BarcodeReader.exe [image-file] [optional: license-file] [optional: template-file]
```

## Linux and Raspberry Pi OS
Expand Down
Loading

0 comments on commit 53cb73b

Please sign in to comment.