barcode/QmlBarcode
1 import os, sys
2 
3 import cvb
4 import cvb.ui
5 
6 from result_model import ResultModel
7 from barcode_reader import BarcodeReader
8 
9 from PySide2.QtCore import QObject, QUrl, QAbstractListModel, QModelIndex
10 from PySide2.QtQml import QQmlApplicationEngine
11 from PySide2.QtGui import QGuiApplication, QIcon
12 
13 
14 if __name__ == "__main__":
15 
16  app = QGuiApplication([])
17  app.setOrganizationName('STEMMER IMAGING')
18  app.setOrganizationDomain('https://www.stemmer-imaging.com/')
19  app.setApplicationName('Barcode Python tutorial')
20 
21  # tell Windows the correct AppUserModelID for this process (shows icon in the taskbar)
22  if sys.platform == 'win32':
23  import ctypes
24  myappid = u'stemmerimaging.commonvisionblox.pybarcode.0'
25  ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
26 
27  app.setWindowIcon(QIcon('Tutorial-Python_32x32.png'))
28 
29  # load the device
30  device = cvb.DeviceFactory.open(
31  os.path.join(cvb.install_path(), "drivers", "CVMock.vin"),
32  cvb.AcquisitionStack.Vin)
33  s = device.stream()
34  # setup QML interface objects
35  image_controller = cvb.ui.ImageController()
36  result_model = ResultModel(image_controller)
37  # result_model.Text = "abcd"
38  # result_model.DecodeTime = 3000
39  result_model.Location = [cvb.Point2D(1,2), cvb.Point2D(2,1)]
40 
41  # main classification object
42  barcode_reader = BarcodeReader(device, result_model)
43 
44  # register QML components for an image display
47 
48  engine = QQmlApplicationEngine()
49  context = engine.rootContext()
50 
51  # create a controller object to communicate with QML
52  context.setContextProperty("mainImage", image_controller)
53  # create a Minos result model to communicate with QML for overlays
54  context.setContextProperty("resultModel", result_model)
55  # create a Minos search object to communicate with QML (grab, snap)
56  context.setContextProperty("barcodeReader", barcode_reader)
57 
58  # load main QML file
59  engine.load(os.path.join(os.path.dirname(os.path.abspath(__file__)), "main.qml"))
60 
61  barcode_reader.snap_n_decode()
62 
63  app.exec_()
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")
Convenience method to register this type or a derived type in QML.
Definition: __init__.py:122
Common Vision Blox UI module for Python.
Definition: __init__.py:1
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:193
Multi-purpose 2D vector class.
Definition: __init__.py:3413
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:1327
str install_path()
Directory Common Vision Blox has been installed to.
Definition: __init__.py:7153
1 import os, sys
2 
3 import cvb
4 import cvb.barcode
5 
6 from PySide2 import QtCore
7 from PySide2.QtCore import QObject, Slot
8 
9 from result_model import ResultModel
10 
11 class BarcodeReader(QtCore.QObject):
12  def __init__(self, device, result_model):
13  super().__init__()
14  self._device = device
15  self._stream = device.stream()
16  self._model = result_model
17  self._barcode_config = cvb.barcode.ReaderConfig(cvb.barcode.ReaderInitialization.ReadAll)
18 
19  @Slot()
20  def snap_n_decode(self):
21  self.snap()
22  self.decode()
23 
24  def snap(self):
25  image, wait_status = self._stream.get_timed_snapshot(1000)
26  if (wait_status != cvb.WaitStatus.Ok):
27  return None
28  self._model.refresh(image)
29 
30  def decode(self):
31  result = cvb.barcode.decode(self._barcode_config, self._model.Image.planes[0], self._model.Image.bounds, cvb.barcode.ReadResultDataMatrix)
32  if result.type == cvb.barcode.Symbology.DataMatrix :
33  result.__class__ = cvb.barcode.ReadResultDataMatrix
34  self._model.update(result)
35 
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
Derived from ReadResult and gives specific access to DataMatrix and PharmaCode2D results.
Definition: __init__.py:649
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:1529
1 
2 import cvb
3 import cvb.ui
4 
5 from PySide2.QtCore import QObject, QAbstractListModel, Qt, QModelIndex, Property, Signal, Slot
6 
7 
8 
9 class ResultModel(QAbstractListModel):
10 
11 
12  _Text = ""
13  _DecodeTime = 0
14  _Location = [None] *0
15  _location_role = Qt.UserRole
16 
17  def __init__(self, image_controller, parent=None):
18  super(ResultModel, self).__init__(parent)
19  self._image_controller = image_controller
20 
21 
22  def roleNames(self):
23  roles = dict()
24  roles[ResultModel._location_role] = b"location"
25  return roles
26 
27  def data(self, index, role = Qt.DisplayRole):
28  if not index.isValid():
29  return None
30  pos = self._Location[index.row()]
31  if role == ResultModel._location_role:
32  return cvb.ui.cvb_to_qt_point(pos)
33  else:
34  return None
35 
36  def rowCount(self, parent = QModelIndex()):
37  return len(self._Location)
38 
39  def update(self, barcode_result):
40  self._result = barcode_result
41  self._Text = barcode_result.text
42  self._DecodeTime = barcode_result.decode_time
43  self._Location = barcode_result.location
44 
45  self.layoutChanged.emit()
46  self.notify_text.emit()
47  self.notify_decode_time.emit()
48 
49  @Slot()
50  def refresh(self, image):
51  self.Image = image
52  self._image_controller.refresh(image)
53 
54  def Text(self):
55  return self._Text
56 
57  def DecodeTime(self):
58  return self._DecodeTime
59 
60  notify_text = Signal()
61  notify_decode_time = Signal()
62 
63  Text = Property(str, Text, notify=notify_text)
64  DecodeTime = Property(int, DecodeTime, notify=notify_decode_time)
65 
66 
67 
Common Vision Blox UI module for Python.
Definition: __init__.py:1
PySide2.QtCore.QPointF cvb_to_qt_point(cvb.Point2D point)
Convenience converter for points.
Definition: __init__.py:381
1 import QtQuick 2.3
2 import CvbQuick 1.0 as CvbQuick
3 import QtQuick.Controls 1.3
4 import QtQuick.Layouts 1.2
5 import QtQuick.Dialogs 1.2
6 
7 ApplicationWindow
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 }