QT的功能和Android一样吗,Qt Quick 实现android自带摄像头预览功能

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

文件结构

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值