欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序調(diào)用攝像頭實現(xiàn)拍照功能

 更新時間:2022年07月18日 16:59:08   作者:阿啦ala  
這篇文章主要為大家詳細介紹了微信小程序調(diào)用攝像頭實現(xiàn)拍照功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了微信小程序調(diào)用攝像頭實現(xiàn)拍照的具體代碼,供大家參考,具體內(nèi)容如下

微信小程序開發(fā)文檔

首先,需要用戶授權(quán)攝像頭權(quán)限,這一步是必須的

具體步驟:

1、獲取用戶當(dāng)前授權(quán)狀態(tài),看是否已經(jīng)授權(quán),如果已經(jīng)授權(quán)直接顯示攝像頭
2、如果用戶還沒有授權(quán),則調(diào)起授權(quán)彈框,用戶允許授權(quán)則顯示攝像頭
3、如果用戶不允許,則提示用戶去設(shè)置頁面打開攝像頭權(quán)限

用戶授權(quán)之后,就可以進行拍攝了,微信的camera組件無法顯示為圓形,我這里是用一張圖片遮蓋了

上代碼:

wxml:

<view class='camera'>
? <image src="/images/border.png" mode="widthFix"></image>
? <camera wx:if="{{isAuth}}" device-position="back" flash="off" binderror="error"></camera>
</view>
<button class="takePhoto" type="primary" bindtap="takePhoto">拍照</button>

wxss:

.camera {
? width: 430rpx;
? height: 430rpx;
? border-radius: 50%;
? margin: 20px auto 0;
? position: relative;
}

.camera image {
? position: absolute;
? width: 100%;
? height: 100%;
? z-index: 10;
}

.camera camera {
? width: 428rpx;
? height: 428rpx;
}

button.takePhoto:not([size='mini']) {
? position: fixed;
? bottom: 0;
? left: 0;
? width: 100vw;
? height: 90rpx;
? border-radius: 0;
}

js:

Page({
? data: {
? ? isAuth: false,
? ? src: ''
? },
? onLoad() {
? ? const _this = this
? ? wx.getSetting({
? ? ? success: res => {
? ? ? ? if (res.authSetting['scope.camera']) {
? ? ? ? ? // 用戶已經(jīng)授權(quán)
? ? ? ? ? _this.setData({
? ? ? ? ? ? isAuth: true
? ? ? ? ? })
? ? ? ? } else {
? ? ? ? ? // 用戶還沒有授權(quán),向用戶發(fā)起授權(quán)請求
? ? ? ? ? wx.authorize({
? ? ? ? ? ? scope: 'scope.camera',
? ? ? ? ? ? success() { // 用戶同意授權(quán)
? ? ? ? ? ? ? _this.setData({
? ? ? ? ? ? ? ? isAuth: true
? ? ? ? ? ? ? })
? ? ? ? ? ? },
? ? ? ? ? ? fail() { // 用戶不同意授權(quán)
? ? ? ? ? ? ? _this.openSetting().then(res => {
? ? ? ? ? ? ? ? _this.setData({
? ? ? ? ? ? ? ? ? isAuth: true
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? })
? ? ? ? ? ? }
? ? ? ? ? })
? ? ? ? }
? ? ? },
? ? ? fail: res => {
? ? ? ? console.log('獲取用戶授權(quán)信息失敗')
? ? ? }
? ? })
? },

? // 打開授權(quán)設(shè)置界面
? openSetting() {
? ? const _this = this
? ? let promise = new Promise((resolve, reject) => {
? ? ? wx.showModal({
? ? ? ? title: '授權(quán)',
? ? ? ? content: '請先授權(quán)獲取攝像頭權(quán)限',
? ? ? ? success(res) {
? ? ? ? ? if (res.confirm) {
? ? ? ? ? ? wx.openSetting({
? ? ? ? ? ? ? success(res) {
? ? ? ? ? ? ? ? if (res.authSetting['scope.camera']) { // 用戶打開了授權(quán)開關(guān)
? ? ? ? ? ? ? ? ? resolve(true)
? ? ? ? ? ? ? ? } else { // 用戶沒有打開授權(quán)開關(guān), 繼續(xù)打開設(shè)置頁面
? ? ? ? ? ? ? ? ? _this.openSetting().then(res => {
? ? ? ? ? ? ? ? ? ? resolve(true)
? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? },
? ? ? ? ? ? ? fail(res) {
? ? ? ? ? ? ? ? console.log(res)
? ? ? ? ? ? ? }
? ? ? ? ? ? })
? ? ? ? ? } else if (res.cancel) {
? ? ? ? ? ? _this.openSetting().then(res => {
? ? ? ? ? ? ? resolve(true)
? ? ? ? ? ? })
? ? ? ? ? }
? ? ? ? }
? ? ? })
? ? })
? ? return promise;
? },

? takePhoto() {
? ? const ctx = wx.createCameraContext()
? ? ctx.takePhoto({
? ? ? quality: 'high',
? ? ? success: (res) => {
? ? ? ? this.setData({
? ? ? ? ? src: res.tempImagePath
? ? ? ? })
? ? ? ? wx.previewImage({
? ? ? ? ? current: res.tempImagePath, // 當(dāng)前顯示圖片的http鏈接
? ? ? ? ? urls: [res.tempImagePath] // 需要預(yù)覽的圖片http鏈接列表
? ? ? ? })
? ? ? }
? ? })
? }
})

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論