|
7 | 7 |
|
8 | 8 | #include "WriteBarcode.h"
|
9 | 9 |
|
| 10 | +#ifdef ZXING_USE_ZINT |
| 11 | +#include <zint.h> |
| 12 | + |
| 13 | +template <> |
| 14 | +struct std::default_delete<zint_symbol> |
| 15 | +{ |
| 16 | + void operator()(zint_symbol* p) const noexcept { ZBarcode_Delete(p); } |
| 17 | +}; |
| 18 | + |
| 19 | +#else |
10 | 20 | struct zint_symbol {};
|
| 21 | +#endif |
11 | 22 |
|
12 | 23 | namespace ZXing {
|
13 | 24 |
|
@@ -71,6 +82,220 @@ WriterOptions& WriterOptions::operator=(WriterOptions&&) = default;
|
71 | 82 |
|
72 | 83 | } // namespace ZXing
|
73 | 84 |
|
| 85 | +#ifdef ZXING_USE_ZINT |
| 86 | +#include "ECI.h" |
| 87 | +#include "ReadBarcode.h" |
| 88 | + |
| 89 | +#include <zint.h> |
| 90 | +#include <charconv> |
| 91 | + |
| 92 | +namespace ZXing { |
| 93 | + |
| 94 | +struct BarcodeFormatZXing2Zint |
| 95 | +{ |
| 96 | + BarcodeFormat zxing; |
| 97 | + int zint; |
| 98 | +}; |
| 99 | + |
| 100 | +static constexpr BarcodeFormatZXing2Zint barcodeFormatZXing2Zint[] = { |
| 101 | + {BarcodeFormat::Aztec, BARCODE_AZTEC}, |
| 102 | + {BarcodeFormat::Codabar, BARCODE_CODABAR}, |
| 103 | + {BarcodeFormat::Code39, BARCODE_CODE39}, |
| 104 | + {BarcodeFormat::Code93, BARCODE_CODE93}, |
| 105 | + {BarcodeFormat::Code128, BARCODE_CODE128}, |
| 106 | + {BarcodeFormat::DataBar, BARCODE_DBAR_OMN}, |
| 107 | + {BarcodeFormat::DataBarExpanded, BARCODE_DBAR_EXP}, |
| 108 | + {BarcodeFormat::DataMatrix, BARCODE_DATAMATRIX}, |
| 109 | + {BarcodeFormat::DXFilmEdge, -1}, |
| 110 | + {BarcodeFormat::EAN8, BARCODE_EANX}, |
| 111 | + {BarcodeFormat::EAN13, BARCODE_EANX}, |
| 112 | + {BarcodeFormat::ITF, BARCODE_ITF14}, |
| 113 | + {BarcodeFormat::MaxiCode, BARCODE_MAXICODE}, |
| 114 | + {BarcodeFormat::MicroQRCode, BARCODE_MICROQR}, |
| 115 | + {BarcodeFormat::PDF417, BARCODE_PDF417}, |
| 116 | + {BarcodeFormat::QRCode, BARCODE_QRCODE}, |
| 117 | + {BarcodeFormat::RMQRCode, BARCODE_RMQR}, |
| 118 | + {BarcodeFormat::UPCA, BARCODE_UPCA}, |
| 119 | + {BarcodeFormat::UPCE, BARCODE_UPCE}, |
| 120 | +}; |
| 121 | + |
| 122 | +struct String2Int |
| 123 | +{ |
| 124 | + const char* str; |
| 125 | + int val; |
| 126 | +}; |
| 127 | + |
| 128 | +static int ParseECLevel(int symbology, std::string_view s) |
| 129 | +{ |
| 130 | + constexpr std::string_view EC_LABELS_QR[4] = {"L", "M", "Q", "H"}; |
| 131 | + |
| 132 | + int res = 0; |
| 133 | + if (Contains({BARCODE_QRCODE, BARCODE_MICROQR, BARCODE_RMQR}, symbology)) |
| 134 | + if ((res = IndexOf(EC_LABELS_QR, s) != -1)) |
| 135 | + return res + 1; |
| 136 | + |
| 137 | + if (std::from_chars(s.data(), s.data() + s.size() - (s.back() == '%'), res).ec != std::errc{}) |
| 138 | + throw std::invalid_argument("Invalid ecLevel: '" + std::string(s) + "'"); |
| 139 | + |
| 140 | + auto findClosestECLevel = [](const std::vector<int>& list, int val) { |
| 141 | + int mIdx, mAbs = 100; |
| 142 | + for (int i = 0; i < Size(list); ++i) |
| 143 | + if (int abs = std::abs(val - list[i]); abs < mAbs) { |
| 144 | + mIdx = i; |
| 145 | + mAbs = abs; |
| 146 | + } |
| 147 | + return mIdx + 1; |
| 148 | + }; |
| 149 | + |
| 150 | + if (s.back()=='%'){ |
| 151 | + switch (symbology) { |
| 152 | + case BARCODE_QRCODE: |
| 153 | + case BARCODE_MICROQR: |
| 154 | + case BARCODE_RMQR: |
| 155 | + return findClosestECLevel({20, 37, 55, 65}, res); |
| 156 | + case BARCODE_AZTEC: |
| 157 | + return findClosestECLevel({10, 23, 26, 50}, res); |
| 158 | + case BARCODE_PDF417: |
| 159 | + // TODO: do something sensible with PDF417? |
| 160 | + default: |
| 161 | + return -1; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return res; |
| 166 | +}; |
| 167 | + |
| 168 | +zint_symbol* CreatorOptions::zint() const |
| 169 | +{ |
| 170 | + auto& zint = d->zint; |
| 171 | + |
| 172 | + if (!zint) { |
| 173 | + printf("zint version: %d, sizeof(zint_symbol): %ld\n", ZBarcode_Version(), sizeof(zint_symbol)); |
| 174 | + |
| 175 | + zint.reset(ZBarcode_Create()); |
| 176 | + |
| 177 | + auto i = FindIf(barcodeFormatZXing2Zint, [zxing = format()](auto& v) { return v.zxing == zxing; }); |
| 178 | + if (i == std::end(barcodeFormatZXing2Zint)) |
| 179 | + throw std::invalid_argument("unsupported barcode format: " + ToString(format())); |
| 180 | + zint->symbology = i->zint; |
| 181 | + |
| 182 | + zint->scale = 0.5f; |
| 183 | + |
| 184 | + if (!ecLevel().empty()) |
| 185 | + zint->option_1 = ParseECLevel(zint->symbology, ecLevel()); |
| 186 | + } |
| 187 | + |
| 188 | + return zint.get(); |
| 189 | +} |
| 190 | + |
| 191 | +Barcode CreateBarcode(const void* data, int size, int mode, const CreatorOptions& opts) |
| 192 | +{ |
| 193 | + auto zint = opts.zint(); |
| 194 | + |
| 195 | + zint->input_mode = mode; |
| 196 | + zint->output_options |= OUT_BUFFER_INTERMEDIATE | BARCODE_QUIET_ZONES; |
| 197 | + |
| 198 | + if (mode == DATA_MODE && ZBarcode_Cap(zint->symbology, ZINT_CAP_ECI)) |
| 199 | + zint->eci = static_cast<int>(ECI::Binary); |
| 200 | + |
| 201 | + int err = ZBarcode_Encode_and_Buffer(zint, (uint8_t*)data, size, 0); |
| 202 | + |
| 203 | + if (err || *zint->errtxt) |
| 204 | + printf("Error %d: %s\n", err, zint->errtxt); |
| 205 | + |
| 206 | + printf("symbol size: %dx%d\n", zint->width, zint->rows); |
| 207 | + |
| 208 | + auto buffer = std::vector<uint8_t>(zint->bitmap_width * zint->bitmap_height); |
| 209 | + std::transform(zint->bitmap, zint->bitmap + zint->bitmap_width * zint->bitmap_height, buffer.data(), |
| 210 | + [](unsigned char v) { return (v == '0') * 0xff; }); |
| 211 | + auto bits = BitMatrix(zint->bitmap_width, zint->bitmap_height); |
| 212 | + std::transform(zint->bitmap, zint->bitmap + zint->bitmap_width * zint->bitmap_height, bits.row(0).begin(), |
| 213 | + [](unsigned char v) { return (v == '0') * BitMatrix::SET_V; }); |
| 214 | + |
| 215 | + auto res = ReadBarcode({buffer.data(), zint->bitmap_width, zint->bitmap_height, ImageFormat::Lum}, |
| 216 | + ReaderOptions().setFormats(opts.format()).setIsPure(true).setBinarizer(Binarizer::BoolCast)); |
| 217 | + res.symbol(std::move(bits)); |
| 218 | + res.zint(std::move(opts.d->zint)); |
| 219 | + |
| 220 | + return res; |
| 221 | +} |
| 222 | + |
| 223 | +Barcode CreateBarcodeFromText(std::string_view contents, const CreatorOptions& opts) |
| 224 | +{ |
| 225 | + return CreateBarcode(contents.data(), contents.size(), UNICODE_MODE, opts); |
| 226 | +} |
| 227 | + |
| 228 | +Barcode CreateBarcodeFromText(std::u8string_view contents, const CreatorOptions& opts) |
| 229 | +{ |
| 230 | + return CreateBarcode(contents.data(), contents.size(), UNICODE_MODE, opts); |
| 231 | +} |
| 232 | + |
| 233 | +Barcode CreateBarcodeFromBytes(const void* data, int size, const CreatorOptions& opts) |
| 234 | +{ |
| 235 | + return CreateBarcode(data, size, DATA_MODE, opts); |
| 236 | +} |
| 237 | + |
| 238 | +// Writer ======================================================================== |
| 239 | + |
| 240 | +static void SetCommonWriterOptions(zint_symbol* zint, const WriterOptions& opts) |
| 241 | +{ |
| 242 | + zint->show_hrt = opts.withHRT(); |
| 243 | + |
| 244 | + zint->output_options &= ~OUT_BUFFER_INTERMEDIATE; |
| 245 | + zint->output_options |= opts.withQuietZones() ? BARCODE_QUIET_ZONES: BARCODE_NO_QUIET_ZONES; |
| 246 | + |
| 247 | + if (opts.scale()) |
| 248 | + zint->scale = opts.scale(); |
| 249 | + else if (opts.sizeHint()) { |
| 250 | + int size = std::max(zint->width, zint->rows); |
| 251 | + zint->scale = std::max(1, int(float(opts.sizeHint()) / size)) / 2.f; |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +std::string WriteBarcodeToSVG(const Barcode& barcode, const WriterOptions& opts) |
| 256 | +{ |
| 257 | + auto zs = barcode.zint(); |
| 258 | + |
| 259 | + if (!zs) |
| 260 | + return ToSVG(barcode.symbol()); |
| 261 | + |
| 262 | + SetCommonWriterOptions(zs, opts); |
| 263 | + |
| 264 | + zs->output_options |= BARCODE_MEMORY_FILE;// | EMBED_VECTOR_FONT; |
| 265 | + strcpy(zs->outfile, "null.svg"); |
| 266 | + |
| 267 | + int err = ZBarcode_Print(zs, opts.rotate()); |
| 268 | + if (err || *zs->errtxt) |
| 269 | + printf("Error %d: %s\n", err, zs->errtxt); |
| 270 | + |
| 271 | + return std::string(reinterpret_cast<const char*>(zs->memfile), zs->memfile_size); |
| 272 | +} |
| 273 | + |
| 274 | +Image WriteBarcodeToImage(const Barcode& barcode, const WriterOptions& opts) |
| 275 | +{ |
| 276 | + auto zs = barcode.zint(); |
| 277 | + |
| 278 | + SetCommonWriterOptions(zs, opts); |
| 279 | + |
| 280 | + int err = ZBarcode_Buffer(zs, opts.rotate()); |
| 281 | + if (err || *zs->errtxt) |
| 282 | + printf("Error %d: %s\n", err, zs->errtxt); |
| 283 | + |
| 284 | + printf("symbol size: %dx%d\n", zs->bitmap_width, zs->bitmap_height); |
| 285 | + auto iv = Image(zs->bitmap_width, zs->bitmap_height); |
| 286 | + auto* src = zs->bitmap; |
| 287 | + auto* dst = const_cast<uint8_t*>(iv.data()); |
| 288 | + for(int y = 0; y < iv.height(); ++y) |
| 289 | + for(int x = 0, w = iv.width(); x < w; ++x, src += 3) |
| 290 | + *dst++ = RGBToLum(src[0], src[1], src[2]); |
| 291 | + |
| 292 | + return iv; |
| 293 | +} |
| 294 | + |
| 295 | +} // ZXing |
| 296 | + |
| 297 | + |
| 298 | +#else |
74 | 299 |
|
75 | 300 | #include "BitMatrix.h"
|
76 | 301 | #include "MultiFormatWriter.h"
|
@@ -139,4 +364,6 @@ Image WriteBarcodeToImage(const Barcode& barcode, [[maybe_unused]] const WriterO
|
139 | 364 |
|
140 | 365 | } // namespace ZXing
|
141 | 366 |
|
| 367 | +#endif |
| 368 | + |
142 | 369 | #endif // ZXING_BUILD_EXPERIMENTAL_API
|
0 commit comments