一、调取相机闪光灯功能的实现方法
目前部分插件(如uview/uvui)通过调用微信小程序的wx.chooseMedia
API实现相机功能,但该接口不支持闪光灯控制。如需实现闪光灯功能,需使用原生相机API自行开发组件。
二、隐私权限申请
在小程序开发中调用相机功能,需在app.json
中添加权限声明:
三、核心实现代码
通过uni.createCameraContext()
创建相机实例,控制闪光灯开关:
<!--
* @组件由其他博主项目简化过来,如需要源码,请访问:https://blog.csdn.net/weixin_42960907/article/details/136563330?ops_request_misc=%257B%2522request%255Fid%2522%253A%252227e66ebc705b5bc88791613b5343e476%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=27e66ebc705b5bc88791613b5343e476&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-136563330-null-null.142^v102^pc_search_result_base7&utm_term=uniapp%E5%9C%A8%E4%BD%BF%E7%94%A8uni.chooseImage%E8%B0%83%E8%B5%B7%E6%83%B3%E7%9B%B8%E6%9C%BA%E7%9A%84%E6%97%B6%E5%80%99%2C%E6%80%8E%E4%B9%88%E8%87%AA%E5%8A%A8%E6%89%93%E5%BC%80%E9%97%AA%E5%85%89%E7%81%AF&spm=1018.2226.3001.4187
* @Description: 支持拍照、闪光灯设置,返回图片地址
* @show:控制相机显示/隐藏
* @getUrl:返回拍照图片地址
* @close:关闭相机
* @Author: 程圣宝
* @Date: 2024-6-06 09:51:25
-->
<template>
<view class="custom-upload">
<!-- 相机弹出层 -->
<u-overlay :show="show" mask-click-able="false">
<view class="nav">
<view class="back-btn" @tap="back">
<u-icon name="arrow-left" color="#fff" size="26"></u-icon>
</view>
<view class="flash" @tap="showOptionsPopup = true">
<!-- 闪光灯按钮 -->
<image
@click="setFlash()"
v-if="flashStatus === 'auto'"
src="@/static/flash/auto.png"
mode="widthFix"
style="width: 26px; height: 26px"
></image>
<image
@click="setFlash()"
v-if="flashStatus === 'off'"
src="@/static/flash/off.png"
mode="widthFix"
style="width: 26px; height: 26px"
></image>
<image
@click="setFlash()"
v-if="flashStatus === 'torch'"
src="@/static/flash/torch.png"
mode="widthFix"
style="width: 26px; height: 26px"
></image>
</view>
</view>
<view class="camera-container">
<view v-if="!isCameraReady" class="loading">
<u-loading mode="circle"></u-loading>
<text>相机启动中...</text>
</view>
<camera
v-if="show"
device-position="back"
:flash="flashStatus"
style="width: 100%; height: 100%"
@ready="onCameraReady"
@error="onCameraError"
:frame-size="frameSize"
:resolution="resolution"
>
<cover-view class="camera-btn" @tap.stop="takePhoto">
<cover-view class="btn"></cover-view>
</cover-view>
</camera>
</view>
</u-overlay>
</view>
</template>
<script>
export default {
name: "myCamera",
props: {
show: {
type: Boolean,
default: false,
},
},
data() {
return {
showOptionsPopup: false, // 闪光灯选项弹出层
flashStatus: "auto", // 闪光灯,值为auto, on, off, torch
flashList: ["auto", "torch", "off"],
index: 0,
cameraContext: null,
isCameraReady: false,
frameSize: "medium", // 使用中等尺寸,加快加载速度
resolution: "medium", // 使用中等分辨率,加快加载速度
};
},
watch: {
show(newVal) {
if (newVal) {
// 显示时立即初始化相机
this.$nextTick(() => {
this.initCamera();
});
} else {
this.isCameraReady = false;
}
},
},
methods: {
initCamera() {
if (!this.cameraContext) {
this.cameraContext = uni.createCameraContext();
}
},
onCameraReady() {
console.log("相机就绪");
this.isCameraReady = true;
},
onCameraError(err) {
console.error("相机错误:", err);
this.isCameraReady = false;
uni.showToast({
title: "相机启动失败",
icon: "none",
});
},
async takePhoto() {
if (!this.cameraContext) {
this.initCamera();
}
try {
const res = await new Promise((resolve, reject) => {
this.cameraContext.takePhoto({
quality: "normal", // 使用普通质量,加快拍照速度
success: resolve,
fail: reject,
});
});
const tempUrl = res.tempImagePath;
this.$emit("getUrl", tempUrl);
} catch (err) {
console.log("拍照报错:", err);
uni.showToast({
title: "拍照失败",
icon: "none",
});
}
},
back() {
this.$emit("close");
},
setFlash() {
if (this.index == 2) {
this.index = 0;
this.flashStatus = this.flashList[this.index];
} else {
this.index++;
this.flashStatus = this.flashList[this.index];
}
},
},
beforeDestroy() {
this.cameraContext = null;
},
};
</script>
<style lang="scss">
.custom-upload {
// border: 1px dashed red;
display: flex;
flex-direction: row;
flex-wrap: wrap;
background-color: #fff;
.file-item {
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border-radius: 2px;
margin: 0 8px 8px 0;
box-sizing: border-box;
.upload-deletable {
position: absolute;
top: 0;
right: 0;
background-color: #373737;
height: 14px;
width: 14px;
display: flex;
flex-direction: row;
border-bottom-left-radius: 100px;
align-items: center;
justify-content: center;
z-index: 3;
.upload-deletable-icon {
position: absolute;
-webkit-transform: scale(0.7);
transform: scale(0.7);
top: 0px;
right: 0px;
}
}
.upload-success {
position: absolute;
bottom: 0;
right: 0;
display: flex;
flex-direction: row;
border-style: solid;
border-top-color: transparent;
border-left-color: transparent;
border-bottom-color: #5ac725;
border-right-color: #5ac725;
border-width: 9px;
align-items: center;
justify-content: center;
.upload-success-icon {
position: absolute;
-webkit-transform: scale(0.7);
transform: scale(0.7);
bottom: -10px;
right: -10px;
}
}
}
.upload-button {
padding: 10rpx;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f4f5f7;
border-radius: 2px;
margin: 0 8px 8px 0;
box-sizing: border-box;
.upload-button-text {
margin-top: 8rpx;
color: #ccc;
text-align: center;
}
}
.option-list {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 40rpx 40rpx 20rpx 40rpx;
.option-btn {
border-bottom: 1px solid #ccc6;
padding: 30rpx;
width: 100%;
text-align: center;
font-size: 16px;
}
.option-btn-close {
padding: 30rpx;
width: 100%;
text-align: center;
font-size: 16px;
}
}
.nav {
height: 100rpx;
padding: 0 30rpx;
padding-left: 40rpx;
box-sizing: border-box;
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
background-color: black;
}
.camera-container {
position: relative;
width: 100%;
height: 100%;
background-color: #000;
.back-btn {
position: absolute;
bottom: 140rpx;
left: 100rpx;
background-color: #fff;
border-radius: 50%;
padding: 15rpx;
display: flex;
place-content: center;
.back-btn-icon {
transform: rotate(180deg);
}
}
.flash-btn {
position: absolute;
top: 100rpx;
left: 100rpx;
}
.camera-btn {
position: absolute;
bottom: 100rpx;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
justify-content: center;
width: 170rpx;
height: 170rpx;
border-radius: 50%;
background-color: #626264;
.btn {
width: 135rpx;
height: 135rpx;
border-radius: 50%;
background-color: #fff;
}
}
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
align-items: center;
color: #fff;
text {
margin-top: 20rpx;
font-size: 28rpx;
}
}
}
}
</style>