C
Qt Quick Ultralite 相机示例
演示如何将特定平台的图像数据与 QML 用户界面集成。
概述
本示例演示了如何处理特定于平台的图像数据。它提供了一个简单的用户界面,用于控制摄像头并通过Qul::SharedImage 访问其帧数据。该示例在受支持的开发板上使用摄像头硬件,而在其他开发板上则采用模拟实现。




目标平台
项目结构
该示例包含一个简单的用户界面(camera.qml)、相机接口定义(camerainterface.h)、针对NXP 平台的相机接口实现,以及相机接口的示例实现(位于platform 目录中)。此外,还包含一张背景图像资源(bg_qt.png)。
CMake 项目文件
除了Qt Quick Ultralite应用程序的CMake配置外,还包含一个platform子目录,其中存放了针对NXP 和dummy平台的相机接口实现。
add_subdirectory(platform)
在 QmlProject 文件中,还注册了相机接口,以便Qt Quick Ultralite 能找到它。
InterfaceFiles {
files: [
"camerainterface.h"
]
}platform目录中的CMakeLists.txt文件会在选择具体实现前先检测当前平台:
# Copyright (C) 2025 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial
message(STATUS "Board manufacturer: ${BOARD_MANUFACTURER_NAME}")
string(REGEX MATCHALL "([^\-]+|[^\-]+$)" PLATFORM_COMPONENTS ${QUL_PLATFORM})
list(GET PLATFORM_COMPONENTS 0 BOARD_NAME)
string(TOLOWER ${BOARD_NAME} BOARD_NAME)
if(NOT ${BOARD_NAME} STREQUAL "mimxrt1170" AND ${BOARD_MANUFACTURER_NAME} STREQUAL "nxp")
add_subdirectory(nxp)
else()
add_subdirectory(dummy)
endif()该示例依赖于 MCUXpresso SDK 提供的一些代码。这些代码并未添加到Qt Quick Ultralite 平台库中,而是直接添加到了示例中。该代码主要处理 CSI 引脚多路复用配置和摄像头 I2C 功能。此外,摄像头支持代码和驱动程序是从 MCUXpresso SDK 中编译而入的。
应用程序用户界面
用户界面在camera.qml 中实现。
应用程序启动时会显示背景图像和“启动相机”按钮。点击该按钮后,相机图像流将开始传输,“拍摄”和“停止”按钮随之启用。 点击“拍摄”按钮将停止摄像头流,并保留当前可见的帧,同时启用“最小化”和“关闭”按钮。“最小化”按钮将当前可见的帧最小化至右上角,“关闭”按钮则恢复到先前状态。点击“停止”按钮将停止摄像头流,并切换回初始状态。
用户界面代码中值得注意的部分是cameraView Image 类型:
Text { id: message color: "red" font.pixelSize: 14 } Image { id: cameraView anchors.right: parent.right anchors.top: parent.top source: CameraInterface.image width: 0 height: 0 Behavior on width { NumberAnimation { duration: 500; easing.type: Easing.OutCubic } } Behavior on height { NumberAnimation { duration: 500; easing.type: Easing.OutCubic } } Component.onCompleted: { if (!CameraInterface.initCamera()) { console.log("Camera initialization failed"); message.text = "Camera initialization failed.\nConnect a camera and/or check the serial console output."; } } SequentialAnimation on opacity { id: shootAnimation PropertyAnimation { property: "opacity"; from: 1.0; to: 0.0; duration: 200} PropertyAnimation { property: "opacity"; from: 0.0; to: 1.0; duration: 200} } }
它将相机接口的image 属性绑定到其source 属性上。它还通过在事件处理程序中调用initCamera() 来初始化相机。
相机接口
相机接口是一个带有Qul::EventQueue 的简单Qul::Singleton 接口。事件队列用于将相机帧从平台异步传递到Qt Quick Ultralite应用程序。
首先,定义一个简单的结构体,用于存储指向相机帧数据的指针:
struct FrameEvent { uint8_t *newFrame; };
该结构体用作Qul::EventQueue 的事件数据类型。
以下是相机接口的定义:
struct CameraInterface : public Qul::Singleton<CameraInterface>, public Qul::EventQueue<FrameEvent> { Qul::Property<Qul::SharedImage> image; bool initCamera(); void startCamera(); void stopCamera(); private: void onEvent(const FrameEvent &frame) QUL_DECL_OVERRIDE; friend struct Qul::Singleton<CameraInterface>; CameraInterface() {} CameraInterface(const CameraInterface &); CameraInterface &operator=(const CameraInterface &); };
首先,相机接口引入了类型为Qul::SharedImage的image属性。该属性包含来自平台相机流的帧数据。随后,声明了用于控制相机的函数。initCamera()函数用于初始化平台摄像头,startCamera()和stopCamera()函数分别用于启动和停止摄像头流。重写了Qul::EventQueue 的onEvent()函数以处理图像更新。最后,禁止了外部构造和复制操作。
相机接口的实现
相机接口的实现位于platform目录中。dummy子目录包含一个占位实现,该实现实现了接口的存根函数。这可作为将示例移植到其他平台的起点:
#include "camerainterface.h" /* Initialize your platform's camera hardware in this function. */ bool CameraInterface::initCamera() { return false; } /* Start camera stream. */ void CameraInterface::startCamera() {} /* Stop camera stream. */ void CameraInterface::stopCamera() {} /* When the camera frame is ready, the image property can be updated in this event handler. */ void CameraInterface::onEvent(const FrameEvent &frameEvent)
nxp子目录包含针对NXP 开发板的实现。该实现使用了NXP 中的相机接收器API。它在initCamera() 中初始化相机接收器,该函数设置了一个回调函数以接收新帧,并将this 指针作为用户数据,以便通过Qul::EventQueue 发布事件。
err = CAMERA_RECEIVER_Init(&cameraReceiver, &cameraConfig, newCameraFrame, this);
此外,空的摄像头帧缓冲区也会提交给摄像头接收器。
for (uint32_t i = 0; i < CAMERA_BUFFER_COUNT; i++) { err = CAMERA_RECEIVER_SubmitEmptyBuffer(&cameraReceiver, (uint32_t) (s_cameraBuffers[i])); RETURN_FALSE_IF(err != kStatus_Success, "Failed to submit camera buffer!"); }
摄像头接收器通过newCameraFrame 回调通知新帧的到来。随后,该回调会发布一个FrameEvent ,其中包含指向新摄像头帧的指针。
me->postEventFromInterrupt(FrameEvent{(uint8_t *) cameraFrameAddr});
FrameEvent 在onEvent() 中进行处理,其中会构建带有自定义清理函数的Qul::Image ,将其封装在Qul::SharedImage 中,并将其设置为image 属性的值。
image.setValue(Qul::SharedImage(newFrame));
当Qt Quick Ultralite引擎销毁Qul::SharedImage 引用时,会调用cleanup 函数。该函数的memory 参数指向通过image 属性传递给Qt Quick Ultralite引擎的缓冲区。该空缓冲区将被重复利用,以便在新的摄像机帧可用时随时访问它们。
status_t err = CAMERA_RECEIVER_SubmitEmptyBuffer(&cameraReceiver, (uint32_t) (memory));
文件:
- camera/CMakeLists.txt
- camera/camera.qml
- camera/camerainterface.h
- camera/mcu_camera.qmlproject
- camera/platform/CMakeLists.txt
- camera/platform/dummy/CMakeLists.txt
- camera/platform/dummy/camerainterface.cpp
- camera/platform/nxp/CMakeLists.txt
- camera/platform/nxp/camerainterface.cpp
- camera/platform/nxp/pin_mux_csi.c
- camera/platform/nxp/pin_mux_csi.h
图片:
另请参阅 Qul::SharedImage 、Qul::Image 、Qul::EventQueue 以及Qul::Singleton 。
在某些 Qt 许可下提供。
了解更多信息。