forked from zxing-cpp/zxing-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZXingWriter.cpp
225 lines (204 loc) · 7 KB
/
ZXingWriter.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright 2016 Nu-book Inc.
*/
// SPDX-License-Identifier: Apache-2.0
#ifdef ZXING_EXPERIMENTAL_API
#include "WriteBarcode.h"
#else
#include "BitMatrix.h"
#include "BitMatrixIO.h"
#include "CharacterSet.h"
#include "MultiFormatWriter.h"
#endif
#include "Version.h"
#include <algorithm>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
using namespace ZXing;
static void PrintUsage(const char* exePath)
{
std::cout << "Usage: " << exePath
<< " [-size <width/height>] [-eclevel <level>] [-noqz] [-hrt] <format> <text> <output>\n"
<< " -size Size of generated image\n"
// << " -margin Margin around barcode\n"
// << " -encoding Encoding used to encode input text\n"
<< " -eclevel Error correction level, [0-8]\n"
<< " -binary Interpret <text> as a file name containing binary data\n"
<< " -noqz Print barcode witout quiet zone\n"
<< " -hrt Print human readable text below the barcode (if supported)\n"
<< " -help Print usage information\n"
<< " -version Print version information\n"
<< "\n"
<< "Supported formats are:\n";
for (auto f : BarcodeFormatsFromString("Aztec Codabar Code39 Code93 Code128 DataMatrix EAN8 EAN13 ITF PDF417 QRCode UPCA UPCE"))
std::cout << " " << ToString(f) << "\n";
std::cout << "Format can be lowercase letters, with or without '-'.\n"
<< "Output format is determined by file name, supported are png, jpg and svg.\n";
}
static bool ParseSize(std::string str, int* width, int* height)
{
std::transform(str.begin(), str.end(), str.begin(), [](char c) { return (char)std::tolower(c); });
auto xPos = str.find('x');
if (xPos != std::string::npos) {
*width = std::stoi(str.substr(0, xPos));
*height = std::stoi(str.substr(xPos + 1));
return true;
}
return false;
}
struct CLI
{
BarcodeFormat format;
int sizeHint = 0;
std::string input;
std::string outPath;
std::string ecLevel;
bool inputIsFile = false;
bool withHRT = false;
bool withQZ = true;
bool verbose = false;
// CharacterSet encoding = CharacterSet::Unknown;
};
static bool ParseOptions(int argc, char* argv[], CLI& cli)
{
int nonOptArgCount = 0;
for (int i = 1; i < argc; ++i) {
auto is = [&](const char* str) { return strncmp(argv[i], str, strlen(argv[i])) == 0; };
if (is("-size")) {
if (++i == argc)
return false;
cli.sizeHint = std::stoi(argv[i]);
} else if (is("-eclevel")) {
if (++i == argc)
return false;
cli.ecLevel = argv[i];
// } else if (is("-margin")) {
// if (++i == argc)
// return false;
// cli.margin = std::stoi(argv[i]);
// } else if (is("-encoding")) {
// if (++i == argc)
// return false;
// cli.encoding = CharacterSetFromString(argv[i]);
} else if (is("-binary")) {
cli.inputIsFile = true;
} else if (is("-hrt")) {
cli.withHRT = true;
} else if (is("-noqz")) {
cli.withQZ = false;
} else if (is("-verbose")) {
cli.verbose = true;
} else if (is("-help") || is("--help")) {
PrintUsage(argv[0]);
exit(0);
} else if (is("-version") || is("--version")) {
std::cout << "ZXingWriter " << ZXING_VERSION_STR << "\n";
exit(0);
} else if (nonOptArgCount == 0) {
cli.format = BarcodeFormatFromString(argv[i]);
if (cli.format == BarcodeFormat::None) {
std::cerr << "Unrecognized format: " << argv[i] << std::endl;
return false;
}
++nonOptArgCount;
} else if (nonOptArgCount == 1) {
cli.input = argv[i];
++nonOptArgCount;
} else if (nonOptArgCount == 2) {
cli.outPath = argv[i];
++nonOptArgCount;
} else {
return false;
}
}
return nonOptArgCount == 3;
}
static std::string GetExtension(const std::string& path)
{
auto fileNameStart = path.find_last_of("/\\");
auto fileName = fileNameStart == std::string::npos ? path : path.substr(fileNameStart + 1);
auto extStart = fileName.find_last_of('.');
auto ext = extStart == std::string::npos ? "" : fileName.substr(extStart + 1);
std::transform(ext.begin(), ext.end(), ext.begin(), [](char c) { return std::tolower(c); });
return ext;
}
template <typename T = char>
std::vector<T> ReadFile(const std::string& fn)
{
std::basic_ifstream<T> ifs(fn, std::ios::binary);
if (!ifs.good())
throw std::runtime_error("failed to open/read file " + fn);
return ifs ? std::vector(std::istreambuf_iterator<T>(ifs), std::istreambuf_iterator<T>()) : std::vector<T>();
};
int main(int argc, char* argv[])
{
CLI cli;
if (!ParseOptions(argc, argv, cli)) {
PrintUsage(argv[0]);
return -1;
}
try {
#ifdef ZXING_EXPERIMENTAL_API
auto cOpts = CreatorOptions(cli.format).ecLevel(cli.ecLevel);
auto barcode = cli.inputIsFile ? CreateBarcodeFromBytes(ReadFile(cli.input), cOpts) : CreateBarcodeFromText(cli.input, cOpts);
auto wOpts = WriterOptions().sizeHint(cli.sizeHint).withQuietZones(cli.withQZ).withHRT(cli.withHRT).rotate(0);
auto bitmap = WriteBarcodeToImage(barcode, wOpts);
if (cli.verbose) {
std::cout << "Text: \"" << barcode.text() << "\"\n"
<< "Bytes: " << ToHex(barcode.bytes()) << "\n"
<< "Format: " << ToString(barcode.format()) << "\n"
<< "Identifier: " << barcode.symbologyIdentifier() << "\n"
<< "Content: " << ToString(barcode.contentType()) << "\n"
<< "HasECI: " << barcode.hasECI() << "\n"
<< "Position: " << ToString(barcode.position()) << "\n"
<< "Rotation: " << barcode.orientation() << " deg\n"
<< "IsMirrored: " << barcode.isMirrored() << "\n"
<< "IsInverted: " << barcode.isInverted() << "\n"
<< "ecLevel: " << barcode.ecLevel() << "\n";
std::cout << WriteBarcodeToUtf8(barcode);
}
#else
auto writer = MultiFormatWriter(cli.format).setMargin(cli.withQZ ? 10 : 0);
if (!cli.ecLevel.empty())
writer.setEccLevel(std::stoi(cli.ecLevel));
BitMatrix matrix;
if (cli.inputIsFile) {
auto file = ReadFile(cli.input);
std::wstring bytes;
for (uint8_t c : file)
bytes.push_back(c);
writer.setEncoding(CharacterSet::BINARY);
matrix = writer.encode(bytes, cli.sizeHint, std::clamp(cli.sizeHint / 2, 50, 300));
} else {
matrix = writer.encode(cli.input, cli.sizeHint, std::clamp(cli.sizeHint / 2, 50, 300));
}
auto bitmap = ToMatrix<uint8_t>(matrix);
#endif
auto ext = GetExtension(cli.outPath);
int success = 0;
if (ext == "" || ext == "png") {
success = stbi_write_png(cli.outPath.c_str(), bitmap.width(), bitmap.height(), 1, bitmap.data(), 0);
} else if (ext == "jpg" || ext == "jpeg") {
success = stbi_write_jpg(cli.outPath.c_str(), bitmap.width(), bitmap.height(), 1, bitmap.data(), 0);
} else if (ext == "svg") {
#ifdef ZXING_EXPERIMENTAL_API
success = (std::ofstream(cli.outPath) << WriteBarcodeToSVG(barcode, wOpts)).good();
#else
success = (std::ofstream(cli.outPath) << ToSVG(matrix)).good();
#endif
}
if (!success) {
std::cerr << "Failed to write image: " << cli.outPath << std::endl;
return -1;
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return -1;
}
return 0;
}