Skip to content

Commit

Permalink
Defer creation of VideoOuput element until we open the barcode reader
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Aug 12, 2023
1 parent 22f0317 commit d3ddef2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/core/barcodedecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void BarcodeDecoder::decodeImage( const QImage &image )
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
QVideoSink *BarcodeDecoder::videoSink() const
{
return mVideoSink;
return mVideoSink.get();
}

void BarcodeDecoder::setVideoSink( QVideoSink *sink )
Expand Down
3 changes: 2 additions & 1 deletion src/core/barcodedecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QObject>
#include <QThread>
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
#include <QPointer>
#include <QVideoSink>
#endif

Expand Down Expand Up @@ -60,7 +61,7 @@ class BarcodeDecoder : public QObject

#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
QThread *mDecodingThread = nullptr;
QVideoSink *mVideoSink = nullptr;
QPointer<QVideoSink> mVideoSink;

signals:
void videoSinkChanged();
Expand Down
59 changes: 33 additions & 26 deletions src/qml/imports/QFieldControls/+Qt6/BarcodeReader.qml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Popup {

BarcodeDecoder {
id: barcodeDecoder
videoSink: videoOutput.videoSink

onDecodedStringChanged: {
if (decodedString !== '') {
Expand All @@ -44,27 +43,6 @@ Popup {
}
}

Loader {
id: cameraLoader
sourceComponent: cameraComponent
active: barcodeReader.visible

onLoaded: {
item.videoOutput = videoOutput
}
}

Component {
id: cameraComponent

CaptureSession {
camera: Camera {
active: barcodeReader.visible
flashMode: Camera.FlashOff
}
}
}

Page {
width: parent.width
height: parent.height
Expand Down Expand Up @@ -119,11 +97,40 @@ Popup {
radius: 10
clip: true

VideoOutput {
id: videoOutput
Loader {
id: cameraLoader
sourceComponent: cameraComponent
active: barcodeReader.visible
anchors.fill: parent
anchors.margins: 6
fillMode: VideoOutput.PreserveAspectCrop

onLoaded: {
barcodeDecoder.videoSink = item.sink
}
}

Component {
id: cameraComponent

Item {
property alias camera: captureSession.camera
property alias sink: videoOutput.videoSink

CaptureSession {
id: captureSession
camera: Camera {
active: true
flashMode: Camera.FlashOff
}
videoOutput: videoOutput
}

VideoOutput {
id: videoOutput
anchors.fill: parent
anchors.margins: 6
fillMode: VideoOutput.PreserveAspectCrop
}
}
}

Rectangle {
Expand Down

1 comment on commit d3ddef2

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.