@@ -35,175 +35,175 @@ using namespace ZXing;
35
35
36
36
static void PrintUsage (const char * exePath)
37
37
{
38
- std::cout << " Usage: " << exePath << " [-size <width>x<height>] [-margin <margin>] [-encoding <encoding>] [-ecc <level>] <format> <text> <output>" << std::endl
39
- << " -size Size of generated image" << std::endl
40
- << " -margin Margin around barcode" << std::endl
41
- << " -encoding Encoding used to encode input text" << std::endl
42
- << " -ecc Error correction level, [0-8]"
43
- << std::endl
44
- << " Supported formats are:" << std::endl
45
- << " AZTEC" << std::endl
46
- << " CODABAR" << std::endl
47
- << " CODE_39" << std::endl
48
- << " CODE_93" << std::endl
49
- << " CODE_128" << std::endl
50
- << " DATA_MATRIX" << std::endl
51
- << " EAN_8" << std::endl
52
- << " EAN_13" << std::endl
53
- << " ITF" << std::endl
54
- << " PDF_417" << std::endl
55
- << " QR_CODE" << std::endl
56
- << " UPC_A" << std::endl
57
- << " UPC_E" << std::endl
58
- << " Formats can be lowercase letters, with or without underscore." << std::endl;
38
+ std::cout << " Usage: " << exePath << " [-size <width>x<height>] [-margin <margin>] [-encoding <encoding>] [-ecc <level>] <format> <text> <output>" << std::endl
39
+ << " -size Size of generated image" << std::endl
40
+ << " -margin Margin around barcode" << std::endl
41
+ << " -encoding Encoding used to encode input text" << std::endl
42
+ << " -ecc Error correction level, [0-8]"
43
+ << std::endl
44
+ << " Supported formats are:" << std::endl
45
+ << " AZTEC" << std::endl
46
+ << " CODABAR" << std::endl
47
+ << " CODE_39" << std::endl
48
+ << " CODE_93" << std::endl
49
+ << " CODE_128" << std::endl
50
+ << " DATA_MATRIX" << std::endl
51
+ << " EAN_8" << std::endl
52
+ << " EAN_13" << std::endl
53
+ << " ITF" << std::endl
54
+ << " PDF_417" << std::endl
55
+ << " QR_CODE" << std::endl
56
+ << " UPC_A" << std::endl
57
+ << " UPC_E" << std::endl
58
+ << " Formats can be lowercase letters, with or without underscore." << std::endl;
59
59
}
60
60
61
61
static std::string FormatClean (std::string str)
62
62
{
63
- std::transform (str.begin (), str.end (), str.begin (), [](char c) { return (char )std::tolower (c); });
64
- str.erase (std::remove (str.begin (), str.end (), ' _' ), str.end ());
65
- return str;
63
+ std::transform (str.begin (), str.end (), str.begin (), [](char c) { return (char )std::tolower (c); });
64
+ str.erase (std::remove (str.begin (), str.end (), ' _' ), str.end ());
65
+ return str;
66
66
}
67
67
68
68
static std::string ParseFormat (std::string str)
69
69
{
70
- str = FormatClean (str);
71
- for (int i = 0 ; i < (int )BarcodeFormat::FORMAT_COUNT; ++i) {
72
- auto standardForm = ToString ((BarcodeFormat)i);
73
- if (str == FormatClean (standardForm))
74
- return standardForm;
75
- }
76
- return std::string ();
70
+ str = FormatClean (str);
71
+ for (int i = 0 ; i < (int )BarcodeFormat::FORMAT_COUNT; ++i) {
72
+ auto standardForm = ToString ((BarcodeFormat)i);
73
+ if (str == FormatClean (standardForm))
74
+ return standardForm;
75
+ }
76
+ return std::string ();
77
77
}
78
78
79
79
static bool ParseSize (std::string str, int * width, int * height)
80
80
{
81
- std::transform (str.begin (), str.end (), str.begin (), [](char c) { return (char )std::tolower (c); });
82
- auto xPos = str.find (' x' );
83
- if (xPos != std::string::npos) {
84
- *width = std::stoi (str.substr (0 , xPos));
85
- *height = std::stoi (str.substr (xPos + 1 ));
86
- return true ;
87
- }
88
- return false ;
81
+ std::transform (str.begin (), str.end (), str.begin (), [](char c) { return (char )std::tolower (c); });
82
+ auto xPos = str.find (' x' );
83
+ if (xPos != std::string::npos) {
84
+ *width = std::stoi (str.substr (0 , xPos));
85
+ *height = std::stoi (str.substr (xPos + 1 ));
86
+ return true ;
87
+ }
88
+ return false ;
89
89
}
90
90
91
91
static bool ParseOptions (int argc, char * argv[], int * width, int * height, int * margin, int * eccLevel, std::string* format, std::string* text, std::string* filePath)
92
92
{
93
- int nonOptArgCount = 0 ;
94
- for (int i = 1 ; i < argc; ++i) {
95
- if (strcmp (argv[i], " -size" ) == 0 ) {
96
- if (i + 1 < argc) {
97
- ++i;
98
- if (!ParseSize (argv[i], width, height)) {
99
- std::cerr << " Invalid size specification: " << argv[i] << std::endl;
100
- return false ;
101
- }
102
- }
103
- else {
104
- return false ;
105
- }
106
- }
107
- else if (strcmp (argv[i], " -margin" ) == 0 ) {
108
- if (i + 1 < argc) {
109
- ++i;
110
- *margin = std::stoi (argv[i]);
111
- }
112
- else {
113
- return false ;
114
- }
115
- }
116
- else if (strcmp (argv[i], " -ecc" ) == 0 ) {
117
- if (i + 1 < argc) {
118
- ++i;
119
- *eccLevel = std::stoi (argv[i]);
120
- }
121
- else {
122
- return false ;
123
- }
124
- }
125
- else if (nonOptArgCount == 0 ) {
126
- *format = ParseFormat (argv[i]);
127
- if (format->empty ()) {
128
- std::cerr << " Unreconigned format: " << argv[i] << std::endl;
129
- return false ;
130
- }
131
- ++nonOptArgCount;
132
- }
133
- else if (nonOptArgCount == 1 ) {
134
- *text = argv[i];
135
- ++nonOptArgCount;
136
- }
137
- else if (nonOptArgCount == 2 ) {
138
- *filePath = argv[i];
139
- ++nonOptArgCount;
140
- }
141
- else {
142
- return false ;
143
- }
144
- }
145
-
146
- return !format->empty () && !text->empty () && !filePath->empty ();
93
+ int nonOptArgCount = 0 ;
94
+ for (int i = 1 ; i < argc; ++i) {
95
+ if (strcmp (argv[i], " -size" ) == 0 ) {
96
+ if (i + 1 < argc) {
97
+ ++i;
98
+ if (!ParseSize (argv[i], width, height)) {
99
+ std::cerr << " Invalid size specification: " << argv[i] << std::endl;
100
+ return false ;
101
+ }
102
+ }
103
+ else {
104
+ return false ;
105
+ }
106
+ }
107
+ else if (strcmp (argv[i], " -margin" ) == 0 ) {
108
+ if (i + 1 < argc) {
109
+ ++i;
110
+ *margin = std::stoi (argv[i]);
111
+ }
112
+ else {
113
+ return false ;
114
+ }
115
+ }
116
+ else if (strcmp (argv[i], " -ecc" ) == 0 ) {
117
+ if (i + 1 < argc) {
118
+ ++i;
119
+ *eccLevel = std::stoi (argv[i]);
120
+ }
121
+ else {
122
+ return false ;
123
+ }
124
+ }
125
+ else if (nonOptArgCount == 0 ) {
126
+ *format = ParseFormat (argv[i]);
127
+ if (format->empty ()) {
128
+ std::cerr << " Unreconigned format: " << argv[i] << std::endl;
129
+ return false ;
130
+ }
131
+ ++nonOptArgCount;
132
+ }
133
+ else if (nonOptArgCount == 1 ) {
134
+ *text = argv[i];
135
+ ++nonOptArgCount;
136
+ }
137
+ else if (nonOptArgCount == 2 ) {
138
+ *filePath = argv[i];
139
+ ++nonOptArgCount;
140
+ }
141
+ else {
142
+ return false ;
143
+ }
144
+ }
145
+
146
+ return !format->empty () && !text->empty () && !filePath->empty ();
147
147
}
148
148
149
149
static std::string GetExtension (const std::string& path)
150
150
{
151
- auto fileNameStart = path.find_last_of (" /\\ " );
152
- auto fileName = fileNameStart == std::string::npos ? path : path.substr (fileNameStart + 1 );
153
- auto extStart = fileName.find_last_of (' .' );
154
- auto ext = extStart == std::string::npos ? " " : fileName.substr (extStart + 1 );
155
- std::transform (ext.begin (), ext.end (), ext.begin (), [](unsigned char c) { return std::tolower (c); });
156
- return ext;
151
+ auto fileNameStart = path.find_last_of (" /\\ " );
152
+ auto fileName = fileNameStart == std::string::npos ? path : path.substr (fileNameStart + 1 );
153
+ auto extStart = fileName.find_last_of (' .' );
154
+ auto ext = extStart == std::string::npos ? " " : fileName.substr (extStart + 1 );
155
+ std::transform (ext.begin (), ext.end (), ext.begin (), [](unsigned char c) { return std::tolower (c); });
156
+ return ext;
157
157
}
158
158
159
159
int main (int argc, char * argv[])
160
160
{
161
- if (argc <= 2 ) {
162
- PrintUsage (argv[0 ]);
163
- return 0 ;
164
- }
165
-
166
- int width = 100 , height = 100 ;
167
- int margin = 10 ;
168
- int eccLevel = -1 ;
169
- std::string format, text, filePath;
170
-
171
- if (!ParseOptions (argc, argv, &width, &height, &margin, &eccLevel, &format, &text, &filePath)) {
172
- PrintUsage (argv[0 ]);
173
- return -1 ;
174
- }
175
-
176
- try {
177
- auto barcodeFormat = BarcodeFormatFromString (format);
178
- if (barcodeFormat == BarcodeFormat::FORMAT_COUNT)
179
- throw std::invalid_argument (" Unsupported format: " + format);
180
-
181
- MultiFormatWriter writer (barcodeFormat);
182
- if (margin >= 0 )
183
- writer.setMargin (margin);
184
- if (eccLevel >= 0 )
185
- writer.setEccLevel (eccLevel);
186
-
187
- auto bitmap = writer.encode (TextUtfEncoding::FromUtf8 (text), width, height).toByteMatrix ();
188
-
189
- auto ext = GetExtension (filePath);
190
- int success = 0 ;
191
- if (ext == " " || ext == " png" ) {
192
- success = stbi_write_png (filePath.c_str (), bitmap.width (), bitmap.height (), 1 , bitmap.data (), 0 );
193
- }
194
- else if (ext == " jpg" || ext == " jpeg" ) {
195
- success = stbi_write_jpg (filePath.c_str (), bitmap.width (), bitmap.height (), 1 , bitmap.data (), 0 );
196
- }
197
-
198
- if (!success) {
199
- std::cerr << " Failed to write image: " << filePath << std::endl;
200
- return -1 ;
201
- }
202
- }
203
- catch (const std::exception& e) {
204
- std::cerr << e.what () << std::endl;
205
- return -1 ;
206
- }
207
-
208
- return 0 ;
161
+ if (argc <= 2 ) {
162
+ PrintUsage (argv[0 ]);
163
+ return 0 ;
164
+ }
165
+
166
+ int width = 100 , height = 100 ;
167
+ int margin = 10 ;
168
+ int eccLevel = -1 ;
169
+ std::string format, text, filePath;
170
+
171
+ if (!ParseOptions (argc, argv, &width, &height, &margin, &eccLevel, &format, &text, &filePath)) {
172
+ PrintUsage (argv[0 ]);
173
+ return -1 ;
174
+ }
175
+
176
+ try {
177
+ auto barcodeFormat = BarcodeFormatFromString (format);
178
+ if (barcodeFormat == BarcodeFormat::FORMAT_COUNT)
179
+ throw std::invalid_argument (" Unsupported format: " + format);
180
+
181
+ MultiFormatWriter writer (barcodeFormat);
182
+ if (margin >= 0 )
183
+ writer.setMargin (margin);
184
+ if (eccLevel >= 0 )
185
+ writer.setEccLevel (eccLevel);
186
+
187
+ auto bitmap = writer.encode (TextUtfEncoding::FromUtf8 (text), width, height).toByteMatrix ();
188
+
189
+ auto ext = GetExtension (filePath);
190
+ int success = 0 ;
191
+ if (ext == " " || ext == " png" ) {
192
+ success = stbi_write_png (filePath.c_str (), bitmap.width (), bitmap.height (), 1 , bitmap.data (), 0 );
193
+ }
194
+ else if (ext == " jpg" || ext == " jpeg" ) {
195
+ success = stbi_write_jpg (filePath.c_str (), bitmap.width (), bitmap.height (), 1 , bitmap.data (), 0 );
196
+ }
197
+
198
+ if (!success) {
199
+ std::cerr << " Failed to write image: " << filePath << std::endl;
200
+ return -1 ;
201
+ }
202
+ }
203
+ catch (const std::exception& e) {
204
+ std::cerr << e.what () << std::endl;
205
+ return -1 ;
206
+ }
207
+
208
+ return 0 ;
209
209
}
0 commit comments