wxml
<view wx:if="{{authorized}}">
<camera devicePosition="{{devicePosition}}" flash="off" hidden="{{isShoot}}" style="width: 100%; height: 100vh;">
<cover-view class="tip-container">
<cover-view class="tips">站在白墙前拍摄效果最佳哦~</cover-view>
<cover-view>
<cover-image class="line" src="../../style/imgs/outline.png"></cover-image>
</cover-view>
<cover-view class="control-pannel">
<!--左 图片选择-->
<cover-view class="control-item">
<cover-image bindtap="chooseAlbumPhoto" class="control-item-img" src="../../style/imgs/album.png"></cover-image>
</cover-view>
<!--中 蓝色按钮-->
<cover-view class="control-item">
<cover-image bindtap="takePhoto" class="control-item-img" id="control-shoot" src="../../style/imgs/shoot.png"></cover-image>
</cover-view>
<!--右 摄像前反-->
<cover-view class="control-item">
<cover-image bindtap="reverseCamera" class="control-item-right" src="../../style/imgs/convert.png"></cover-image>
</cover-view>
</cover-view>
</cover-view>
</camera>
<!--绘制canvas-->
<canvas canvasId="photo" id="photo" style="position: fixed; left: -999px;width:{{canvasWidth}}px;height:{{canvasHeight}}px"></canvas>
</view>
<view wx:else>
<view id="tip-for-fool">尚未打开相机权限,无法正常使用小程序</view>
<button id="btn-for-fool" openType="openSetting">设置权限</button>
</view>
js
var app = getApp()
Page({
data: {
isShoot: !1, //是否拍摄
isProcessed: !1,
devicePosition: "back",
authorized: !0, //是否已经授权
originImage:'',
},
//中 蓝色按钮
takePhoto: function () {
var that = this;
wx.createCameraContext().takePhoto({
success: function (res) {
var a = res.tempImagePath;//图片临时路径
that.setData({
src: a,
isShoot: !0
}), that.checkFileSize(a, function (isCheck) {
isCheck ? (that.data.originImage = a, that.processPhoto()) : wx.getImageInfo({
src: a,
success: function (o) {
var s = o.width,
i = o.height,
n = wx.createCanvasContext("photo", t);
that.setData({
canvasWidth: s,
canvasHeight: i
}), n.drawImage(a), n.draw(), setTimeout(function () {
wx.canvasToTempFilePath({
canvasId: "photo",
success: function (o) {
var a = o.tempFilePath;
wx.getFileInfo({
filePath: a,
success: function (e) {
console.log("file size", e.size);
},
fail: function (e) {
console.log(e);
}
}), that.setData({
src: a
}), that.data.originImage = a, that.processPhoto();
},
fail: function (e) {
console.log(e);
}
}, t);
}, 200);
},
fail: function (e) {
console.log(e);
}
});
});
}
});
},
//检查图片大小 1M
checkFileSize: function (e, t) {
wx.getFileInfo({
filePath: e,
success: function (e) {
t(e.size < 1048576);
}
});
},
//左 图片选择
chooseAlbumPhoto: function () {
wx.vibrateShort({
type: 'light',
})
var that = this;
wx.chooseImage({
count: 1,
sourceType: ["album"],
sizeType: ["compressed"],
success: function (o) {
var a = o.tempFilePaths[0];
that.data.originImage = o.tempFilePaths[0], that.setData({
src: a,
isShoot: !0
}), that.processPhoto();
}
});
},
//右 摄像头前反
reverseCamera: function () {
wx.vibrateShort({
type: 'light',
})
this.setData({
devicePosition: "back" === this.data.devicePosition ? "front" : "back"
});
},
//图片 跳转预览(获取图片,统一处理)
processPhoto: function () {
},
onLoad: function (e) {},
onShow: function () {
var that = this;
this.setData({
isProcessed: !1,
isShoot: !1
})
//判断是否授权摄像权限
wx.getSetting({
success: function (e) {
console.log(e.authSetting);
that.setData({
authorized:e.authSetting["scope.camera"]
})
}
});
},
});
wxss
.line {
max-height: 70.77vh;
max-width: 100%;
}
.tips {
align-self: center;
background: #fca435;
border-radius: 26rpx;
color: #fff;
display: inline-block;
font-size: 30rpx;
height: 52rpx;
line-height: 52rpx;
margin-top: 20rpx;
text-align: center;
width: 442rpx;
}
.tip-container {
flex-direction: column;
height: 100vh;
justify-content: space-around;
}
.control-pannel,.tip-container {
display: flex;
}
.control-pannel {
height: 200rpx;
}
.control-item {
align-self: center;
display: flex;
justify-content: center;
width: 33.33%;
}
.control-item-img {
height: 48rpx;
width: 60rpx;
}
.control-item-right {
height: 54rpx;
width: 54rpx;
}
#control-shoot {
height: 160rpx;
width: 160rpx;
}
#btn-for-fool {
background-color: #fff;
border: 1px solid #4455e4;
border-radius: 5px;
color: #4455e4;
font-size: 30rpx;
height: 50rpx;
line-height: 50rpx;
margin-top: 30rpx;
width: 200rpx;
}
#tip-for-fool {
font-size: 32rpx;
margin-top: 25vh;
text-align: center;
}
其它样式
,album
convert
outline
shoot
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/107055.html