前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小程序摄像头使用技巧

小程序摄像头使用技巧

作者头像
极乐君
发布2018-07-05 15:37:34
1.5K0
发布2018-07-05 15:37:34
举报
文章被收录于专栏:极乐技术社区极乐技术社区

小程序社区版主 x837195936(熊晨沣):做了一个小demo,小程序调用摄像头拍照。拍照时可以选相框,也可以在相机上添加别的饰品、表情等覆盖物。主要是cover-view,cover-image结合camera组件的应用。

index.wxml <view class="container"> <camera device-position='{{devicePosition}}' hidden='{{hideCamara}}' style="width: 100%;height: {{hh}}px;"> <cover-image bindtap='takePhoto' class='ground' src='{{ground}}' mode='aspectFill'></cover-image> <cover-view class='tips' style='opacity: {{tips}};'>点击屏幕任意处拍照</cover-view> <cover-view class='change' bindtap='change'> <cover-image src='../../images/change.png'></cover-image> </cover-view> </camera> <image src='{{photo}}' style='width: 100%;' mode='widthFix'></image> <button class='retoken' bindtap='retoken'>重拍</button> </view>

index.wxss .tips { position: fixed; left: 50%; top: 50%; z-index: +999; color: #fff; font-size: 24rpx; transition: 1s; transform: translate3d(-50%, -50%, 0); -webkit-transform: translate3d(-50%, -50%, 0); -moz-transform: translate3d(-50%, -50%, 0); -o-transform: translate3d(-50%, -50%, 0); } .retoken { width: 280rpx; height: 48rpx; color: #fff; position: fixed; bottom: 40rpx; font-size: 24rpx; line-height: 48rpx; left: 50%; padding: 0; margin: 0; background: #0082df; border-radius: 48rpx; transform: translateX(-50%); -webkit-transform: translateX(-50%); -moz-transform: translateX(-50%); -o-transform: translateX(-50%); } .change { width: 72rpx; height: 72rpx; position: fixed; right: 40px; bottom: 40rpx; border-radius: 50%; background: #fff; } .change cover-image { width: 40rpx; height: 40rpx; margin: 16rpx; }

index.js

//index.js

//获取应用实例

const app = getApp()

const ww = app.globalData.ww;

const hh = app.globalData.hh;

Page({

data: {

ww: ww,

hh: hh,

tips: 0,

hideCamara: false,

devicePosition: 'back',

photo: '',

ground: '../../images/ground.png'

},

onLoad() {

this.ctx = wx.createCameraContext();

this.showTips();

},

takePhoto() {

this.ctx.takePhoto({

quality: 'high',

success: (res) => {

this.setData({

photo: res.tempImagePath,

hideCamara: true

});

}

})

},

retoken() {

this.setData({

photo: 0,

hideCamara: false

})

},

change() {

if (this.data.devicePosition == 'back') {

this.setData({

devicePosition: 'front'

})

} else {

this.setData({

devicePosition: 'back'

})

}

},

showTips() {

this.setData({

tips: 1

});

setTimeout(() => {

this.setData({

tips: 0

});

}, 3000);

}

})

app.js

App({

onLaunch: function () {

this.getSize();

},

getSize() {

var that = this;

wx.getSystemInfo({

success: function (res) {

that.globalData.ww = res.windowWidth;

that.globalData.hh = res.windowHeight;

}

})

},

globalData: {

userInfo: null

}

})

效果图

跳坑

1、camare组件是顶级组件,想要在组件上覆盖其他元素需要在<camare>XXXXXX</carame>内部使用cover-view,cover-image才可以。

2、隐藏camera组件需要在camera组件上绑定属性 hidden="{{hideCamera}}" 使用hideCamera属性来控制组件的隐藏显示,其余的opacity,display等都无效。

极乐技术社区

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-04-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 极乐技术社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档