#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
程序的大部分源码是照着《qt quick核心编程》书中的摄像头程序写的,但是源程序只是后置摄像头,我添加了调用前置摄像头,一点点改进
首先 FlatButton.qml 自定义有了一个按钮
import QtQuick 2.2
import QtQuick.Controls 1.2
Rectangle
{
id:bkgnd;
implicitWidth: 120;
implicitHeight: 50
color:"transparent";
property alias iconSource:icon.source;
property alias iconWidth:icon.width;
property alias iconHeight:icon.height;
property alias textcolor:btnText.color;
property alias font:btnText.font;
property alias text:btnText.text;
radius:6
property bool hovered: false;
border.color: "#949494";
border.width: hovered ? 2:0;
signal clicked;
Image{
id:icon;
anchors.top: parent.top;
anchors.topMargin: 5;
// anchors.margins: 4;
anchors.horizontalCenter: parent.horizontalCenter;
}
Text
{
id:btnText;
anchors.top: icon.bottom;
anchors.horizontalCenter: icon.horizontalCenter;
anchors.margins: 2;
color: ma.pressed ? "blue" : (parent.hovered ? "#0000a0" : "white");
}
MouseArea
{
id:ma;
anchors.fill:parent;
hoverEnabled:true;
onEntered: {
bkgnd.hovered=true;
}
onExited: {
bkgnd.hovered=false;
}
onClicked: {
if(Qt.platform.os == "android"){
bkgnd.hovered = false;
}
bkgnd.clicked();
}
}
}
main.qml实现了相机程序
import QtQuick 2.4
import QtQuick.Window 2.2
import QtMultimedia 5.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
Window {
visible: true
width: Qt.platform.os === "android"? Screen.width: 640
height: Qt.platform.os === "android"? Screen.height: 480
color:"black";
property var cameras: QtMultimedia.availableCameras
property int currentCamera: 0
FlatButton {
id:qiehuan
anchors.right: parent.right;
anchors.rightMargin: 2;
anchors.top: parent.top;
iconSource: "res/11.png";
width:Qt.platform.os === "android"? Screen.width/8:70;
height: Qt.platform.os === "android"? Screen.width/8:70;
iconHeight: Qt.platform.os === "android"? Screen.width/10:84;
iconWidth: Qt.platform.os === "android"? Screen.width/10:84;
visible: true;
z:1
onClicked: {
currentCamera++
if (currentCamera >= cameras.length)
currentCamera = 0
camera.deviceId = cameras[currentCamera].deviceId
}
}
Camera{
id:camera;
captureMode: Camera.CaptureStillImage;//捕获静态图像
focus{
focusMode:Camera.FocusAuto;//自动变焦
focusPointMode: Camera.FocusPointCenter;//中心焦点
}
imageProcessing{
whiteBalanceMode:CameraImageProcessing.WhiteBalanceAuto;//白平衡自动
}
flash.mode:Camera.FlashAuto;//闪光灯自动
imageCapture{
resolution: Qt.size(1920,1080);
onImageCaptured: {
camera.stop();
photoPreview.visible=true;
actionBar.visible=false;
viewfinder.visible=false;
photoPreview.source=preview;
qiehuan.visible=false;
}
onImageSaved: {
console.log(path);
}
}
onLockStatusChanged:{
switch(lockStatus)
{
case Camera.Locked:
imageCapture.captureToLocation("camera.jpg");
unlock();
break;
case Camera.Searching:
console.log("searching");
break;
case Camera.unlocked:
console.log("unlocked");
break;
}
}
}
VideoOutput{
id:viewfinder;
source: camera;
focus:visible;
anchors.fill:parent;
autoOrientation: true;
}
Image{
id:photoPreview;
anchors.fill:parent;
visible: false;
fillMode: Image.PreserveAspectFit;
FlatButton{
iconSource: "res/ic_launcher_camera.png";
width: Qt.platform.os === "android"? Screen.width/8: 76 ;
height: Qt.platform.os === "android"? Screen.width/8: 76 ;
iconHeight: Qt.platform.os === "android"? Screen.width/10:84;
iconWidth: Qt.platform.os === "android"? Screen.width/10:84;
anchors.left: parent.left;
anchors.bottom: parent.bottom;
anchors.margins: 4;
onClicked: {
camera.start();
actionBar.visible=true;
viewfinder.visible=true;
photoPreview.visible=false;
qiehuan.visible=true;
}
}
}
Image
{
id:actionBar;
source:"res/control_bar.png";
width:Qt.platform.os === "android"? Screen.width: 640;
height:Qt.platform.os === "android"? Screen.height/10: 80;
anchors.bottom: parent.bottom;
anchors.bottomMargin: 8;
anchors.horizontalCenter: parent.horizontalCenter;
visible: true;
z:1;
FlatButton{
id:shutter;
anchors.centerIn: parent;
iconSource: "res/ic_cam_shutter.png";
width:Qt.platform.os === "android"? Screen.width/7:88;
height: Qt.platform.os === "android"? Screen.width/7:88;
iconHeight: Qt.platform.os === "android"? Screen.width/10:84;
iconWidth: Qt.platform.os === "android"? Screen.width/10:84;
onClicked: {
camera.searchAndLock();//计算并锁定图像,触发锁定信号
}
}
FlatButton{
id:zoomout;
anchors.verticalCenter: shutter.verticalCenter;
anchors.right: shutter.left;
anchors.rightMargin: 4;
width:Qt.platform.os === "android"? Screen.width/7:70;
height: Qt.platform.os === "android"? Screen.width/7:70;
iconHeight: Qt.platform.os === "android"? Screen.width/10:84;
iconWidth: Qt.platform.os === "android"? Screen.width/10:84;
text:"缩小";
font.pointSize: 12;
iconSource: "res/ic_zoom_out.png";
onClicked: {
if(camera.digitalZoom>1)
camera.digitalZoom=-0.5;
}
}
FlatButton{
id:zoomin;
anchors.verticalCenter: shutter.verticalCenter;
anchors.right: zoomout.left;
anchors.rightMargin: 4;
width:Qt.platform.os === "android"? Screen.width/7:70;
height: Qt.platform.os === "android"? Screen.width/7:70;
iconHeight: Qt.platform.os === "android"? Screen.width/10:84;
iconWidth: Qt.platform.os === "android"? Screen.width/10:84;
text:"放大";
font.pointSize: 12;
iconSource: "res/ic_zoom_in.png";
onClicked: {
if(camera.digitalZoom<camera.maximumDigitalZoom)
camera.digitalZoom+=0.5;
}
}
FlatButton{
id:currentFlath;
anchors.verticalCenter: shutter.verticalCenter;
anchors.left: shutter.right;
anchors.leftMargin: 4;
width:Qt.platform.os === "android"? Screen.width/7:70;
height: Qt.platform.os === "android"? Screen.width/7:70;
iconHeight: Qt.platform.os === "android"? Screen.width/10:84;
iconWidth: Qt.platform.os === "android"? Screen.width/10:84;
font.pointSize: 12;
property var moderIcon:["res/ic_menu_stat_flash_auto.png","res/ic_menu_stat_flash.png","res/ic_menu_stat_flash_off.png"]
property var moderDesc:["自动","打开","关闭"]
property var flashMode: [
Camera.FlashAuto, Camera.FlashOn, Camera.FlashOff
]
property int mode: 0;
text: moderDesc[mode];
iconSource: moderIcon[mode];
onClicked: {
mode = (mode + 1)%3;
camera.flash.mode = flashMode[mode];
}
}
FlatButton{
id:currentScense;
anchors.verticalCenter: shutter.verticalCenter;
anchors.left: currentFlath.right;
anchors.leftMargin: 4;
width:Qt.platform.os === "android"? Screen.width/7:70;
height: Qt.platform.os === "android"? Screen.width/7:70;
iconHeight: Qt.platform.os === "android"? Screen.width/10:84;
iconWidth: Qt.platform.os === "android"? Screen.width/10:84;
font.pointSize: 12;
property var modeIcon: ["res/ic_menu_stat_auto.png","res/ic_menu_stat_portrait.png","res/ic_menu_stat_landscape.png","res/ic_menu_stat_night.png","res/ic_menu_stat_action.png"]
property var modeDesc:["自动","人物","风景","夜间","运动"]
property var exposureMode:[Camera.ExposureAuto,Camera.ExposurePortrait,Camera.ExposureBeach,Camera.ExposureNight,Camera.ExposureSports]
property int mode:0;
text:modeDesc[mode];
iconSource: modeIcon[mode];
onClicked: {
mode=(mode+1)%5;
camera.exposure.exposureMode=exposureMode[mode];
}
}
}
}
main.cpp函数#include <QGuiApplication>#include <QQmlApplicationEngine>int main(int argc, char *argv[]){QGuiApplication app(argc, argv);QQmlApplicationEngine engine;engine.load(QUrl(QStringLiteral("qrc:/main.qml")));return app.exec();}
在程序中加入了安卓程序启动画面
在andriod重点xml中加入<meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/splash"/>找到res/drawble文件夹中的splash照片是自己设置的开机启动图片
源码下载链接我的qt版本是5.6 http://download.csdn.net/detail/qq_28637193/9610336