Prison

videoscannerworker.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: MIT
4*/
5
6#include "config-prison-scanner.h"
7#include "format_p.h"
8#include "imagescanner_p.h"
9#include "scanresult.h"
10#include "scanresult_p.h"
11#include "videoscannerframe_p.h"
12#include "videoscannerworker_p.h"
13
14#include <QDebug>
15#include <QImage>
16#include <QTransform>
17
18#define ZX_USE_UTF8 1
19#include <ZXing/ReadBarcode.h>
20
21using namespace Prison;
22
23VideoScannerWorker::VideoScannerWorker(QObject *parent)
24 : QObject(parent)
25{
26 connect(this, &VideoScannerWorker::scanFrameRequest, this, &VideoScannerWorker::slotScanFrame, Qt::QueuedConnection);
27}
28
29void VideoScannerWorker::slotScanFrame(VideoScannerFrame frame)
30{
31#if ZXING_VERSION < QT_VERSION_CHECK(1, 4, 0)
32 ZXing::Result zxRes(ZXing::DecodeStatus::FormatError);
33#else
34 ZXing::Result zxRes;
35#endif
36 ZXing::DecodeHints hints;
37 hints.setFormats(frame.formats() == Format::NoFormat ? ZXing::BarcodeFormats::all() : Format::toZXing(frame.formats()));
38
39 frame.map();
40 switch (frame.pixelFormat()) {
41 case QVideoFrameFormat::Format_Invalid: // already checked before we get here
42 break;
43 // formats ZXing can consume directly
44 case QVideoFrameFormat::Format_ARGB8888:
45 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
46 case QVideoFrameFormat::Format_XRGB8888:
47 zxRes = ZXing::ReadBarcode({frame.bits(), frame.width(), frame.height(), ZXing::ImageFormat::XRGB, frame.bytesPerLine()}, hints);
48 break;
49 case QVideoFrameFormat::Format_BGRA8888:
50 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
51 case QVideoFrameFormat::Format_BGRX8888:
52 zxRes = ZXing::ReadBarcode({frame.bits(), frame.width(), frame.height(), ZXing::ImageFormat::BGRX, frame.bytesPerLine()}, hints);
53 break;
54 case QVideoFrameFormat::Format_ABGR8888:
55 case QVideoFrameFormat::Format_XBGR8888:
56 zxRes = ZXing::ReadBarcode({frame.bits(), frame.width(), frame.height(), ZXing::ImageFormat::XBGR, frame.bytesPerLine()}, hints);
57 break;
58 case QVideoFrameFormat::Format_RGBA8888:
59 case QVideoFrameFormat::Format_RGBX8888:
60 zxRes = ZXing::ReadBarcode({frame.bits(), frame.width(), frame.height(), ZXing::ImageFormat::RGBX, frame.bytesPerLine()}, hints);
61 break;
62 case QVideoFrameFormat::Format_AYUV:
63 case QVideoFrameFormat::Format_AYUV_Premultiplied:
64 zxRes = ZXing::ReadBarcode({frame.bits() + 1, frame.width(), frame.height(), ZXing::ImageFormat::Lum, frame.bytesPerLine(), 4}, hints);
65 break;
66 case QVideoFrameFormat::Format_YUV420P:
67 case QVideoFrameFormat::Format_YUV422P:
68 case QVideoFrameFormat::Format_YV12:
69 case QVideoFrameFormat::Format_NV12:
70 case QVideoFrameFormat::Format_NV21:
71 case QVideoFrameFormat::Format_IMC1:
72 case QVideoFrameFormat::Format_IMC2:
73 case QVideoFrameFormat::Format_IMC3:
74 case QVideoFrameFormat::Format_IMC4:
75 zxRes = ZXing::ReadBarcode({frame.bits(), frame.width(), frame.height(), ZXing::ImageFormat::Lum, frame.bytesPerLine()}, hints);
76 break;
77 case QVideoFrameFormat::Format_UYVY:
78 zxRes = ZXing::ReadBarcode({frame.bits() + 1, frame.width(), frame.height(), ZXing::ImageFormat::Lum, frame.bytesPerLine(), 2}, hints);
79 break;
80 case QVideoFrameFormat::Format_YUYV:
81 zxRes = ZXing::ReadBarcode({frame.bits(), frame.width(), frame.height(), ZXing::ImageFormat::Lum, frame.bytesPerLine(), 2}, hints);
82 break;
83 case QVideoFrameFormat::Format_Y8:
84 zxRes = ZXing::ReadBarcode({frame.bits(), frame.width(), frame.height(), ZXing::ImageFormat::Lum, frame.bytesPerLine()}, hints);
85 break;
86 case QVideoFrameFormat::Format_Y16:
87 zxRes = ZXing::ReadBarcode({frame.bits() + 1, frame.width(), frame.height(), ZXing::ImageFormat::Lum, frame.bytesPerLine(), 1}, hints);
88 break;
89 case QVideoFrameFormat::Format_P010:
90 case QVideoFrameFormat::Format_P016:
91 case QVideoFrameFormat::Format_YUV420P10:
92 zxRes = ZXing::ReadBarcode({frame.bits(), frame.width(), frame.height(), ZXing::ImageFormat::Lum, frame.bytesPerLine(), 1}, hints);
93 break;
94
95 // formats needing conversion before ZXing can consume them
96 case QVideoFrameFormat::Format_Jpeg:
97 case QVideoFrameFormat::Format_SamplerExternalOES:
98 case QVideoFrameFormat::Format_SamplerRect:
99 frame.convertToImage();
100 zxRes = ImageScanner::readBarcode(frame.image(), frame.formats());
101 break;
102 }
103 frame.unmap();
104
105 // process scan result
106 if (zxRes.isValid()) {
107 QTransform t;
108 if (frame.isVerticallyFlipped()) {
109 t.scale(1, -1);
110 t.translate(0, -frame.height());
111 }
112 Q_EMIT result(ScanResultPrivate::fromZXingResult(zxRes, t));
113 } else {
114 Q_EMIT result(ScanResult());
115 }
116}
117
118void VideoScannerThread::run()
119{
120 exec();
121}
122
123#include "moc_videoscannerworker_p.cpp"
Result of a barcode scan attempt.
Definition scanresult.h:31
Provides classes and methods for generating barcodes.
Definition barcode.h:24
QueuedConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QTransform & scale(qreal sx, qreal sy)
QTransform & translate(qreal dx, qreal dy)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:51:03 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.