WIP: Use CameraView for UM camera feeds

CURA-5821

The original implementation that refreshes a QImage seems to cause
memory overflow on MacOSX and Windows. This is a different
implementation. It doesn't cause memory overflow, but it does consume a
lot of CPU power.
This commit is contained in:
Lipu Fei 2018-10-24 15:46:16 +02:00
parent b00ea4719a
commit 9d07409cce
3 changed files with 31 additions and 26 deletions

View File

@ -16,7 +16,6 @@ class NetworkCamera(QObject):
self._image_request = None self._image_request = None
self._image_reply = None self._image_reply = None
self._image = QImage() self._image = QImage()
self._image_id = 0
self._target = target self._target = target
self._started = False self._started = False
@ -33,15 +32,9 @@ class NetworkCamera(QObject):
if restart_required: if restart_required:
self.start() self.start()
@pyqtProperty(QUrl, notify=newImage) @pyqtProperty(QImage, notify=newImage)
def latestImage(self): def latestImage(self):
self._image_id += 1 return self._image
# There is an image provider that is called "camera". In order to ensure that the image qml object, that
# requires a QUrl to function, updates correctly we add an increasing number. This causes to see the QUrl
# as new (instead of relying on cached version and thus forces an update.
temp = "image://camera/" + str(self._image_id)
return QUrl(temp, QUrl.TolerantMode)
@pyqtSlot() @pyqtSlot()
def start(self): def start(self):

View File

@ -10,7 +10,7 @@ Component {
height: maximumHeight; height: maximumHeight;
width: maximumWidth; width: maximumWidth;
Image { Cura.CameraView {
id: cameraImage; id: cameraImage;
anchors { anchors {
horizontalCenter: parent.horizontalCenter; horizontalCenter: parent.horizontalCenter;
@ -21,7 +21,7 @@ Component {
OutputDevice.activePrinter.camera.start(); OutputDevice.activePrinter.camera.start();
} }
} }
height: Math.floor((sourceSize.height === 0 ? 600 * screenScaleFactor : sourceSize.height) * width / sourceSize.width); height: Math.floor((imageHeight === 0 ? 600 * screenScaleFactor : imageHeight) * width / imageWidth);
onVisibleChanged: { onVisibleChanged: {
if (visible) { if (visible) {
if (OutputDevice.activePrinter != null && OutputDevice.activePrinter.camera != null) { if (OutputDevice.activePrinter != null && OutputDevice.activePrinter.camera != null) {
@ -33,14 +33,20 @@ Component {
} }
} }
} }
source: { width: Math.min(imageWidth === 0 ? 800 * screenScaleFactor : imageWidth, maximumWidth);
if (OutputDevice.activePrinter != null && OutputDevice.activePrinter.camera != null && OutputDevice.activePrinter.camera.latestImage) {
return OutputDevice.activePrinter.camera.latestImage;
}
return "";
}
width: Math.min(sourceSize.width === 0 ? 800 * screenScaleFactor : sourceSize.width, maximumWidth);
z: 1; z: 1;
Connections
{
target: OutputDevice.activePrinter.camera;
onNewImage:
{
if (cameraImage.visible) {
cameraImage.image = OutputDevice.activePrinter.camera.latestImage;
cameraImage.update();
}
}
}
} }
} }
} }

View File

@ -5,6 +5,7 @@ import QtQuick 2.2
import QtQuick.Controls 1.4 import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4 import QtQuick.Controls.Styles 1.4
import UM 1.3 as UM import UM 1.3 as UM
import Cura 1.0 as Cura
Item { Item {
property var camera: null; property var camera: null;
@ -33,11 +34,11 @@ Item {
z: 999; z: 999;
} }
Image { Cura.CameraView {
id: cameraImage id: cameraImage
anchors.horizontalCenter: parent.horizontalCenter; anchors.horizontalCenter: parent.horizontalCenter;
anchors.verticalCenter: parent.verticalCenter; anchors.verticalCenter: parent.verticalCenter;
height: Math.round((sourceSize.height === 0 ? 600 * screenScaleFactor : sourceSize.height) * width / sourceSize.width); height: Math.round((imageHeight === 0 ? 600 * screenScaleFactor : imageHeight) * width / imageWidth);
onVisibleChanged: { onVisibleChanged: {
if (visible) { if (visible) {
if (camera != null) { if (camera != null) {
@ -49,13 +50,18 @@ Item {
} }
} }
} }
source: {
if (camera != null && camera.latestImage != null) { Connections
return camera.latestImage; {
target: camera
onNewImage: {
if (cameraImage.visible) {
cameraImage.image = camera.latestImage;
cameraImage.update();
} }
return "";
} }
width: Math.min(sourceSize.width === 0 ? 800 * screenScaleFactor : sourceSize.width, maximumWidth); }
width: Math.min(imageWidth === 0 ? 800 * screenScaleFactor : imageWidth, maximumWidth);
z: 1 z: 1
} }