QML Camera 摄像头拍照(带滑动条设置焦距)

额…什么都不说了,直接看代码吧

Item{
        id:item
        anchors.fill: parent
        Camera {
            id: camera
            focus {
                focusMode: Camera.FocusAuto;
                focusPointMode: Camera.FocusPointCenter;
            }
            captureMode: Camera.CaptureStillImage;
            imageProcessing {
                whiteBalanceMode: CameraImageProcessing.WhiteBalanceAuto;
            }
            flash.mode: Camera.FlashAuto;

            imageCapture {
                onImageCaptured: {
                    // Show the preview in an Image
                    photoPreview.source = preview
                }
            }
        }


        VideoOutput {
            id:viewfinder
            source: camera
            fillMode: Stretch
            focus : visible // to receive focus and capture key events when visible
            anchors.fill: parent
            autoOrientation: true
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    camera.searchAndLock();
                }
            }
        }

        ZoomControl {
            id:zoomControl
            x : 0
            y : 0
            z:3
            width : 100
            height: parent.height

            currentZoom: camera.digitalZoom
            maximumZoom: Math.min(4.0, camera.maximumDigitalZoom)
            onZoomTo: camera.setDigitalZoom(value)
        }

        TLImageButton{
            id:captureBtn
            width: 60
            height: width
            picNormal:commonParameter.getSkinPath() + "icon_capture_normal.png"
            picPressed: commonParameter.getSkinPath() + "icon_capture_press.png"
            picHover: commonParameter.getSkinPath() + "icon_capture_normal.png"
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 8*initWidth/375.0
            anchors.horizontalCenter: parent.horizontalCenter
            onClicked: {
                camera.imageCapture.capture()
            }
        }
    }
    Image {id: photoPreview}

ZoomControl是自定义的一个滑动条,用于设置拍照焦距的.来看看滑动条代码.

import QtQuick 2.0
import QtMultimedia 5.0

Item {
    id : zoomControl
    property real currentZoom : 1
    property real maximumZoom : 1
    signal zoomTo(real value)

    visible: zoomControl.maximumZoom > 1

    MouseArea {
        id : mouseArea
        anchors.fill: parent

        property real initialZoom : 0
        property real initialPos : 0

        onPressed: {
            initialPos = mouseY
            initialZoom = zoomControl.currentZoom
        }

        onPositionChanged: {
            if (pressed) {
                var target = initialZoom * Math.pow(5, (initialPos-mouseY)/zoomControl.height);
                target = Math.max(1, Math.min(target, zoomControl.maximumZoom))
                zoomControl.zoomTo(target)
            }
        }
    }

    Item {
        id : bar
        x : 16
        y : parent.height/4
        width : 24
        height : parent.height/2

        Rectangle {
            anchors.fill: parent

            smooth: true
            radius: 8
            border.color: "white"
            border.width: 2
            color: "black"
            opacity: 0.3
        }

        Rectangle {
            id: groove
            x : 0
            y : parent.height * (1.0 - (zoomControl.currentZoom-1.0) / (zoomControl.maximumZoom-1.0))
            width: parent.width
            height: parent.height - y
            smooth: true
            radius: 8
            color: "white"
            opacity: 0.5
        }

        Text {
            id: zoomText
            anchors {
                left: bar.right; leftMargin: 16
            }
            y: Math.min(parent.height - height, Math.max(0, groove.y - height / 2))
            text: "x" + Math.round(zoomControl.currentZoom * 100) / 100
            font.bold: true
            color: "white"
            style: Text.Raised; styleColor: "black"
            opacity: 0.85
            font.pixelSize: 18
        }
    }
}

OK 所有代码已奉上.逻辑很简单.

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luoyayun361

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值