Skip to content

Commit

Permalink
[harbour-barcode] Detect camera resolution at run time
Browse files Browse the repository at this point in the history
if it's not known in advance.
  • Loading branch information
monich committed Oct 21, 2022
1 parent 44e3444 commit 3f73ac1
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 94 deletions.
32 changes: 32 additions & 0 deletions qml/components/ViewFinder.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ VideoOutput {
property real digitalZoom: 1.0
property bool invert

property size supportedWideResolution: Qt.size(0,0) // 4:3
property size supportedNarrowResolution: Qt.size(0,0) // 16:9

readonly property bool cameraActive: camera.cameraState === Camera.ActiveState
readonly property bool flashOn: camera.flash.mode !== Camera.FlashOff
// Not sure why not just camera.orientation but this makes the camera
Expand All @@ -28,6 +31,8 @@ VideoOutput {

// Internal properties
readonly property bool _tapFocusActive: focusTimer.running
readonly property real _ratio_4_3: 4./3.
readonly property real _ratio_16_9: 16./9.
property bool _completed

layer.enabled: invert
Expand Down Expand Up @@ -156,6 +161,7 @@ VideoOutput {
viewFinder.maximumDigitalZoom(maximumDigitalZoom)
digitalZoom = viewFinder.digitalZoom
}
updateSupportedResolutions()
}
onCameraStateChanged: {
if (cameraState === Camera.UnloadedState) {
Expand All @@ -165,6 +171,32 @@ VideoOutput {
}
}
}
function updateSupportedResolutions() {
var list = camera.supportedViewfinderResolutions()
var max_4_3 = Qt.size(0,0)
var max_16_9 = Qt.size(0,0)
for (var i = 0; i < list.length; i++) {
var size = list[i]
var ratio = size.width / size.height
if (ratio === _ratio_4_3) {
if (size.width > max_4_3.width) {
max_4_3 = size
}
} else if (ratio === _ratio_16_9) {
if (size.width > max_16_9.width) {
max_16_9 = size
}
}
}
if (supportedWideResolution.width !== max_4_3.width &&
supportedWideResolution.height !== max_4_3.height) {
supportedWideResolution = Qt.size(max_4_3.width, max_4_3.height)
}
if (supportedNarrowResolution.width !== max_16_9.width &&
supportedNarrowResolution.height !== max_16_9.height) {
supportedNarrowResolution = Qt.size(max_16_9.width, max_16_9.height)
}
}
}

Timer {
Expand Down
36 changes: 29 additions & 7 deletions qml/pages/ScanPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ Page {
if (viewFinder.source.availability === Camera.Available) {
viewFinder.source.start()
}
viewFinderContainer.updateSupportedResolution_4_3(viewFinder.supportedWideResolution)
viewFinderContainer.updateSupportedResolution_16_9(viewFinder.supportedNarrowResolution)
}
}

Expand Down Expand Up @@ -482,15 +484,17 @@ Page {
Rectangle {
id: viewFinderContainer

readonly property real ratio_4_3: 4./3.
readonly property real ratio_16_9: 16./9.
readonly property bool canSwitchResolutions: typeof ViewfinderResolution_4_3 !== "undefined" &&
typeof ViewfinderResolution_16_9 !== "undefined"
readonly property real defaultRatio: AppSettings.narrowRatio
readonly property bool canSwitchResolutions:
AppSettings.wideResolution.width > 0 && AppSettings.wideResolution.height > 0 &&
AppSettings.narrowResolution.width > 0 && AppSettings.narrowResolution.height > 0
readonly property size viewfinderResolution: canSwitchResolutions ?
(AppSettings.wideMode ? ViewfinderResolution_4_3 : ViewfinderResolution_16_9) :
(AppSettings.wideMode ? AppSettings.wideResolution : AppSettings.narrowResolution) :
Qt.size(0,0)
readonly property real ratio: canSwitchResolutions ? (AppSettings.wideMode ? ratio_4_3 : ratio_16_9) :
typeof ViewfinderResolution_4_3 !== "undefined" ? ratio_4_3 : ratio_16_9
readonly property real ratio: canSwitchResolutions ?
(AppSettings.wideMode ? AppSettings.wideRatio : AppSettings.narrowRatio) :
(AppSettings.wideResolution.width > 0 && AppSettings.wideResolution.height > 0) ? AppSettings.wideRatio :
AppSettings.narrowRatio

readonly property int portraitWidth: Math.floor((parent.height/parent.width > ratio) ? parent.width : parent.height/ratio)
readonly property int portraitHeight: Math.floor((parent.height/parent.width > ratio) ? (parent.width * ratio) : parent.height)
Expand Down Expand Up @@ -518,6 +522,24 @@ Page {
function updateViewFinderPosition() {
scanner.viewFinderRect = Qt.rect(x + parent.x, y + parent.y, width, height)
}

function updateSupportedResolution_4_3(res) {
if (res.width > AppSettings.wideResolution.width) {
AppSettings.wideResolution = res
}
}

function updateSupportedResolution_16_9(res) {
if (res.width > AppSettings.narrowResolution.width) {
AppSettings.narrowResolution = res
}
}

Connections {
target: viewFinder
onSupportedWideResolutionChanged: viewFinderContainer.updateSupportedResolution_4_3(viewFinder.supportedWideResolution)
onSupportedNarrowResolutionChanged: viewFinderContainer.updateSupportedResolution_16_9(viewFinder.supportedNarrowResolution)
}
}

Image {
Expand Down
Loading

0 comments on commit 3f73ac1

Please sign in to comment.