forked from yushulx/cmake-cpp-barcode-qrcode-mrz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarcodeReader.cxx
More file actions
143 lines (121 loc) · 3.58 KB
/
BarcodeReader.cxx
File metadata and controls
143 lines (121 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include "DynamsoftBarcodeReader.h"
#include "BarcodeReaderConfig.h"
#include <iostream>
#include <fstream>
using namespace std;
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 {
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;}
char c;
char *tmp = text;
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;
if (fp)
{
fseek(fp, 0, SEEK_END);
size = ftell(fp);
}
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;}
size_t result = fread(buffer, 1, size, fp);
*out_size = size;
if (result != size) {fputs ("Reading error",stderr); return NULL;}
fclose (fp);
return buffer;
}
int barcode_decoding(const unsigned char* buffer, int size, int formats, char* license)
{
// Initialize Dynamsoft Barcode Reader
CBarcodeReader reader;
reader.InitLicense(license);
// Get and update settings
char sError[512];
PublicRuntimeSettings* runtimeSettings = new PublicRuntimeSettings();
reader.GetRuntimeSettings(runtimeSettings);
// Four is the defalt value here, we can experiment with timing and adjust as needed
runtimeSettings->maxAlgorithmThreadCount = 4;
runtimeSettings->barcodeFormatIds = formats;
reader.UpdateRuntimeSettings(runtimeSettings, sError, 512);
delete runtimeSettings;
// Read barcodes from file stream
int iRet = reader.DecodeFileInMemory(buffer, (int)size, "");
// 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)
{
printf("Failed to read barcode: %s\n", CBarcodeReader::GetErrorString(iRet));
return 1;
}
TextResultArray *paryResult = NULL;
reader.GetAllTextResults(&paryResult);
if (paryResult->resultsCount == 0)
{
printf("No barcode found.\n");
CBarcodeReader::FreeTextResults(&paryResult);
return 4;
}
// Since more than one barcode can be scanned, the results set is an array.
for (int iIndex = 0; iIndex < paryResult->resultsCount; iIndex++)
{
// Print only the decoded text
printf("%s", paryResult->results[iIndex]->barcodeText);
}
CBarcodeReader::FreeTextResults(&paryResult);
return 0;
}
int main(int argc, const char* argv[])
{
if (argc < 3) {
printf("Usage: BarcodeReader [image-file] [license-file]\n");
return 2;
}
char* license = NULL;
// Read the license and exit with code 2 if it's invalid
license = read_file_text(argv[2]);
if (license == NULL) {
printf("License is null\n");
return 1;
}
int size = 0;
unsigned char* buffer = NULL;
buffer = read_file_binary(argv[1], &size);
if (buffer == NULL) {
printf("Image is null\n");
return 1;
}
int exitCode = 0;
exitCode = barcode_decoding(buffer, size, BF_QR_CODE, license);
free(license);
free(buffer);
return exitCode;
}