KItinerary

barcode.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "barcode.h"
8#include "pdf/pdfbarcodeutil_p.h"
9
10#include <KItinerary/PdfDocument>
11
12#include <QImage>
13#include <QQmlEngine>
14
15using namespace KItinerary;
16
17void JsApi::Barcode::setDecoder(const BarcodeDecoder *decoder)
18{
19 m_decoder = decoder;
20}
21
23{
24 return decodeBarcode(img, BarcodeDecoder::PDF417);
25}
26
28{
29 return decodeBarcode(img, BarcodeDecoder::Aztec);
30}
31
33{
34 if (img.userType() == qMetaTypeId<PdfImage>()) {
35 const auto pdfImg = img.value<PdfImage>();
36 if (PdfBarcodeUtil::maybeBarcode(pdfImg, BarcodeDecoder::Aztec) == BarcodeDecoder::None) {
37 return {};
38 }
39 const auto content = m_decoder->decode(pdfImg.image(), BarcodeDecoder::Aztec).toByteArray();
40 if (content.isEmpty()) {
41 return {};
42 }
43 const auto engine = qjsEngine(this);
44 return QJSValue(QJSManagedValue(QVariant(content), engine));
45 }
46 return {};
47}
48
50{
51 return decodeBarcode(img, BarcodeDecoder::QRCode);
52}
53
55{
56 return decodeBarcode(img, BarcodeDecoder::DataMatrix);
57}
58
60{
61 if (img.userType() == qMetaTypeId<PdfImage>()) {
62 const auto pdfImg = img.value<PdfImage>();
63 return m_decoder->decode(pdfImg.image(), BarcodeDecoder::Any | BarcodeDecoder::IgnoreAspectRatio).toString();
64 }
65 return {};
66}
67
68QString JsApi::Barcode::decodeBarcode(const QVariant &img, BarcodeDecoder::BarcodeTypes hints) const
69{
70 if (img.userType() == qMetaTypeId<PdfImage>()) {
71 const auto pdfImg = img.value<PdfImage>();
72 if (PdfBarcodeUtil::maybeBarcode(pdfImg, hints) == BarcodeDecoder::None) {
73 return {};
74 }
75 return m_decoder->decode(pdfImg.image(), hints).toString();
76 }
77 return {};
78}
79
80#include "moc_barcode.cpp"
Barcode decoding with result caching.
Q_INVOKABLE QJSValue decodeAztecBinary(const QVariant &img) const
Decode a Aztec barcode image containing binary data.
Definition barcode.cpp:32
Q_INVOKABLE QString decodeAztec(const QVariant &img) const
Decode a Aztec barcode image.
Definition barcode.cpp:27
Q_INVOKABLE QString decodePdf417(const QVariant &img) const
Decode a PDF417 barcode image.
Definition barcode.cpp:22
Q_INVOKABLE QString decodeQR(const QVariant &img) const
Decode as QR barcode image.
Definition barcode.cpp:49
Q_INVOKABLE QString decodeDataMatrix(const QVariant &img) const
Decode a DataMatrix barcode image.
Definition barcode.cpp:54
Q_INVOKABLE QString decodeAnyBarcode(const QVariant &img) const
Attempts to decode any barcode found in the given image.
Definition barcode.cpp:59
An image in a PDF document.
Definition pdfimage.h:74
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
int userType() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:00:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.