Commit ff2a2b71 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳

qrcode: Set camera format to avoid issues with unsupported formats

Synced from Kaidan with slight adaptions
parent c69dc91d
......@@ -31,6 +31,8 @@
#include "QrCodeScannerFilter.h"
#include <QDebug>
#include <QCamera>
#include <QCameraViewfinderSettings>
#include <QtConcurrent/QtConcurrent>
QrCodeScannerFilter::QrCodeScannerFilter(QObject *parent)
......@@ -61,6 +63,18 @@ QVideoFilterRunnable *QrCodeScannerFilter::createFilterRunnable()
return new QrCodeScannerFilterRunnable(this);
}
void QrCodeScannerFilter::setCameraDefaultVideoFormat(QObject *qmlCamera)
{
QCamera *camera = qvariant_cast<QCamera*>(qmlCamera->property("mediaObject"));
if (camera) {
QCameraViewfinderSettings settings = camera->viewfinderSettings();
settings.setPixelFormat(QVideoFrame::Format_RGB24);
camera->setViewfinderSettings(settings);
} else {
qWarning() << "Could not set pixel format of QML camera";
}
}
QrCodeScannerFilterRunnable::QrCodeScannerFilterRunnable(QrCodeScannerFilter *filter)
: QObject(nullptr),
m_filter(filter)
......@@ -97,7 +111,7 @@ void QrCodeScannerFilterRunnable::processVideoFrameProbed(
QrCodeScannerFilter *filter
) {
// Return if the frame is empty.
if (videoFrame.data().length() < 1)
if (videoFrame.data().isEmpty())
return;
// Create an image from the frame.
......@@ -108,7 +122,7 @@ void QrCodeScannerFilterRunnable::processVideoFrameProbed(
// dirty hack: write QVideoFrame::PixelFormat as string to format using QDebug
// QMetaEnum::valueToKey() did not work
QString format;
QDebug(&format) << videoFrame.pixelFormat();
QDebug(&format).nospace() << videoFrame.pixelFormat();
qDebug() << "QrCodeScannerFilterRunnable error: Cannot create image file to process.";
qDebug() << "Maybe it was a format conversion problem.";
......
......@@ -48,7 +48,6 @@ class QrCodeScannerFilter : public QAbstractVideoFilter
friend class QrCodeScannerFilterRunnable;
Q_OBJECT
Q_PROPERTY(QrCodeDecoder* decoder READ decoder)
public:
/**
......@@ -65,6 +64,13 @@ public:
QrCodeDecoder *decoder();
QVideoFilterRunnable *createFilterRunnable() override;
/**
* Sets the video frame format of the QML Camera object to our default format
*
* @param qmlCamera Camera object from QML
*/
Q_INVOKABLE void setCameraDefaultVideoFormat(QObject *qmlCamera);
signals:
/**
* Emitted when the scanning of an image did not succeed, i.e. no valid QR code was found.
......
......@@ -144,4 +144,8 @@ Kirigami.Page {
focusPointMode: Camera.FocusPointCenter
}
}
Component.onCompleted: {
scannerFilter.setCameraDefaultVideoFormat(camera);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment