ZXingQt5CamReader.qml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2020 Axel Waggershauser
  3. */
  4. // SPDX-License-Identifier: Apache-2.0
  5. import QtQuick 2.12
  6. import QtQuick.Window 2.12
  7. import QtQuick.Controls 2.5
  8. import QtQuick.Layouts 1.12
  9. import QtQuick.Shapes 1.12
  10. import QtMultimedia 5.12
  11. import ZXing 1.0
  12. Window {
  13. visible: true
  14. width: 640
  15. height: 480
  16. title: Qt.application.name
  17. property var nullPoints: [Qt.point(0,0), Qt.point(0,0), Qt.point(0,0), Qt.point(0,0)]
  18. property var points: nullPoints
  19. Timer {
  20. id: resetInfo
  21. interval: 1000
  22. }
  23. BarcodeReader {
  24. id: barcodeReader
  25. formats: (linearSwitch.checked ? (ZXing.LinearCodes) : ZXing.None) | (matrixSwitch.checked ? (ZXing.MatrixCodes) : ZXing.None)
  26. tryRotate: tryRotateSwitch.checked
  27. tryHarder: tryHarderSwitch.checked
  28. tryInvert: tryInvertSwitch.checked
  29. tryDownscale: tryDownscaleSwitch.checked
  30. textMode: ZXing.HRI
  31. // callback with parameter 'barcode', called for every successfully processed frame
  32. onFoundBarcode: (barcode)=> {
  33. points = [barcode.position.topLeft, barcode.position.topRight, barcode.position.bottomRight, barcode.position.bottomLeft]
  34. info.text = qsTr("Format: \t %1 \nText: \t %2 \nType: \t %3 \nTime: \t %4 ms").arg(barcode.formatName).arg(barcode.text).arg(barcode.contentTypeName).arg(runTime)
  35. resetInfo.restart()
  36. // console.log(barcode)
  37. }
  38. // called for every processed frame where no barcode was detected
  39. onFailedRead: ()=> {
  40. points = nullPoints
  41. if (!resetInfo.running)
  42. info.text = "No barcode found (in %1 ms)".arg(runTime)
  43. }
  44. }
  45. Camera {
  46. id: camera
  47. captureMode: Camera.CaptureViewfinder
  48. deviceId: QtMultimedia.availableCameras[camerasComboBox.currentIndex] ? QtMultimedia.availableCameras[camerasComboBox.currentIndex].deviceId : ""
  49. onDeviceIdChanged: {
  50. focus.focusMode = CameraFocus.FocusContinuous
  51. focus.focusPointMode = CameraFocus.FocusPointAuto
  52. }
  53. onError: console.log("camera error:" + errorString)
  54. }
  55. ColumnLayout {
  56. anchors.fill: parent
  57. RowLayout {
  58. Layout.fillWidth: true
  59. Layout.fillHeight: false
  60. visible: QtMultimedia.availableCameras.length > 1
  61. Label {
  62. text: qsTr("Camera: ")
  63. Layout.fillWidth: false
  64. }
  65. ComboBox {
  66. id: camerasComboBox
  67. Layout.fillWidth: true
  68. model: QtMultimedia.availableCameras
  69. textRole: "displayName"
  70. currentIndex: 0
  71. }
  72. }
  73. VideoOutput {
  74. id: videoOutput
  75. Layout.fillHeight: true
  76. Layout.fillWidth: true
  77. filters: [barcodeReader]
  78. source: camera
  79. autoOrientation: true
  80. Shape {
  81. id: polygon
  82. anchors.fill: parent
  83. visible: points.length == 4
  84. ShapePath {
  85. strokeWidth: 3
  86. strokeColor: "red"
  87. strokeStyle: ShapePath.SolidLine
  88. fillColor: "transparent"
  89. //TODO: really? I don't know qml...
  90. startX: videoOutput.mapPointToItem(points[3]).x
  91. startY: videoOutput.mapPointToItem(points[3]).y
  92. PathLine {
  93. x: videoOutput.mapPointToItem(points[0]).x
  94. y: videoOutput.mapPointToItem(points[0]).y
  95. }
  96. PathLine {
  97. x: videoOutput.mapPointToItem(points[1]).x
  98. y: videoOutput.mapPointToItem(points[1]).y
  99. }
  100. PathLine {
  101. x: videoOutput.mapPointToItem(points[2]).x
  102. y: videoOutput.mapPointToItem(points[2]).y
  103. }
  104. PathLine {
  105. x: videoOutput.mapPointToItem(points[3]).x
  106. y: videoOutput.mapPointToItem(points[3]).y
  107. }
  108. }
  109. }
  110. Label {
  111. id: info
  112. color: "white"
  113. padding: 10
  114. background: Rectangle { color: "#80808080" }
  115. }
  116. ColumnLayout {
  117. anchors.right: parent.right
  118. anchors.bottom: parent.bottom
  119. Switch {id: tryRotateSwitch; text: qsTr("Try Rotate"); checked: true }
  120. Switch {id: tryHarderSwitch; text: qsTr("Try Harder"); checked: true }
  121. Switch {id: tryInvertSwitch; text: qsTr("Try Invert"); checked: true }
  122. Switch {id: tryDownscaleSwitch; text: qsTr("Try Downscale"); checked: true }
  123. Switch {id: linearSwitch; text: qsTr("Linear Codes"); checked: true }
  124. Switch {id: matrixSwitch; text: qsTr("Matrix Codes"); checked: true }
  125. }
  126. }
  127. }
  128. }