Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Qml Webcam Integration
Qt 6.11 is out! See what's new in the release blog

Qml Webcam Integration

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 292 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    melih16
    wrote on last edited by
    #1

    Hello, I want to monitoring my webcam screen on my UI. I am using jetson nano ubuntu pc and I didn't make that. I can select the webcams but webcam's screen does not appear on my UI.
    This is my qml part.
    / Camera view container
    Rectangle {
    id: viewfinder
    anchors.fill: parent
    color: "black"

        MediaDevices {
            id: mediaDevices
        }
        
        CaptureSession {
            id: captureSession
            camera: Camera {
                id: camera
                // Activate the camera when isActive is true
                active: root.isActive
                onErrorOccurred: {
                    console.error("Camera error:", errorString)
                    root.error(errorString)
                }
                onActiveChanged: {
                    console.log("Camera active changed:", active)
                }
            }
            videoOutput: VideoOutput {
                id: videoOutput
                anchors.fill: parent
                fillMode: VideoOutput.PreserveAspectFit
                onSourceRectChanged: {
                    console.log("Video source rect changed:", sourceRect)
                    if (sourceRect.width > 0 && sourceRect.height > 0) {
                        busyIndicator.running = false
                    }
                }
            }
        }
    

    and this is my .py part
    @Slot(str, result=bool)
    def start_camera(self, device_id):
    """Start the selected camera using Qt Multimedia."""
    try:
    print(f"Attempting to start Qt camera with ID: {device_id}")
    self.cleanup_camera()
    return self._start_qt_camera(device_id)
    except Exception as e:
    print(f"Error starting camera: {str(e)}")
    return False

    def _start_qt_camera(self, device_id):
        """Internal method to start the camera via Qt Multimedia."""
        try:
            devices = QMediaDevices()
            available_cameras = devices.videoInputs()
            print(f"Available cameras: {[cam.description() for cam in available_cameras]}")  # Debug log
            camera_info = None
            for cam in available_cameras:
                if cam.id().data().decode() == device_id:
                    camera_info = cam
                    break
            if camera_info:
                print(f"Starting camera: {camera_info.description()}")  # Debug log
                self.current_camera = QCamera(camera_info)
                self.current_camera.start()
                import time
                time.sleep(0.5)
                if self.current_camera.isActive():
                    print("Camera is active.")  # Debug log
                    self.capture_session = QMediaCaptureSession()
                    self.capture_session.setCamera(self.current_camera)
                    self.image_capture = QImageCapture()
                    self.capture_session.setImageCapture(self.image_capture)
                    self.image_capture.imageSaved.connect(
                        lambda id, path: self.imageSaved.emit(path)
                    )
                    return True
                else:
                    print("Camera is not active after starting.")  # Debug log
            else:
                print("No matching camera found.")  # Debug log
            return False
        except Exception as e:
            print(f"Error starting Qt camera: {str(e)}")
            return False!![ss.png](https://ddgobkiprc33d.cloudfront.net/dd84827d-d163-4193-9d34-1985bfa662e0.png) 
    

    Disconnected is not important, it is not related to webcam in screenshoot.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved