Prison

pdf417barcode.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: MIT
5 */
6 
7 #include "pdf417barcode.h"
8 #include "zxingutil_p.h"
9 
10 #include <ZXing/BitMatrix.h>
11 #include <ZXing/MultiFormatWriter.h>
12 
13 #include <stdexcept>
14 
15 using namespace Prison;
16 
17 Pdf417Barcode::Pdf417Barcode()
18  : AbstractBarcode(TwoDimensions)
19 {
20 }
21 
23 {
24  Q_UNUSED(size);
25 
26  std::wstring input;
27  const bool isBinary = data().isEmpty();
28  if (!isBinary) {
29  input = data().toStdWString();
30  } else {
31  const auto b = byteArrayData();
32  input.reserve(b.size());
33  // ensure we explicitly copy unsigned bytes here, ie. each byte ends up in the least significant byte of
34  // the std::wstring. If we end up converting to a signed value inbetween, values > 127 end up negative and
35  // will be wrongly represented in the std::wstring
36  for (uint8_t c : b) {
37  input.push_back(c);
38  }
39  }
40 
41  try {
42  ZXing::MultiFormatWriter writer(ZXing::BarcodeFormat::PDF417);
43  // ISO/IEC 15438:2006(E) ยง5.8.3 Quiet Zone
44  writer.setMargin(2);
45  if (isBinary) {
46  writer.setEncoding(ZXing::CharacterSet::BINARY);
47  }
48  // aspect ratio 4 is hard-coded in ZXing
49  const auto matrix = writer.encode(input, 4, 1);
50  return ZXingUtil::toImage(matrix, foregroundColor(), backgroundColor());
51  } catch (const std::invalid_argument &e) {
52  }; // input too large
53  return {};
54 }
QImage paintImage(const QSizeF &size) override
Doing the actual painting of the image.
const QColor & backgroundColor() const
bool isEmpty() const const
std::wstring toStdWString() const const
base class for barcode generators To add your own barcode generator, subclass this class and reimplem...
QByteArray byteArrayData() const
Binary data encoded in this barcode.
const QColor & foregroundColor() const
QString data() const
Textual content encoded in this barcode.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Dec 1 2023 04:09:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.