微信小程序拍照,调用相机,模仿相机APP

小程序>微信小程序拍照

实现拍照,切换摄像头,图片预览,拍照动画

先看效果

在这里插入图片描述

wxml

<!--调用相机拍照-->
<view class="page-container">
    <view class='top'>
        <view class='mask1 {{ka? "ka" : "" }}'></view>
        <view class='mask2 {{ka? "ka" : "" }}'></view>
        <camera class="camera" wx:if="{{isAuth}}" device-position="{{devicePosition}}" flash="off" binderror="error" />
    </view>
    <view class="line" />
    <view class='bottom'>
        <view class='item' bindtap="preview">
            <image class='img' src="{{src}}" mode="aspectFill" />
        </view>
        <view class='item'>
            <view class='button' bindtap="takePhoto" hover-class="hover" hover-stop-propagation hover-start-time='0' hover-stay-time='200'> </view>
        </view>
        <view class='item' bindtap="change" mode="widthFix" hover-class="hover" hover-stop-propagation hover-start-time='0' hover-stay-time='200'>
            <image class='shotCut' src="img/shotCut.svg"  />
        </view>
    </view>
</view>

scss

/* pages/camera/camera.wxss */
.page-container {
    height: 100vh;
    width: 100vw;
    background: #F4F4F4;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;

    .top {
        width: 100%;
        flex: 1;
        position: relative;

        .camera {
            width: 100%;
            height: 100%;
        }

        @keyframes ka {
            0% {
                height: 0;
            }

            50% {
                height: 50%;
            }

            100% {
                height: 0%;
            }
        }

        .mask1 {
            position: absolute;
            width: 100%;
            height: 0%;
            background-color: rgba($color: #000000, $alpha: 0.5);
            z-index: 10;

            &.ka {
                animation: ka 0.2s;
            }
        }

        .mask2 {
            position: absolute;
            width: 100%;
            height: 0%;
            background-color: rgba($color: #000000, $alpha: 0.5);
            transform: rotate(180de);
            bottom: 0;
            z-index: 10;

            &.ka {
                animation: ka 0.2s;
            }
        }
    }

    .line {
        background-color: #F4F4F4;
        height: 1rpx;
        width: 100%;
    }

    .bottom {
        display: grid;
        height: 300rpx;
        width: 100%;
        grid-template-columns: repeat(3, 1fr);
        align-items: center; //垂直方向
        background-color: white;

        .item {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100%;

            .img {
                width: 120rpx;
                height: 120rpx;
                border: 1px solid #CDCDCD;
                border-radius: 10rpx;
                overflow: hidden;
            }

            &.hover {
                .shotCut {
                    width: 80rpx;
                    height: 80rpx;
                    background-color: #F0F0F0;
                    border-radius: 10rpx;
                }
            }

            .shotCut {
                width: 80rpx;
                height: 80rpx;
            }

            .button {
                background-color: white;
                width: 120rpx;
                height: 120rpx;
                justify-content: center;
                align-items: center;
                background-color: #CDCDCD;
                border: 6px solid #e0e0e0;
                border-radius: 60rpx;
                box-sizing: border-box;

                &.hover {
                    background-color: white;
                }
            }
        }
    }
}

ts

// pages/camera/camera.ts
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isAuth: false,
        src: '',
        devicePosition: "back",
        ka: false
    },
    //切换摄像头
    change() {
        let position = this.data.devicePosition === 'front' ? "back" : 'front';
        this.setData({ devicePosition: position })
    },
    //预览
    preview() {
        wx.previewImage({
            current: this.data.src, 
            urls: [this.data.src] 
        })
    },
    takePhoto() {
        this.setData({ ka: true })
        setTimeout(() => {
            this.setData({
                ka: false
            })
        }, 200);
        const ctx = wx.createCameraContext()
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
                let tempImagePath = res.tempImagePath
                this.setData({
                    src: tempImagePath
                })
                //保存到本地相册
                // wx.saveFile({
                //     tempFilePath: tempImagePath,
                //     success: function (res) {
                //       //返回保存时的临时路径 res.savedFilePath
                //       const savedFilePath = res.savedFilePath
                //       // 保存到本地相册
                //       wx.saveImageToPhotosAlbum({
                //         filePath: savedFilePath,
                //         success(res) {
                //             console.log("保存成功",res)
                //          },
                //          fail(res) {
                //             console.log("保存失败",res)
                //          }
                //       })
                //     },
                //     //保存失败回调(比如内存不足)
                //     fail: console.log
                //   })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        const _this = this
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.camera']) {
                    // 用户已经授权
                    _this.setData({
                        isAuth: true
                    })
                } else {
                    // 用户还没有授权,向用户发起授权请求
                    wx.authorize({
                        scope: 'scope.camera',
                        success() { // 用户同意授权
                            _this.setData({
                                isAuth: true
                            })
                        },
                        fail() { // 用户不同意授权
                            _this.openSetting().then(res => {
                                _this.setData({
                                    isAuth: true
                                })
                            })
                        }
                    })
                }
            },
            fail: res => {
                console.log('获取用户授权信息失败')
            }
        })
    },
    // 打开授权设置界面
    openSetting() {
        const _this = this
        let promise = new Promise((resolve, reject) => {
            wx.showModal({
                title: '授权',
                content: '请先授权获取摄像头权限',
                success(res) {
                    if (res.confirm) {
                        wx.openSetting({
                            success(res) {
                                if (res.authSetting['scope.camera']) { // 用户打开了授权开关
                                    resolve(true)
                                } else { // 用户没有打开授权开关, 继续打开设置页面
                                    _this.openSetting().then(res => {
                                        resolve(true)
                                    })
                                }
                            },
                            fail(res) {
                                console.log(res)
                            }
                        })
                    } else if (res.cancel) {
                        _this.openSetting().then(res => {
                            resolve(true)
                        })
                    }
                }
            })
        })
        return promise;
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

http://www.niftyadmin.cn/n/442470.html

相关文章

day56_springmvc

今日内容 零、 复习昨日 零、 复习昨日 一、JSON处理【重点】 springmvc支持json数据交互,但是自己本身没有对应jar,使用的是第三方Jackson,只需要导入对应依赖,springmvc即可使用 如果需要换用到FastJson 导入依赖配置文件中指定json转换的类型为FastJson本次课程没有替换,用的…

华为OD机试之Boss分销提成计算(boss的收入)(Java源码)

Boss分销提成计算&#xff08;boss的收入&#xff09; 题目描述 一个XX产品行销总公司&#xff0c;只有一个boss&#xff0c;其有若干一级分销&#xff0c;一级分销又有若干二级分销&#xff0c;每个分销只有唯一的上级分销. 规定&#xff0c;每个月&#xff0c;下级分销需要将…

I/O体系结构和设备驱动程序(一)

I/O体系结构 让信息在CPU、RAM和I/O设备之间流动的数据通路称之为总线&#xff0c;即计算机内的主通信通道。所有计算机都有一条系统总线&#xff08;一种典型的系统总线是PCI总线&#xff09;&#xff0c;连接内部大部分的硬件设备。计算机内不同的总线可以通过“桥”进行连接…

rhce8考试

rhce考试模拟环境准备&#xff1a; cat /etc/rht 确认当前是否为294环境&#xff0c;真实考试有5台被管理节点&#xff0c;借助bastion当做第5台。 将考试所需的文件放到这个目录&#xff0c;/content/courses/rh294/rhel8.0/materials目录&#xff0c;虚拟机看br0网卡信息ifc…

虚拟网络namespace到bridge

前言 容器的网络是一大难点&#xff0c;不管是docker 还是kubernetes 都绕不开计算机网络。以下的介绍主要以计算机网络的namespace 和bridge 两个方面来展开介绍&#xff0c;方便深入理解容器的网络原理。 1.namespace分析 linux 支持六种资源的namespace :mount ns 、UTS ns…

Oracle数据库从入门到精通系列之十八:Oracle进程

Oracle数据库从入门到精通系列之十八&#xff1a;Oracle进程 一、Oracle进程二、服务器进程server process三、后台进程background process四、从属进程(slave process) 一、Oracle进程 Oracle中的每个进程都要执行一个特定的任务(或一组任务)&#xff0c;每个进程都会为自己分…

English Learning - L3 综合练习 7 TED-Living Beyond the Limits 2023.06.14 周三

English Learning - L3 综合练习 7 TED-Living Beyond the Limits 2023.06.14 周三 句 1扩展 go 句 2句 3句 4 - 6句 7-8句 9 - 10句 11扩展 detour 句 12 -13句 14扩展生词 句 15 -16句 17 -18扩展 patchwork 句 18句 19扩展生词 句 20句 21扩展生词 句 22句 23句 24句 25 -26…

docker注意事项和https

docker容器安全注意&#xff1a; 尽量别做的事&#xff1a; 尽量不用 --privileged 运行容器授权容器root用户拥有宿主机的root权限 尽量不在 容器中运行 ssh 服务 尽量不用 --network host 使用 host 网络模式运行容器 尽量要做的事&#xff1a; 尽量使用最小化的镜像 尽量…