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_reply = None
self._image = QImage()
self._image_id = 0
self._target = target
self._started = False
@ -33,15 +32,9 @@ class NetworkCamera(QObject):
if restart_required:
self.start()
@pyqtProperty(QUrl, notify=newImage)
@pyqtProperty(QImage, notify=newImage)
def latestImage(self):
self._image_id += 1
# 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)
return self._image
@pyqtSlot()
def start(self):
@ -116,4 +109,4 @@ class NetworkCamera(QObject):
self._stream_buffer_start_index = -1
self._image.loadFromData(jpg_data)
self.newImage.emit()
self.newImage.emit()

View File

@ -10,7 +10,7 @@ Component {
height: maximumHeight;
width: maximumWidth;
Image {
Cura.CameraView {
id: cameraImage;
anchors {
horizontalCenter: parent.horizontalCenter;
@ -21,7 +21,7 @@ Component {
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: {
if (visible) {
if (OutputDevice.activePrinter != null && OutputDevice.activePrinter.camera != null) {
@ -33,14 +33,20 @@ Component {
}
}
}
source: {
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);
width: Math.min(imageWidth === 0 ? 800 * screenScaleFactor : imageWidth, maximumWidth);
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.Styles 1.4
import UM 1.3 as UM
import Cura 1.0 as Cura
Item {
property var camera: null;
@ -33,11 +34,11 @@ Item {
z: 999;
}
Image {
Cura.CameraView {
id: cameraImage
anchors.horizontalCenter: parent.horizontalCenter;
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: {
if (visible) {
if (camera != null) {
@ -49,13 +50,18 @@ Item {
}
}
}
source: {
if (camera != null && camera.latestImage != null) {
return camera.latestImage;
Connections
{
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
}