在查找教程的过程中发现,有的代码功能太多,个人比较喜欢单一功能的代码。

文件结构

16f17dfbe2cafed62f1420c53de00f43.png

quick-camera.pro

QT += quick qml multimedia

CONFIG += c++11

# The following define makes your compiler emit warnings if you use

# any Qt feature that has been marked deprecated (the exact warnings

# depend on your compiler). Refer to the documentation for the

# deprecated API to know how to port your code away from it.

DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.

# In order to do so, uncomment the following line.

# You can also select to disable deprecated APIs only up to a certain version of Qt.

#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \

main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model

QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer

QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.

qnx: target.path = /tmp/$${TARGET}/bin

else: unix:!android: target.path = /opt/$${TARGET}/bin

!isEmpty(target.path): INSTALLS += target

main.cpp

#include

#include

#include

int main(int argc, char *argv[])

{

QGuiApplication app(argc,argv);

QQuickView view;

view.setResizeMode(QQuickView::SizeRootObjectToView);

// Qt.quit() called in embedded .qml by default only emits

// quit() signal, so do this (optionally use Qt.exit()).

QObject::connect(view.engine(), &QQmlEngine::quit,

qApp, &QGuiApplication::quit);

view.setSource(QUrl("qrc:///main.qml"));

view.resize(800, 480);

view.show();

return app.exec();

}

main.qml

import QtQuick 2.0

//import QtQuick.Window 2.12

import QtMultimedia 5.4

//Window {

// visible: true

// width: 640

// height: 480

// title: qsTr("Hello World")

//}

Rectangle {

id : cameraUI

width: 800

height: 480

color: "black"

state: "PhotoCapture"

states: [

State {

name: "PhotoCapture"

StateChangeScript {

script: {

camera.captureMode = Camera.CaptureStillImage

camera.start()

}

}

},

State {

name: "PhotoPreview"

},

State {

name: "VideoCapture"

StateChangeScript {

script: {

camera.captureMode = Camera.CaptureVideo

camera.start()

}

}

},

State {

name: "VideoPreview"

StateChangeScript {

script: {

camera.stop()

}

}

}

]

Camera {

id: camera

captureMode: Camera.CaptureStillImage

imageCapture {

onImageCaptured: {

photoPreview.source = preview

stillControls.previewAvailable = true

cameraUI.state = "PhotoPreview"

}

}

videoRecorder {

resolution: "640x480"

frameRate: 30

}

}

PhotoPreview {

id : photoPreview

anchors.fill : parent

onClosed: cameraUI.state = "PhotoCapture"

visible: cameraUI.state == "PhotoPreview"

focus: visible

}

VideoPreview {

id : videoPreview

anchors.fill : parent

onClosed: cameraUI.state = "VideoCapture"

visible: cameraUI.state == "VideoPreview"

focus: visible

//don't load recorded video if preview is invisible

source: visible ? camera.videoRecorder.actualLocation : ""

}

VideoOutput {

id: viewfinder

visible: cameraUI.state == "PhotoCapture" || cameraUI.state == "VideoCapture"

x: 0

y: 0

width: parent.width

height: parent.height

source: camera

autoOrientation: true

}

}

PhotoPreview.qml

import QtQuick 2.0

import QtMultimedia 5.0

Item {

property alias source : preview.source

signal closed

Image {

id: preview

anchors.fill : parent

fillMode: Image.PreserveAspectFit

smooth: true

}

MouseArea {

anchors.fill: parent

onClicked: {

parent.closed();

}

}

}

VideoPreview.qml

import QtQuick 2.0

import QtMultimedia 5.0

Item {

id: videoPreview

property alias source : player.source

signal closed

MediaPlayer {

id: player

autoPlay: true

//switch back to viewfinder after playback finished

onStatusChanged: {

if (status == MediaPlayer.EndOfMedia)

videoPreview.closed();

}

}

VideoOutput {

source: player

anchors.fill : parent

}

MouseArea {

anchors.fill: parent

onClicked: {

videoPreview.closed();

}

}

}

参考文献:

Qt官方declarative-camera例程

本文地址:https://blog.csdn.net/qq_24815615/article/details/107285324

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐