From 62cd9698263821c113862fbeb3b0830911ec30f1 Mon Sep 17 00:00:00 2001 From: Emanuele Date: Sun, 31 May 2020 00:24:13 +0100 Subject: [PATCH] Add auto detection of orientation --- README.md | 2 +- src/rmview.py | 38 +++++++++++++++++++++++++++++++++++--- src/viewer.py | 11 +++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3b21c62..e218013 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ The supported configuration settings are: // alternatively to key, "password": "****" is supported "timeout": 1 // in seconds }, - "orientation": "portrait", // default: landscape + "orientation": "portrait", // auto for auto-detect, default: landscape "pen_size": 10, // set to 0 to disable "pen_color": "red", "background_color": "black", // default: white diff --git a/src/rmview.py b/src/rmview.py index 48073bd..6e9a552 100644 --- a/src/rmview.py +++ b/src/rmview.py @@ -65,11 +65,17 @@ class rMViewApp(QApplication): self.viewer.menu.addAction(act) self.viewer.setWindowTitle("rMview") self.viewer.show() - if self.config.get('orientation', 'landscape') == 'landscape': + + self.orient = None + orient = self.config.get('orientation', 'landscape') + if orient == 'landscape': self.viewer.rotateCW() self.autoResize(WIDTH / HEIGHT) - else: + elif orient == 'portrait': self.autoResize(HEIGHT / WIDTH) + else: # orient + self.autoResize(HEIGHT / WIDTH) + self.orient = True # bar = QMenuBar() # menu = bar.addMenu('&View') @@ -93,6 +99,25 @@ class rMViewApp(QApplication): self.aboutToQuit.connect(self.joinWorkers) self.requestConnect() + def detectOrientation(self, image): + c = image.pixel + portrait = False + # print(c(48, 47) , c(72, 72) , c(55, 55) , c(64, 65)) + if c(48, 47) == 4278190080 and c(72, 72) == 4278190080 and \ + (c(55, 55) == 4294967295 or c(64, 65) == 4294967295): + if c(61, 1812) != 4278190080 or c(5,5) == 4278190080: + portrait = True + elif c(1356, 47) == 4278190080 and c(1329, 72) == 4278190080 and \ + (c(1348, 54) == 4294967295 or c(1336, 65) == 4294967295): + portrait = True + elif c(5,5) == 4278190080: + portrait = True + if portrait: + self.viewer.portrait() + self.autoResize(HEIGHT / WIDTH) + else: + self.viewer.landscape() + self.autoResize(WIDTH / HEIGHT) def autoResize(self, ratio): dg = self.desktop().availableGeometry(self.viewer) @@ -144,7 +169,7 @@ class rMViewApp(QApplication): self.ssh = ssh self.viewer.setWindowTitle("rMview - " + self.config.get('ssh').get('address')) self.fbworker = FrameBufferWorker(ssh, delay=self.config.get('fetch_frame_delay'), lz4_path=self.config.get('lz4_path_on_remarkable')) - self.fbworker.signals.onNewFrame.connect(lambda image: self.viewer.setImage(image)) + self.fbworker.signals.onNewFrame.connect(self.onNewFrame) self.fbworker.signals.onFatalError.connect(self.frameError) self.threadpool.start(self.fbworker) @@ -162,6 +187,13 @@ class rMViewApp(QApplication): self.penworker.signals.onPenFar.connect(self.pen.hide) + @pyqtSlot(QImage) + def onNewFrame(self, image): + if self.orient: + self.detectOrientation(image) + self.orient = False + self.viewer.setImage(image) + @pyqtSlot(int, int) def movePen(self, x, y): y = 20951 - y diff --git a/src/viewer.py b/src/viewer.py index f52f2f3..8d30d30 100644 --- a/src/viewer.py +++ b/src/viewer.py @@ -151,6 +151,17 @@ class QtImageViewer(QGraphicsView): img = img.transformed(QTransform().rotate(self._rotation)) img.save(fileName) + def landscape(self): + self.resetTransform() + self.rotate(90) + self._rotation = 90 + self.updateViewer() + + def portrait(self): + self.resetTransform() + self._rotation = 0 + self.updateViewer() + def rotateCW(self): self.rotate(90) self._rotation += 90