|
@ -1,42 +1,54 @@ |
|
|
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QUrl |
|
|
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QUrl, QRect, QPoint |
|
|
from PyQt5.QtGui import QImage |
|
|
from PyQt5.QtGui import QImage,QColor |
|
|
from PyQt5.QtQuick import QQuickImageProvider |
|
|
from PyQt5.QtQuick import QQuickImageProvider |
|
|
|
|
|
|
|
|
from electrum.logging import get_logger |
|
|
from electrum.logging import get_logger |
|
|
|
|
|
from electrum.qrreader import get_qr_reader |
|
|
|
|
|
|
|
|
|
|
|
from electrum.i18n import _ |
|
|
import qrcode |
|
|
import qrcode |
|
|
#from qrcode.image.styledpil import StyledPilImage |
|
|
#from qrcode.image.styledpil import StyledPilImage |
|
|
#from qrcode.image.styles.moduledrawers import * |
|
|
#from qrcode.image.styles.moduledrawers import * |
|
|
|
|
|
|
|
|
from PIL import Image, ImageQt |
|
|
from PIL import Image, ImageQt |
|
|
|
|
|
|
|
|
from ctypes import * |
|
|
from ctypes import * |
|
|
|
|
|
import sys |
|
|
|
|
|
|
|
|
class QEQR(QObject): |
|
|
class QEQR(QObject): |
|
|
def __init__(self, text=None, parent=None): |
|
|
def __init__(self, text=None, parent=None): |
|
|
super().__init__(parent) |
|
|
super().__init__(parent) |
|
|
self._text = text |
|
|
self._text = text |
|
|
|
|
|
self.qrreader = get_qr_reader() |
|
|
|
|
|
if not self.qrreader: |
|
|
|
|
|
raise Exception(_("The platform QR detection library is not available.")) |
|
|
|
|
|
|
|
|
_logger = get_logger(__name__) |
|
|
_logger = get_logger(__name__) |
|
|
|
|
|
|
|
|
scanReadyChanged = pyqtSignal() |
|
|
busyChanged = pyqtSignal() |
|
|
|
|
|
dataChanged = pyqtSignal() |
|
|
imageChanged = pyqtSignal() |
|
|
imageChanged = pyqtSignal() |
|
|
|
|
|
|
|
|
_scanReady = True |
|
|
_busy = False |
|
|
_image = None |
|
|
_image = None |
|
|
|
|
|
|
|
|
@pyqtSlot('QImage') |
|
|
@pyqtSlot('QImage') |
|
|
def scanImage(self, image=None): |
|
|
def scanImage(self, image=None): |
|
|
if not self._scanReady: |
|
|
if self._busy: |
|
|
self._logger.warning("Already processing an image. Check 'ready' property before calling scanImage") |
|
|
self._logger.warning("Already processing an image. Check 'busy' property before calling scanImage") |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
if image == None: |
|
|
|
|
|
self._logger.warning("No image to decode") |
|
|
return |
|
|
return |
|
|
self._scanReady = False |
|
|
|
|
|
self.scanReadyChanged.emit() |
|
|
|
|
|
|
|
|
|
|
|
pilimage = self.convertToPILImage(image) |
|
|
self._busy = True |
|
|
self.parseQR(pilimage) |
|
|
self.busyChanged.emit() |
|
|
|
|
|
|
|
|
self._scanReady = True |
|
|
self.logImageStats(image) |
|
|
|
|
|
self._parseQR(image) |
|
|
|
|
|
|
|
|
|
|
|
self._busy = False |
|
|
|
|
|
self.busyChanged.emit() |
|
|
|
|
|
|
|
|
def logImageStats(self, image): |
|
|
def logImageStats(self, image): |
|
|
self._logger.info('width: ' + str(image.width())) |
|
|
self._logger.info('width: ' + str(image.width())) |
|
@ -44,33 +56,63 @@ class QEQR(QObject): |
|
|
self._logger.info('depth: ' + str(image.depth())) |
|
|
self._logger.info('depth: ' + str(image.depth())) |
|
|
self._logger.info('format: ' + str(image.format())) |
|
|
self._logger.info('format: ' + str(image.format())) |
|
|
|
|
|
|
|
|
def convertToPILImage(self, image): # -> Image: |
|
|
def _parseQR(self, image): |
|
|
self.logImageStats(image) |
|
|
self.w = image.width() |
|
|
|
|
|
self.h = image.height() |
|
|
rawimage = image.constBits() |
|
|
img_crop_rect = self._get_crop(image, 360) |
|
|
# assumption: pixels are 32 bits ARGB |
|
|
frame_cropped = image.copy(img_crop_rect) |
|
|
numbytes = image.width() * image.height() * 4 |
|
|
|
|
|
|
|
|
# Convert to Y800 / GREY FourCC (single 8-bit channel) |
|
|
self._logger.info(type(rawimage)) |
|
|
# This creates a copy, so we don't need to keep the frame around anymore |
|
|
buf = bytearray(numbytes) |
|
|
frame_y800 = frame_cropped.convertToFormat(QImage.Format_Grayscale8) |
|
|
c_buf = (c_byte * numbytes).from_buffer(buf) |
|
|
|
|
|
memmove(c_buf, c_void_p(rawimage.__int__()), numbytes) |
|
|
self.frame_id = 0 |
|
|
buf2 = bytes(buf) |
|
|
# Read the QR codes from the frame |
|
|
|
|
|
self.qrreader_res = self.qrreader.read_qr_code( |
|
|
return Image.frombytes('RGBA', (image.width(), image.height()), buf2, 'raw') |
|
|
frame_y800.constBits().__int__(), frame_y800.byteCount(), |
|
|
|
|
|
frame_y800.bytesPerLine(), |
|
|
def parseQR(self, image): |
|
|
frame_y800.width(), |
|
|
# TODO |
|
|
frame_y800.height(), self.frame_id |
|
|
pass |
|
|
) |
|
|
|
|
|
|
|
|
@pyqtProperty(bool, notify=scanReadyChanged) |
|
|
if len(self.qrreader_res) > 0: |
|
|
def scanReady(self): |
|
|
result = self.qrreader_res[0] |
|
|
return self._scanReady |
|
|
self._data = result |
|
|
|
|
|
self.dataChanged.emit() |
|
|
|
|
|
|
|
|
|
|
|
def _get_crop(self, image: QImage, scan_size: int) -> QRect: |
|
|
|
|
|
""" |
|
|
|
|
|
Returns a QRect that is scan_size x scan_size in the middle of the resolution |
|
|
|
|
|
""" |
|
|
|
|
|
self.scan_pos_x = (image.width() - scan_size) // 2 |
|
|
|
|
|
self.scan_pos_y = (image.height() - scan_size) // 2 |
|
|
|
|
|
return QRect(self.scan_pos_x, self.scan_pos_y, scan_size, scan_size) |
|
|
|
|
|
|
|
|
|
|
|
@pyqtProperty(bool, notify=busyChanged) |
|
|
|
|
|
def busy(self): |
|
|
|
|
|
return self._busy |
|
|
|
|
|
|
|
|
@pyqtProperty('QImage', notify=imageChanged) |
|
|
@pyqtProperty('QImage', notify=imageChanged) |
|
|
def image(self): |
|
|
def image(self): |
|
|
return self._image |
|
|
return self._image |
|
|
|
|
|
|
|
|
|
|
|
@pyqtProperty(str, notify=dataChanged) |
|
|
|
|
|
def data(self): |
|
|
|
|
|
return self._data.data |
|
|
|
|
|
|
|
|
|
|
|
@pyqtProperty('QPoint', notify=dataChanged) |
|
|
|
|
|
def center(self): |
|
|
|
|
|
(x,y) = self._data.center |
|
|
|
|
|
return QPoint(x+self.scan_pos_x, y+self.scan_pos_y) |
|
|
|
|
|
|
|
|
|
|
|
@pyqtProperty('QVariant', notify=dataChanged) |
|
|
|
|
|
def points(self): |
|
|
|
|
|
result = [] |
|
|
|
|
|
for item in self._data.points: |
|
|
|
|
|
(x,y) = item |
|
|
|
|
|
result.append(QPoint(x+self.scan_pos_x, y+self.scan_pos_y)) |
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
class QEQRImageProvider(QQuickImageProvider): |
|
|
class QEQRImageProvider(QQuickImageProvider): |
|
|
def __init__(self, parent=None): |
|
|
def __init__(self, parent=None): |
|
|
super().__init__(QQuickImageProvider.Image) |
|
|
super().__init__(QQuickImageProvider.Image) |
|
|