KItinerary

barcodedocumentprocessorhelper.cpp
1/*
2 SPDX-FileCopyrightText: 2021-2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "barcodedocumentprocessorhelper.h"
7
8#include <KItinerary/ExtractorDocumentNode>
9#include <KItinerary/ExtractorDocumentNodeFactory>
10#include <KItinerary/ExtractorEngine>
11
12using namespace KItinerary;
13
14static bool appendBarcodeResult(const BarcodeDecoder::Result &result, ExtractorDocumentNode &parent, const ExtractorEngine *engine)
15{
16 if (result.contentType == BarcodeDecoder::Result::None) {
17 return false;
18 }
19
20 ExtractorDocumentNode childNode;
21 if (result.contentType & BarcodeDecoder::Result::ByteArray) {
22 childNode = engine->documentNodeFactory()->createNode(result.toByteArray());
23 } else {
24 childNode = engine->documentNodeFactory()->createNode(result.toString().toUtf8());
25 }
26
27 // in case the barcode raw data (string or bytearray) gets detected as a type we handle,
28 // we nevertheless inject a raw data node in between. This is useful in cases where the
29 // content is parsable but that is actually not desired (e.g. JSON content in ticket barcodes).
30
31 if (childNode.isA<QByteArray>() || childNode.isA<QString>()) {
32 parent.appendChild(childNode);
33 return true;
34 }
35
37 if (result.contentType & BarcodeDecoder::Result::ByteArray) {
38 rawNode = engine->documentNodeFactory()->createNode(result.content, u"application/octet-stream");
39 } else {
40 rawNode = engine->documentNodeFactory()->createNode(result.content, u"text/plain");
41 }
42 rawNode.appendChild(childNode);
43 parent.appendChild(rawNode);
44 return true;
45}
46
47bool BarcodeDocumentProcessorHelper::expandNode(const QImage &img, BarcodeDecoder::BarcodeTypes barcodeHints, ExtractorDocumentNode &parent, const ExtractorEngine* engine)
48{
49 if (barcodeHints & BarcodeDecoder::IgnoreAspectRatio) {
50 const auto results = engine->barcodeDecoder()->decodeMulti(img, barcodeHints);
51 bool found = false;
52 for (const auto &res : results) {
53 found = appendBarcodeResult(res, parent, engine) || found; // no short-circuit evaluation!
54 }
55 return found;
56 } else {
57 return appendBarcodeResult(engine->barcodeDecoder()->decode(img, barcodeHints), parent, engine);
58 }
59}
Result decode(const QImage &img, BarcodeTypes hint) const
Decodes a barcode in img based on hint.
std::vector< Result > decodeMulti(const QImage &img, BarcodeTypes hint) const
Decodes multiple barcodes in img based on hint.
ExtractorDocumentNode createNode(const QByteArray &data, QStringView fileName={}, QStringView mimeType={}) const
Create a new document node from data.
A node in the extracted document object tree.
bool isA() const
Checks if the content of this node is of type T.
void appendChild(ExtractorDocumentNode &child)
Add another child node.
Semantic data extraction engine.
const BarcodeDecoder * barcodeDecoder() const
Barcode decoder for use by KItinerary::ExtractorDocumentProcessor.
const ExtractorDocumentNodeFactory * documentNodeFactory() const
Factory for creating new document nodes.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.