Prison

datamatrixbarcode.cpp
1 /*
2  SPDX-FileCopyrightText: 2010-2014 Sune Vuorela <[email protected]>
3 
4  SPDX-License-Identifier: MIT
5 */
6 
7 #include "datamatrixbarcode.h"
8 #include <dmtx.h>
9 using namespace Prison;
10 
12  : AbstractBarcode(AbstractBarcode::TwoDimensions)
13 {
14 }
15 DataMatrixBarcode::~DataMatrixBarcode() = default;
16 
18 {
19  Q_UNUSED(size);
20  if (data().size() > 1200) {
21  return QImage();
22  }
23 
24  DmtxEncode *enc = dmtxEncodeCreate();
25  dmtxEncodeSetProp(enc, DmtxPropPixelPacking, DmtxPack32bppRGBX);
26  dmtxEncodeSetProp(enc, DmtxPropModuleSize, 1);
27  dmtxEncodeSetProp(enc, DmtxPropMarginSize, 2);
28 
29  QByteArray trimmedData(data().trimmed().toUtf8());
30  DmtxPassFail result = dmtxEncodeDataMatrix(enc, trimmedData.length(), reinterpret_cast<unsigned char *>(trimmedData.data()));
31  if (result == DmtxFail) {
32  dmtxEncodeDestroy(&enc);
33  return QImage();
34  }
35  Q_ASSERT(enc->image->width == enc->image->height);
36 
37  QImage ret;
38 
40  QImage tmp(enc->image->pxl, enc->image->width, enc->image->height, QImage::Format_ARGB32);
41  // we need to copy, because QImage generated from a char pointer requires the
42  // char pointer to be kept around forever, and manually deleted.
43  ret = tmp.copy();
44  } else {
45  if (enc->image->width > 0) {
46  int size = enc->image->width * enc->image->height * 4;
47  uchar *img = new uchar[size];
48  QByteArray background(4, '\0');
49  background[3] = qAlpha(backgroundColor().rgba());
50  background[2] = qRed(backgroundColor().rgba());
51  background[1] = qGreen(backgroundColor().rgba());
52  background[0] = qBlue(backgroundColor().rgba());
53  QByteArray foreground(4, '\0');
54  foreground[3] = qAlpha(foregroundColor().rgba());
55  foreground[2] = qRed(foregroundColor().rgba());
56  foreground[1] = qGreen(foregroundColor().rgba());
57  foreground[0] = qBlue(foregroundColor().rgba());
58  for (int i = 1; i < size; i += 4) {
59  QByteArray color;
60  if (enc->image->pxl[i] == 0x00) {
61  color = foreground;
62  } else {
63  color = background;
64  }
65  for (int j = 0; j < 4; j++) {
66  img[i - 1 + j] = color[j];
67  }
68  }
69  QImage tmp(img, enc->image->width, enc->image->height, QImage::Format_ARGB32);
70  // we need to copy, because QImage generated from a char pointer requires the
71  // char pointer to be kept around forever, and manually deleted.
72  ret = tmp.copy();
73  delete[] img;
74  }
75  }
76  dmtxEncodeDestroy(&enc);
77  return ret;
78 }
QImage paintImage(const QSizeF &size) override
This is the function doing the actual work in generating the barcode.
const QColor & backgroundColor() const
DataMatrixBarcode()
creates a datamatrixbarcode generator
base class for barcode generators To add your own barcode generator, subclass this class and reimplem...
const QColor & foregroundColor() const
int length() const const
QString data() const
Textual content encoded in this barcode.
QImage copy(const QRect &rectangle) const const
char * data()
qreal width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Dec 5 2023 03:59:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.