barcodedecodertest.cpp 6.26 KB
Newer Older
1
/*
2
    SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
3

4
    SPDX-License-Identifier: LGPL-2.0-or-later
5
6
7
*/

#include <config-kitinerary.h>
8
#include <KItinerary/BarcodeDecoder>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <QDebug>
#include <QImage>
#include <QObject>
#include <QTest>

using namespace KItinerary;

class BarcodeDecoderTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void testPDF417_data()
    {
        QTest::addColumn<QString>("fileName");

        QTest::newRow("1bit") << QStringLiteral("pdf417_1bit.png");
        QTest::newRow("8bit") << QStringLiteral("pdf417_8bit.png");
        QTest::newRow("24bit") << QStringLiteral("pdf417_24bit.png");
        QTest::newRow("32bit") << QStringLiteral("pdf417_32bit.png");
29
        QTest::newRow("cropped") << QStringLiteral("pdf417_cropped.png");
30
31
32
        QTest::newRow("rot90") << QStringLiteral("pdf417_rot90.png");
        QTest::newRow("rot180") << QStringLiteral("pdf417_rot180.png");
        QTest::newRow("rot270") << QStringLiteral("pdf417_rot270.png");
33
        QTest::newRow("flipped") << QStringLiteral("pdf417_flipped.png");
34
35
36
37
38
39
40
41
42
    }

    void testPDF417()
    {
        QFETCH(QString, fileName);

        QImage img(QStringLiteral(SOURCE_DIR "/barcodes/") + fileName);
        QVERIFY(!img.isNull());

Volker Krause's avatar
Volker Krause committed
43
        BarcodeDecoder decoder;
44
#ifdef HAVE_ZXING
Volker Krause's avatar
Volker Krause committed
45
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::PDF417), QStringLiteral("PDF417 is a stacked linear barcode symbol format used in a variety of applications, primarily transport, identification cards, and inventory management."));
46
47
48
49
50
51
52
#endif
    }

    void testAztec()
    {
        QImage img(QStringLiteral(SOURCE_DIR "/barcodes/aztec.png"));
        QVERIFY(!img.isNull());
Volker Krause's avatar
Volker Krause committed
53
        BarcodeDecoder decoder;
54
#ifdef HAVE_ZXING
Volker Krause's avatar
Volker Krause committed
55
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::Aztec), QStringLiteral("This is an example Aztec symbol for Wikipedia."));
56
#endif
57
58
59
60
61
        img.load(QStringLiteral(SOURCE_DIR "/barcodes/aztec-partial-quiet-zone.png"));
        QVERIFY(!img.isNull());
#ifdef HAVE_ZXING
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::Aztec), QStringLiteral("KF5::Prison - The KDE barcode generation framework."));
#endif
62
63
64

        img.load(QStringLiteral(SOURCE_DIR "/barcodes/uic918-3star.png"));
        QVERIFY(!img.isNull());
65
#ifdef HAVE_ZXING
Volker Krause's avatar
Volker Krause committed
66
        const auto b = decoder.decodeBinary(img, BarcodeDecoder::Aztec);
67
68
69
70
        QCOMPARE(b.size(), 351);
        QVERIFY(b.startsWith("OTI010080000020"));
#endif
    }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    void testQRCode_data()
    {
            QTest::addColumn<QString>("fileName");
            QTest::addColumn<QString>("result");

            QTest::newRow("1") << QStringLiteral("qrcode1.png") << QStringLiteral("M$K0YGV0G");
            QTest::newRow("2") << QStringLiteral("qrcode2.png") << QStringLiteral("KZEXO4HRE");
    }

    void testQRCode()
    {
        QFETCH(QString, fileName);
        QFETCH(QString, result);

        QImage img(QStringLiteral(SOURCE_DIR "/barcodes/") + fileName);
        QVERIFY(!img.isNull());

Volker Krause's avatar
Volker Krause committed
89
        BarcodeDecoder decoder;
90
#ifdef HAVE_ZXING
Volker Krause's avatar
Volker Krause committed
91
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::QRCode), result);
92
93
94
#endif

    }
Volker Krause's avatar
Volker Krause committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

    void testPlausibilityCheck()
    {
        QVERIFY(!BarcodeDecoder::maybeBarcode(10, 10));
        QVERIFY(BarcodeDecoder::maybeBarcode(100, 100));
        QVERIFY(!BarcodeDecoder::maybeBarcode(100, 100, BarcodeDecoder::PDF417));
        QVERIFY(BarcodeDecoder::maybeBarcode(100, 100, BarcodeDecoder::Aztec));
        QVERIFY(BarcodeDecoder::maybeBarcode(100, 100, BarcodeDecoder::AnySquare));
        QVERIFY(BarcodeDecoder::maybeBarcode(100, 180, BarcodeDecoder::PDF417));
        QVERIFY(BarcodeDecoder::maybeBarcode(180, 100, BarcodeDecoder::PDF417));
        QVERIFY(!BarcodeDecoder::maybeBarcode(180, 100, BarcodeDecoder::AnySquare));
    }

    void testNoCode()
    {
        BarcodeDecoder decoder;
        QVERIFY(!decoder.isBarcode({}));
        QCOMPARE(decoder.decodeBinary({}), QByteArray());
        QCOMPARE(decoder.decodeString({}), QString());

        QImage img(10, 10, QImage::Format_ARGB32);
        img.fill(Qt::black);
        QVERIFY(!decoder.isBarcode(img));
        QCOMPARE(decoder.decodeBinary(img), QByteArray());
        QCOMPARE(decoder.decodeString(img), QString());

        img = QImage(200, 200, QImage::Format_ARGB32);
        img.fill(Qt::red);
        img.fill(Qt::black);
        QVERIFY(!decoder.isBarcode(img));
        QCOMPARE(decoder.decodeBinary(img), QByteArray());
        QCOMPARE(decoder.decodeString(img), QString());
    }

    void testDecoderHints()
    {
        BarcodeDecoder decoder;
        QImage img(QStringLiteral(SOURCE_DIR "/barcodes/aztec.png"));
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::PDF417), QString());
#ifdef HAVE_ZXING
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::AnySquare), QStringLiteral("This is an example Aztec symbol for Wikipedia."));
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::Any), QStringLiteral("This is an example Aztec symbol for Wikipedia."));
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::Aztec), QStringLiteral("This is an example Aztec symbol for Wikipedia."));
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::QRCode), QString());

        decoder.clearCache();
        QVERIFY(decoder.isBarcode(img));
        QVERIFY(!decoder.isBarcode(img, BarcodeDecoder::PDF417));
        QVERIFY(!decoder.isBarcode(img, BarcodeDecoder::QRCode));
        QVERIFY(decoder.isBarcode(img, BarcodeDecoder::Aztec));
        QVERIFY(decoder.isBarcode(img, BarcodeDecoder::AnySquare));
#endif
    }

    void testContentTypeDetection()
    {
        BarcodeDecoder decoder;
        QImage img(QStringLiteral(SOURCE_DIR "/barcodes/aztec.png"));
#ifdef HAVE_ZXING
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::Any), QStringLiteral("This is an example Aztec symbol for Wikipedia."));
        QCOMPARE(decoder.decodeBinary(img, BarcodeDecoder::Any), QByteArray("This is an example Aztec symbol for Wikipedia."));

        img.load(QStringLiteral(SOURCE_DIR "/barcodes/uic918-3star.png"));
        QCOMPARE(decoder.decodeString(img, BarcodeDecoder::Any), QString());
        const auto ba = decoder.decodeBinary(img, BarcodeDecoder::Any);
        QCOMPARE(ba.size(), 351);
        QVERIFY(ba.startsWith("OTI010080000020"));
#endif
    }
164
165
166
167
168
};

QTEST_APPLESS_MAIN(BarcodeDecoderTest)

#include "barcodedecodertest.moc"