@@ -9,98 +9,121 @@ using namespace std;
99using namespace dynamsoft ::dbr;
1010
1111#if defined(LINUX) || defined(MACOS)
12- #include < sys/time.h>
12+ #include < sys/time.h>
1313
14- int gettime ()
14+ int gettime ()
1515{
16- struct timeval time;
16+ struct timeval time;
1717 gettimeofday (&time, NULL );
18- return (int )(time.tv_sec * 1000 * 1000 + time.tv_usec ) / 1000 ;
19- }
18+ return (int )(time.tv_sec * 1000 * 1000 + time.tv_usec ) / 1000 ;
19+ }
2020#else
21- int gettime ()
21+ int gettime ()
2222{
2323 return (int )(GetTickCount ());
2424}
2525#endif
2626
27- char * read_file_text (const char * filename) {
28- FILE *fp = fopen (filename, " r" );
29- size_t size;
30- char *text = NULL ;
27+ char *read_file_text (const char *filename)
28+ {
29+ FILE *fp = fopen (filename, " r" );
30+ size_t size;
31+ char *text = NULL ;
3132 if (fp)
3233 {
3334 fseek (fp, 0 , SEEK_END);
3435 size = ftell (fp);
3536 }
36- else {
37+ else
38+ {
3739 cout << " Fail to open file" << endl;
3840 return NULL ;
3941 }
4042
4143 rewind (fp);
4244 text = (char *)calloc ((size + 1 ), sizeof (char ));
43- if (text == NULL ) {fputs (" Memory error" ,stderr); return NULL ;}
44-
45+ if (text == NULL )
46+ {
47+ fputs (" Memory error" , stderr);
48+ return NULL ;
49+ }
50+
4551 char c;
4652 char *tmp = text;
47- do {
48- c = fgetc (fp);
49- *tmp = c;
50- tmp++;
51- } while (c != EOF);
52- fclose (fp);
53+ do
54+ {
55+ c = fgetc (fp);
56+ *tmp = c;
57+ tmp++;
58+ } while (c != EOF);
59+ fclose (fp);
5360 return text;
5461}
5562
56- unsigned char * read_file_binary (const char * filename, int * out_size) {
57- FILE *fp = fopen (filename, " rb" );
58- size_t size;
59- unsigned char *buffer = NULL ;
63+ unsigned char *read_file_binary (const char *filename, int *out_size)
64+ {
65+ FILE *fp = fopen (filename, " rb" );
66+ size_t size;
67+ unsigned char *buffer = NULL ;
6068 if (fp)
6169 {
6270 fseek (fp, 0 , SEEK_END);
6371 size = ftell (fp);
6472 }
65- else {
73+ else
74+ {
6675 cout << " Fail to open file" << endl;
6776 return NULL ;
6877 }
6978
7079 rewind (fp);
71- buffer = ( unsigned char *)malloc (sizeof ( unsigned char ) * size);
72- if (buffer == NULL ) {fputs (" Memory error" ,stderr); return NULL ;}
80+ buffer = (unsigned char *)malloc (sizeof (unsigned char ) * size);
81+ if (buffer == NULL )
82+ {
83+ fputs (" Memory error" , stderr);
84+ return NULL ;
85+ }
7386
7487 size_t result = fread (buffer, 1 , size, fp);
7588 *out_size = size;
76- if (result != size) {fputs (" Reading error" ,stderr); return NULL ;}
77- fclose (fp);
89+ if (result != size)
90+ {
91+ fputs (" Reading error" , stderr);
92+ return NULL ;
93+ }
94+ fclose (fp);
7895 return buffer;
7996}
8097
81- int barcode_decoding (const unsigned char * buffer, int size, int formats, int threadcount, char * license, char * config)
98+ int barcode_decoding (const unsigned char * buffer, int size, int formats, int threadcount, char * license, char * config)
8299{
83100 std::thread::id thread_id = std::this_thread::get_id ();
84101
102+ if (license)
103+ {
104+ char errorMsgBuffer[512 ];
105+ // Click https://www.dynamsoft.com/customer/license/trialLicense/?product=dbr to get a trial license.
106+ DBR_InitLicense (license, errorMsgBuffer, 512 );
107+ printf (" DBR_InitLicense: %s\n " , errorMsgBuffer);
108+ }
109+
85110 // Initialize Dynamsoft Barcode Reader
86- void * reader = DBR_CreateInstance ();
87- const char * version = DBR_GetVersion ();
111+ void * reader = DBR_CreateInstance ();
112+ const char * version = DBR_GetVersion ();
88113 printf (" DBR version: %s\n " , version);
89-
90-
91- if (license) {DBR_InitLicense (reader, license);}
92114
93115 // Load the configuration from a template file
94116 if (config)
95117 {
96118 char szErrorMsg[256 ];
97119 int ret = DBR_InitRuntimeSettingsWithString (reader, config, CM_OVERWRITE, szErrorMsg, 256 );
98- if (ret) printf (" Template status: %s\n\n " , szErrorMsg);
120+ if (ret)
121+ printf (" Template status: %s\n\n " , szErrorMsg);
99122 }
100123
101124 // Update the parameters
102125 char sError [512 ];
103- PublicRuntimeSettings* runtimeSettings = new PublicRuntimeSettings ();
126+ PublicRuntimeSettings * runtimeSettings = new PublicRuntimeSettings ();
104127 DBR_GetRuntimeSettings (reader, runtimeSettings);
105128 runtimeSettings->maxAlgorithmThreadCount = threadcount;
106129 runtimeSettings->barcodeFormatIds = formats;
@@ -112,7 +135,7 @@ int barcode_decoding(const unsigned char* buffer, int size, int formats, int thr
112135 int iRet = DBR_DecodeFileInMemory (reader, buffer, (int )size, " " );
113136 int endtime = gettime ();
114137 int timecost = endtime - starttime;
115-
138+
116139 // Output barcode result
117140 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 &&
118141 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)
@@ -123,94 +146,97 @@ int barcode_decoding(const unsigned char* buffer, int size, int formats, int thr
123146
124147 TextResultArray *paryResult = NULL ;
125148 DBR_GetAllTextResults (reader, &paryResult);
126-
149+
127150 if (paryResult->resultsCount == 0 )
128151 {
129152 printf (" No barcode found.\n " );
130153 DBR_FreeTextResults (&paryResult);
131154 return -1 ;
132155 }
133-
156+
134157 printf (" Thread id: %d. Total barcode(s) found: %d. Time cost: %d ms\n\n " , thread_id, paryResult->resultsCount , timecost);
135-
136- // for (int index = 0; index < paryResult->resultsCount; index++)
137- // {
138- // printf("Barcode %d:\n", index + 1);
139- // printf(" Type: %s\n", paryResult->results[index]->barcodeFormatString);
140- // printf(" Text: %s\n", paryResult->results[index]->barcodeText);
141- // }
158+
159+ for (int index = 0 ; index < paryResult->resultsCount ; index++)
160+ {
161+ printf (" Barcode %d:\n " , index + 1 );
162+ printf (" Type: %s\n " , paryResult->results [index]->barcodeFormatString );
163+ printf (" Text: %s\n " , paryResult->results [index]->barcodeText );
164+ }
142165
143166 DBR_FreeTextResults (&paryResult);
144167
145168 DBR_DestroyInstance (reader);
146169 return timecost;
147170}
148171
149- void ToHexString (unsigned char * pSrc, int iLen, char * pDest)
172+ void ToHexString (unsigned char * pSrc, int iLen, char * pDest)
150173{
151- const char HEXCHARS[16 ] = { ' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' A' , ' B' , ' C' , ' D' , ' E' , ' F' };
174+ const char HEXCHARS[16 ] = {' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' A' , ' B' , ' C' , ' D' , ' E' , ' F' };
152175
153176 int i;
154- char * ptr = pDest;
177+ char * ptr = pDest;
155178
156- for (i = 0 ; i < iLen; ++i)
179+ for (i = 0 ; i < iLen; ++i)
157180 {
158- snprintf (ptr, 4 , " %c%c " , HEXCHARS[ ( pSrc[i] & 0xF0 ) >> 4 ], HEXCHARS[ ( pSrc[i] & 0x0F ) >> 0 ]);
181+ snprintf (ptr, 4 , " %c%c " , HEXCHARS[( pSrc[i] & 0xF0 ) >> 4 ], HEXCHARS[( pSrc[i] & 0x0F ) >> 0 ]);
159182 ptr += 3 ;
160183 }
161184}
162185
163- void multi_thread_performance (int processor_count, unsigned char *buffer, int size, int formats, char * license, char * config)
186+ void multi_thread_performance (int processor_count, unsigned char *buffer, int size, int formats, char * license, char * config)
164187{
165188 int minimum_count = 1 , minimum_timecost = 0 ;
166- for (int i = 0 ; i < processor_count; i++)
189+ for (int i = 0 ; i < processor_count; i++)
167190 {
168191 printf (" Thread count: %d. " , i + 1 );
169192 int timecost = barcode_decoding (buffer, size, formats, i, license, config);
170- if (i == 0 )
193+ if (i == 0 )
171194 {
172- minimum_count = 1 ;
195+ minimum_count = 1 ;
173196 if (timecost > 0 )
174197 {
175- minimum_timecost = timecost;
198+ minimum_timecost = timecost;
176199 }
177200 }
178- else {
201+ else
202+ {
179203 if (timecost < minimum_timecost)
180204 {
181205 minimum_count = i + 1 ;
182- minimum_timecost = timecost;
206+ minimum_timecost = timecost;
183207 }
184208 }
185209 }
186210 printf (" Multi-thread best performance: thread_count = %d, timecost = %d \n\n " , minimum_count, minimum_timecost);
187211}
188212
189- int main (int argc, const char * argv[])
213+ int main (int argc, const char * argv[])
190214{
191215 const auto processor_count = std::thread::hardware_concurrency ();
192216 printf (" CPU threads: %d\n\n " , processor_count);
193217 printf (" Barcode Reader Version %d.%d\n\n " ,
194- BarcodeReader_VERSION_MAJOR, BarcodeReader_VERSION_MINOR);
218+ BarcodeReader_VERSION_MAJOR, BarcodeReader_VERSION_MINOR);
195219
196- if (argc < 2 ) {
220+ if (argc < 2 )
221+ {
197222 printf (" Usage: BarcodeReader [image-file] [optional: license-file] [optional: template-file] \n " );
198- return 0 ;
223+ return 0 ;
199224 }
200225
201- char * license = NULL ;
202- char * config = NULL ;
203- switch (argc) {
204- case 4 :
226+ char *license = NULL ;
227+ char *config = NULL ;
228+ switch (argc)
229+ {
230+ case 4 :
205231 config = read_file_text (argv[3 ]);
206- case 3 :
232+ case 3 :
207233 license = read_file_text (argv[2 ]);
208234 }
209235
210236 int size = 0 ;
211- unsigned char * buffer = read_file_binary (argv[1 ], &size);
212- if (!buffer) return 0 ;
213-
237+ unsigned char * buffer = read_file_binary (argv[1 ], &size);
238+ if (!buffer)
239+ return 0 ;
214240
215241 // Call decoding methods on the main thread
216242 printf (" ---------------- Single thread decoding performance ----------------\n\n " );
@@ -227,7 +253,7 @@ int main(int argc, const char* argv[])
227253 // Call decoding methods on worker threads
228254 printf (" ---------------- Decoding barcodes on worker threads ----------------\n\n " );
229255 int starttime = gettime ();
230- // thread t1(barcode_decoding, buffer, size, BF_ONED);
256+ // thread t1(barcode_decoding, buffer, size, BF_ONED);
231257 thread t2 (barcode_decoding, buffer, size, BF_QR_CODE, 1 , license, config);
232258 thread t3 (barcode_decoding, buffer, size, BF_PDF417, 1 , license, config);
233259 thread t4 (barcode_decoding, buffer, size, BF_DATAMATRIX, 1 , license, config);
@@ -248,7 +274,7 @@ int main(int argc, const char* argv[])
248274 // All
249275 printf (" -------------------------------- All --------------------------------\n\n " );
250276 multi_thread_performance ((int )processor_count, buffer, size, BF_ALL, license, config);
251-
277+
252278 free (license);
253279 free (config);
254280 free (buffer);
0 commit comments