首页 > 教程
微信小程序调用摄像头实现手机拍照的功能源码
- 2025-04-07
- 1390 ℃
WXML文件代码
<view class="content">
<view class="camera-con">
<camera
wx:if="{{cameraStatus}}"
device-position="back"
flash="off"
binderror="error"
></camera>
<van-image
wx:if="{{!cameraStatus}}"
width="100%"
height="auto"
src="{{src}}"
fit="fill"
/>
</view>
<view class="btn-con">
<van-button round color="#999999" size="small" bindtap="rePhoto"
>重新拍照</van-button
>
<van-button
class="take"
icon="stop-circle-o"
color="rgba(0,0,0,0)"
bindtap="takePhoto"
></van-button>
<van-button round color="#999999" size="small" bindtap="sendPhoto"
>提交图片</van-button
>
</view>
</view>JS文件代码
Page({
data: {
cameraStatus: false,
src: "",
},
onLoad() {
const _this = this;
wx.getSetting({
success: (res) => {
if (res.authSetting["scope.camera"]) {
_this.setData({
cameraStatus: true,
});
} else {
// 用户还没有授权,向用户发起授权请求
wx.authorize({
scope: "scope.camera",
success() {
// 用户同意授权
_this.setData({
cameraStatus: true,
});
},
fail() {
// 用户不同意授权
_this.openSetting().then((res) => {
_this.setData({
cameraStatus: 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;
},
// 拍照
takePhoto() {
const ctx = wx.createCameraContext();
ctx.takePhoto({
quality: "high",
success: (res) => {
this.setData({
src: res.tempImagePath,
cameraStatus: false,
});
},
});
},
// 重新拍照
rePhoto() {
this.setData({
cameraStatus: true,
src: "",
});
},
});WXSS文件代码
.content {
padding: 0;
margin: 0;
position: fixed;
width: 100%;
display: flex;
flex-direction: column;
box-sizing: border-box;
justify-content: space-between;
overflow: hidden;
height: 100vh;
}
image {
margin: 0;
padding: 0;
}
.content .camera-con {
flex: 1;
overflow: hidden;
}
.content .camera-con image,
.content .camera-con camera {
width: 100%;
height: calc(100vh - 148rpx);
}
.content .btn-con {
display: flex;
justify-content: space-between;
align-items: center;
height: 148rpx;
background-color: #333333;
}
.content .btn-con button {
margin: 0 30rpx;
}
.content .btn-con .take {
border: 2rpx solid #ffffff;
border-radius: 50%;
height: 93rpx;
width: 93rpx;
display: flex;
align-items: center;
justify-content: center;
}上一篇:这些搜索方法可以助你事半功倍
相关内容
能自动帮你过人机验证的工具
H5页面移动端软键盘弹出...
微信开小号辅助注册避免...
Vue3 + Vite + Vue ...
去美国必备口语推荐
H5 端唤醒 win10 消息通知
显示器比例影响效率?16:...
重症急性胰腺炎诊治指南
-
2025年8款免费且好用的图片翻译工具
2025-03-04 1704
-
Vue3项目Build后部署在Nginx上F5刷新页面空白或404
2025-04-07 1101
-
宝塔面板如何免费使用专业版插件
2024-07-10 2096
-
你真的知道怎么搜索网盘资源吗
2025-03-10 1708
-
微信的8个实用功能,隐藏技巧
2024-02-19 906
-
可以长按进行倍速播放的本地视频播放器
2025-04-27 3601
-
Fetch的GET、POST简单HTTP请求封装
2025-04-07 979
-
瞬间无损截取合并视频的方法!终于能一秒收藏视频片段
2025-04-27 1106
-
人工智能:探索未知领域的科技之光
2024-04-24 1174
-
jQuery点击生成二维码QRCode复制链接保存到本地
2024-03-06 1080
文章评论 (0)
- 这篇文章还没有收到评论,赶紧来抢沙发吧~


进入有缘空间
点击分享文章