CVBpy 15.0
barcode/QmlBarcode
1import os, sys
2
3import cvb
4import cvb.ui
5
6from result_model import ResultModel
7from barcode_reader import BarcodeReader
8
9if sys.version_info >= (3, 11):
10 from PySide6.QtCore import QObject, QUrl, QAbstractListModel, QModelIndex
11 from PySide6.QtQml import QQmlApplicationEngine
12 from PySide6.QtGui import QGuiApplication, QIcon
13else:
14 from PySide2.QtCore import QObject, QUrl, QAbstractListModel, QModelIndex
15 from PySide2.QtQml import QQmlApplicationEngine
16 from PySide2.QtGui import QGuiApplication, QIcon
17
18
19if __name__ == "__main__":
20
21 app = QGuiApplication([])
22 app.setOrganizationName('STEMMER IMAGING')
23 app.setOrganizationDomain('https://www.stemmer-imaging.com/')
24 app.setApplicationName('Barcode Python tutorial')
25
26 # tell Windows the correct AppUserModelID for this process (shows icon in the taskbar)
27 if sys.platform == 'win32':
28 import ctypes
29 myappid = u'stemmerimaging.commonvisionblox.pybarcode.0'
30 ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
31
32 app.setWindowIcon(QIcon('Tutorial-Python_32x32.png'))
33
34 # load the device
36 os.path.join(cvb.install_path(), "drivers", "CVMock.vin"),
37 cvb.AcquisitionStack.Vin)
38 s = device.stream()
39 # setup QML interface objects
40 image_controller = cvb.ui.ImageController()
41 result_model = ResultModel(image_controller)
42 # result_model.Text = "abcd"
43 # result_model.DecodeTime = 3000
44 result_model.Location = [cvb.Point2D(1,2), cvb.Point2D(2,1)]
45
46 # main classification object
47 barcode_reader = BarcodeReader(device, result_model)
48
49 # register QML components for an image display
52
53 engine = QQmlApplicationEngine()
54 context = engine.rootContext()
55
56 # create a controller object to communicate with QML
57 context.setContextProperty("mainImage", image_controller)
58 # create a Minos result model to communicate with QML for overlays
59 context.setContextProperty("resultModel", result_model)
60 # create a Minos search object to communicate with QML (grab, snap)
61 context.setContextProperty("barcodeReader", barcode_reader)
62
63 # load main QML file
64 engine.load(os.path.join(os.path.dirname(os.path.abspath(__file__)), "main.qml"))
65
66 barcode_reader.snap_n_decode()
67
68 app.exec_()
Union[cvb.GenICamDevice, cvb.VinDevice, cvb.EmuDevice, cvb.VideoDevice, cvb.NonStreamingDevice] open(str provider, int acquisition_stack=cvb.AcquisitionStack.PreferVin)
Opens a device with the given provider and acquisition stack.
Definition: __init__.py:1629
Multi-purpose 2D vector class.
Definition: __init__.py:4291
Controller object for the QML image view item.
Definition: __init__.py:14
None register(cls, str uri="CvbQuick", int version_major=1, int version_minor=0, str qml_name="ImageLabel")
Basically just calls qmlRegisterType(...).
Definition: __init__.py:124
None register(cls, str uri="CvbQuick", int version_major=1, int version_minor=0, str qml_name="ImageView")
Convenience method to register this type or a derived type in QML.
Definition: __init__.py:196
Common Vision Blox UI module for Python.
Definition: __init__.py:1
str install_path()
Directory Common Vision Blox has been installed to.
Definition: __init__.py:8318
1import os, sys
2
3import cvb
4import cvb.barcode
5
6if sys.version_info >= (3, 11):
7 from PySide6 import QtCore
8 from PySide6.QtCore import QObject, Slot
9else:
10 from PySide2 import QtCore
11 from PySide2.QtCore import QObject, Slot
12
13from result_model import ResultModel
14
15class BarcodeReader(QtCore.QObject):
16 def __init__(self, device, result_model):
17 super().__init__()
18 self._device = device
19 self._stream = device.stream()
20 self._model = result_model
21 self._barcode_config = cvb.barcode.ReaderConfig(cvb.barcode.ReaderInitialization.ReadAll)
22
23 @Slot()
24 def snap_n_decode(self):
25 self.snap()
26 self.decode()
27
28 def snap(self):
29 image, wait_status = self._stream.get_timed_snapshot(1000)
30 if (wait_status != cvb.WaitStatus.Ok):
31 return None
32 self._model.refresh(image)
33
34 def decode(self):
35 result = cvb.barcode.decode(self._barcode_config, self._model.Image.planes[0], self._model.Image.bounds, cvb.barcode.ReadResultDataMatrix)
36 if result.type == cvb.barcode.Symbology.DataMatrix :
37 result.__class__ = cvb.barcode.ReadResultDataMatrix
38 self._model.update(result)
39
Derived from ReadResult and gives specific access to DataMatrix and PharmaCode2D results.
Definition: __init__.py:649
Contains a map of configurations for all active barcode types.
Definition: __init__.py:769
Common Vision Blox Barcode module for Python.
Definition: __init__.py:1
Union[cvb.barcode.ReadResultUpcE, cvb.barcode.ReadResultSonyCode, cvb.barcode.ReadResultCode128, cvb.barcode.ReadResultCode39Code93, cvb.barcode.ReadResult1D, cvb.barcode.ReadResultPdf417, cvb.barcode.ReadResultDataMatrix, cvb.barcode.ReadResultQr, cvb.barcode.ReadResult] decode(cvb.barcode.ReaderConfig reader_config, cvb.ImagePlane image_plane, Optional[cvb.Rect] aoi, Optional[Union[cvb.barcode.ReadResultUpcE, cvb.barcode.ReadResultSonyCode, cvb.barcode.ReadResultCode128, cvb.barcode.ReadResultCode39Code93, cvb.barcode.ReadResult1D, cvb.barcode.ReadResultPdf417, cvb.barcode.ReadResultDataMatrix, cvb.barcode.ReadResultQr, cvb.barcode.ReadResult]] result_type)
This function initiates the decoding of barcodes and dynamically casts the result.
Definition: __init__.py:1533
1
2import cvb
3import cvb.ui
4import sys
5
6if sys.version_info >= (3, 11):
7 from PySide6.QtCore import QObject, QAbstractListModel, Qt, QModelIndex, Property, Signal, Slot
8else:
9 from PySide2.QtCore import QObject, QAbstractListModel, Qt, QModelIndex, Property, Signal, Slot
10
11
12
13class ResultModel(QAbstractListModel):
14
15
16 _Text = ""
17 _DecodeTime = 0
18 _Location = [None] *0
19 _location_role = Qt.UserRole
20
21 def __init__(self, image_controller, parent=None):
22 super(ResultModel, self).__init__(parent)
23 self._image_controller = image_controller
24
25
26 def roleNames(self):
27 roles = dict()
28 roles[ResultModel._location_role] = b"location"
29 return roles
30
31 def data(self, index, role = Qt.DisplayRole):
32 if not index.isValid():
33 return None
34 pos = self._Location[index.row()]
35 if role == ResultModel._location_role:
36 return cvb.ui.cvb_to_qt_point(pos)
37 else:
38 return None
39
40 def rowCount(self, parent = QModelIndex()):
41 return len(self._Location)
42
43 def update(self, barcode_result):
44 self._result = barcode_result
45 self._Text = barcode_result.text
46 self._DecodeTime = barcode_result.decode_time
47 self._Location = barcode_result.location
48
49 self.layoutChanged.emit()
50 self.notify_text.emit()
51 self.notify_decode_time.emit()
52
53 @Slot()
54 def refresh(self, image):
55 self.Image = image
56 self._image_controller.refresh(image)
57
58 def Text(self):
59 return self._Text
60
61 def DecodeTime(self):
62 return self._DecodeTime
63
64 notify_text = Signal()
65 notify_decode_time = Signal()
66
67 Text = Property(str, Text, notify=notify_text)
68 DecodeTime = Property(int, DecodeTime, notify=notify_decode_time)
69
70
71
PySide2.QtCore.QPointF cvb_to_qt_point(cvb.Point2D point)
Convenience converter for points.
Definition: __init__.py:381
1import QtQuick 2.3
2import CvbQuick 1.0 as CvbQuick
3import QtQuick.Controls 1.3
4import QtQuick.Layouts 1.2
5import QtQuick.Dialogs 1.2
6
7ApplicationWindow
8{
9 id: rootWin
10 visible: true
11 property int margin: 11
12 width: 1080
13 height: 720
14
15 RowLayout
16 {
17 id: mainLayout
18 anchors.fill: parent
19 anchors.margins: margin
20
21 ColumnLayout
22 {
23 CvbQuick.ImageView
24 {
25 id: view
26 image: mainImage
27 Layout.fillWidth: true
28 Layout.fillHeight: true
29
30 Repeater
31 {
32 model : resultModel
33 delegate: CvbQuick.ImageLabel
34 {
35 id: pointLabel
36 imageView : view
37 imageX : location.x
38 imageY : location.y
39
40 Rectangle
41 {
42 width: 15
43 height: width
44 color: "red"
45 border.color: "black"
46 border.width: 1
47 radius: width*0.5
48 }
49 }
50 }
51 }
52
53 Button
54 {
55 text: "Snap"
56 onClicked: barcodeReader.snap_n_decode()
57 }
58 }
59
60 ColumnLayout
61 {
62 TextArea
63 {
64 Layout.minimumWidth: parent.width * 0.3
65 text: resultModel.Text
66 readOnly: true
67 wrapMode: Text.Wrap
68 selectByMouse: true
69 }
70 Text
71 {
72 text: "Processing time: " + resultModel.DecodeTime + " ms"
73 }
74
75 }
76
77 }
78
79
80}