Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.57 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #
  3. # Electron Cash - lightweight Bitcoin client
  4. # Copyright (C) 2019 Axel Gembe <derago@gmail.com>
  5. #
  6. # Permission is hereby granted, free of charge, to any person
  7. # obtaining a copy of this software and associated documentation files
  8. # (the "Software"), to deal in the Software without restriction,
  9. # including without limitation the rights to use, copy, modify, merge,
  10. # publish, distribute, sublicense, and/or sell copies of the Software,
  11. # and to permit persons to whom the Software is furnished to do so,
  12. # subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be
  15. # included in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25.  
  26. import sys
  27. import ctypes
  28. from enum import IntEnum
  29.  
  30. from PyQt5.QtMultimedia import QCameraInfo, QCamera, QCameraImageCapture, QVideoFrame, QAbstractVideoBuffer
  31. from PyQt5.QtMultimediaWidgets import QCameraViewfinder
  32. from PyQt5.QtWidgets import QDialog, QVBoxLayout
  33. from PyQt5.QtGui import QImage
  34.  
  35. from electroncash.i18n import _
  36. from electroncash.util import print_error
  37.  
  38. if sys.platform == 'darwin':
  39.     name = 'libzbar.dylib'
  40. elif sys.platform in ('windows', 'win32'):
  41.     name = 'libzbar-0.dll'
  42. else:
  43.     name = 'libzbar.so.0'
  44.  
  45. try:
  46.     libzbar = ctypes.cdll.LoadLibrary(name)
  47.     libzbar.zbar_image_create.restype = ctypes.c_void_p
  48.     libzbar.zbar_image_set_data.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int]
  49.     libzbar.zbar_image_scanner_create.restype = ctypes.c_void_p
  50.     libzbar.zbar_image_scanner_get_results.restype = ctypes.c_void_p
  51.     libzbar.zbar_symbol_set_first_symbol.restype = ctypes.c_void_p
  52.     libzbar.zbar_symbol_get_data.restype = ctypes.c_void_p
  53. except:
  54.     print_error("Failed to load zbar: {}".format(repr(sys.exc_info()[1])))
  55.     libzbar = None
  56.  
  57. FOURCC_Y800 = 0x30303859
  58.  
  59. class ZbarSymbolType(IntEnum):
  60.     EAN2 = 2,
  61.     EAN5 = 5,
  62.     EAN8 = 8,
  63.     UPCE = 9,
  64.     ISBN10 = 10,
  65.     UPCA = 12,
  66.     EAN13 = 13,
  67.     ISBN13 = 14,
  68.     COMPOSITE = 15,
  69.     I25 = 25,
  70.     DATABAR = 34,
  71.     DATABAR_EXP = 35,
  72.     CODABAR = 38,
  73.     CODE39 = 39,
  74.     PDF417 = 57,
  75.     QRCODE = 64,
  76.     SQCODE = 80,
  77.     CODE93 = 93,
  78.     CODE128 = 128
  79.  
  80. class ZbarConfig(IntEnum):
  81.     ENABLE = 0
  82.  
  83. class QrCameraDialog(QDialog):
  84.     def __init__(self, parent):
  85.         if libzbar is None:
  86.             raise RuntimeError("Cannot start QR scanner; zbar not available.")
  87.  
  88.         QDialog.__init__(self, parent=parent)
  89.  
  90.         self.image_capture = None
  91.  
  92.         self.setWindowTitle(_("Scan QR Code"))
  93.  
  94.         vbox = QVBoxLayout()
  95.         self.setLayout(vbox)
  96.  
  97.         self.viewfinder = QCameraViewfinder()
  98.         vbox.addWidget(self.viewfinder)
  99.  
  100.         # Set up zbar
  101.         libzbar.zbar_set_verbosity(100)
  102.  
  103.         self.zbar_image = libzbar.zbar_image_create()
  104.         self.zbar_scanner = libzbar.zbar_image_scanner_create()
  105.  
  106.         # Disable all symbols
  107.         for sym_type in ZbarSymbolType:
  108.             libzbar.zbar_image_scanner_set_config(self.zbar_scanner, int(sym_type), int(ZbarConfig.ENABLE), 0)
  109.  
  110.         # Enable only QR codes
  111.         libzbar.zbar_image_scanner_set_config(self.zbar_scanner, int(ZbarSymbolType.QRCODE), int(ZbarConfig.ENABLE), 1)
  112.  
  113.     def closeEvent(self, e):
  114.         libzbar.zbar_image_scanner_destroy(self.zbar_scanner)
  115.         libzbar.zbar_image_ref(self.zbar_image, -1)
  116.  
  117.     def scan(self, device=''):
  118.         device_info = None
  119.  
  120.         for camera in QCameraInfo.availableCameras():
  121.             if camera.deviceName() == device:
  122.                 device_info = camera
  123.                 break
  124.  
  125.         if not device_info:
  126.             device_info = QCameraInfo.defaultCamera()
  127.  
  128.         if not device_info or device_info.isNull():
  129.             # TODO: Error message
  130.             return ''
  131.  
  132.         camera = QCamera(device_info)
  133.         camera.setViewfinder(self.viewfinder)
  134.         camera.setCaptureMode(QCamera.CaptureStillImage)
  135.  
  136.         self.image_capture = QCameraImageCapture(camera)
  137.         self.image_capture.setCaptureDestination(QCameraImageCapture.CaptureToBuffer)
  138.         self.image_capture.imageAvailable.connect(self.handle_frame)
  139.  
  140.         camera.start()
  141.  
  142.         self.image_capture.capture()
  143.  
  144.         self.exec()
  145.  
  146.         camera.stop()
  147.  
  148.         return ''
  149.  
  150.     def handle_frame(self, frame_id, frame):
  151.         image_format = QVideoFrame.imageFormatFromPixelFormat(frame.pixelFormat())
  152.  
  153.         if not frame.map(QAbstractVideoBuffer.ReadOnly):
  154.             # FIXME: Handle
  155.             return
  156.  
  157.         try:
  158.             if image_format == QImage.Format_Invalid:
  159.                 # This frame was a format that can not be converter to an image pixel format
  160.                 img = QImage.fromData(frame.bits(), frame.mappedBytes())
  161.             else:
  162.                 img = QImage(frame.bits(), frame.width(), frame.height(), image_format).copy()
  163.             img = img.copy()
  164.         finally:
  165.             frame.unmap()
  166.  
  167.         # Convert to Y800 / GREY FourCC (single 8-bit channel)
  168.         img = img.convertToFormat(QImage.Format_Grayscale8)
  169.  
  170.         self.handle_image(frame_id, img)
  171.  
  172.         self.image_capture.capture()
  173.  
  174.     def handle_image(self, frame_id, image: QImage):
  175.         self.current_image = image
  176.         libzbar.zbar_image_set_sequence(self.zbar_image, frame_id)
  177.         libzbar.zbar_image_set_size(self.zbar_image, image.width(), image.height())
  178.         libzbar.zbar_image_set_format(self.zbar_image, FOURCC_Y800)
  179.         libzbar.zbar_image_set_data(self.zbar_image, image.bits().__int__(), image.byteCount())
  180.         libzbar.zbar_image_scanner_recycle_image(self.zbar_scanner, self.zbar_image)
  181.         libzbar.zbar_scan_image(self.zbar_scanner, self.zbar_image)
  182.         result_set = libzbar.zbar_image_scanner_get_results(self.zbar_scanner)
  183.         libzbar.zbar_image_set_symbols(self.zbar_image, result_set)
  184.  
  185.         #symbol = libzbar.zbar_symbol_set_first_symbol(result_set)
  186.  
  187.         detected_symbols = libzbar.zbar_symbol_set_get_size(result_set)
  188.         print_error('detected {} symbol(s) in frame {}'.format(detected_symbols, frame_id))
  189.  
  190.         #print_error('captured {}'.format(frame_id))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement