feat:集成lime-camera插件实现内联相机预览+连拍

This commit is contained in:
ShenQiLun 2026-06-25 16:05:16 +08:00
parent bdfbf63380
commit 312b91bd48

View File

@ -2,19 +2,43 @@
<view class="cc-page"> <view class="cc-page">
<!-- 摄像头预览区 --> <!-- 摄像头预览区 -->
<view class="cc-camera-wrap"> <view class="cc-camera-wrap">
<video id="ccVideo" ref="ccVideo" class="cc-video" autoplay playsinline muted v-if="useWebRTC"></video> <!-- lime-camera 预览Android nvue / uni-app x -->
<view class="cc-camera-hint" v-if="!useWebRTC"> <l-camera
<text class="cc-hint-text">{{ isBurstMode ? '连拍模式中...' : '点击下方拍照按钮' }}</text> v-if="useLimeCamera"
ref="lCamera"
class="cc-lcamera"
mode="normal"
:device-position="cameraPosition"
flash="off"
:focus="true"
@initdone="onCameraInitDone"
@error="onCameraError"
style="width:100%;height:100%;"
></l-camera>
<!-- WebRTC 预览H5 -->
<video
v-else-if="useWebRTC"
id="ccVideo"
ref="ccVideo"
class="cc-video"
autoplay
playsinline
muted
></video>
<!-- 无预览时占位 -->
<view v-else class="cc-camera-hint">
<text class="cc-hint-text">相机加载中...</text>
</view> </view>
<canvas id="ccCanvas" ref="ccCanvas" class="cc-canvas" style="display:none;"></canvas> <canvas id="ccCanvas" ref="ccCanvas" class="cc-canvas" style="display:none;"></canvas>
<!-- 连拍模式相机遮罩提示 --> <!-- 连拍模式提示遮罩 -->
<view class="cc-burst-overlay" v-if="isBurstMode"> <view class="cc-burst-overlay" v-if="isBurstMode">
<text class="cc-burst-tip">连拍中</text> <text class="cc-burst-tip">连拍中</text>
<text class="cc-burst-count">{{ capturedList.length }} </text> <text class="cc-burst-count">{{ capturedList.length }} </text>
<text class="cc-burst-dots"> <text class="cc-burst-dots">
<text class="cc-dot" v-for="n in 3" :key="n">.</text> <text class="cc-dot" v-for="n in 3" :key="n">.</text>
</text> </text>
<text class="cc-burst-hint">点击红色按钮停止</text>
</view> </view>
</view> </view>
@ -28,8 +52,7 @@
<text class="cc-del-icon"></text> <text class="cc-del-icon"></text>
</view> </view>
</view> </view>
<view class="cc-thumb-item cc-thumb-placeholder" v-for="n in (9 - capturedList.length)" :key="'p'+n"> <view class="cc-thumb-item cc-thumb-placeholder" v-for="n in (9 - capturedList.length)" :key="'p'+n"></view>
</view>
</view> </view>
</view> </view>
@ -45,11 +68,9 @@
</view> </view>
<view class="cc-right-actions"> <view class="cc-right-actions">
<!-- 连拍模式切换 -->
<view class="cc-burst-toggle" @click="toggleBurstMode" v-if="!isBurstMode && capturedList.length < 9"> <view class="cc-burst-toggle" @click="toggleBurstMode" v-if="!isBurstMode && capturedList.length < 9">
<text class="cc-burst-toggle-text">{{ isBurstEnabled ? '⚡连拍' : '拍摄' }}</text> <text class="cc-burst-toggle-text">连拍</text>
</view> </view>
<!-- 确认按钮 -->
<view class="cc-confirm-btn" @click="confirmCapture" :class="{ disabled: capturedList.length === 0 }"> <view class="cc-confirm-btn" @click="confirmCapture" :class="{ disabled: capturedList.length === 0 }">
<text class="cc-confirm-text">确认 ({{ capturedList.length }})</text> <text class="cc-confirm-text">确认 ({{ capturedList.length }})</text>
</view> </view>
@ -59,6 +80,18 @@
</template> </template>
<script> <script>
// lime-cameraAndroid nvue 使
var limeCameraReady = false
var createCameraContext = null
try {
var limeModule = require('@/uni_modules/lime-camera')
createCameraContext = limeModule.createCameraContext
limeCameraReady = true
} catch (e) {
console.log('lime-camera 插件未安装,使用备用方案')
limeCameraReady = false
}
export default { export default {
data() { data() {
return { return {
@ -66,8 +99,10 @@
mediaStream: null, mediaStream: null,
ctxReady: false, ctxReady: false,
useWebRTC: false, useWebRTC: false,
useLimeCamera: false,
cameraContext: null,
cameraPosition: 'back',
isBurstMode: false, isBurstMode: false,
isBurstEnabled: false,
burstTimer: null burstTimer: null
} }
}, },
@ -81,19 +116,46 @@
methods: { methods: {
// //
checkEnvironment() { checkEnvironment() {
// 1. lime-cameraAndroid nvue
if (limeCameraReady) {
this.useLimeCamera = true
this.ctxReady = true
console.log('使用 lime-camera 相机')
return
}
// 2. WebRTCH5
try { try {
if (typeof document !== 'undefined' && typeof navigator !== 'undefined' && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { if (typeof document !== 'undefined' && typeof navigator !== 'undefined' && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
this.useWebRTC = true this.useWebRTC = true
this.startWebRTC() this.startWebRTC()
} else { return
this.useWebRTC = false
this.ctxReady = true
} }
} catch (e) { } catch (e) {
this.useWebRTC = false // fall through
this.ctxReady = true
} }
// 3.
this.useWebRTC = false
this.ctxReady = false
uni.showToast({ title: '当前环境不支持相机预览', icon: 'none' })
}, },
// lime-camera
onCameraInitDone(e) {
console.log('lime-camera 初始化完成,最大缩放:', e.detail.maxZoom)
this.cameraContext = createCameraContext()
this.ctxReady = true
},
// lime-camera
onCameraError(err) {
console.error('lime-camera 错误:', err)
uni.showToast({ title: '相机启动失败: ' + (err.detail ? JSON.stringify(err.detail) : '未知错误'), icon: 'none' })
this.useLimeCamera = false
this.checkEnvironment()
},
// WebRTC H5 // WebRTC H5
startWebRTC() { startWebRTC() {
var that = this var that = this
@ -124,6 +186,7 @@
this.ctxReady = true this.ctxReady = true
} }
}, },
stopCamera() { stopCamera() {
this.clearBurstTimer() this.clearBurstTimer()
if (this.mediaStream) { if (this.mediaStream) {
@ -134,47 +197,46 @@
this.mediaStream = null this.mediaStream = null
} }
}, },
toggleBurstMode() { toggleBurstMode() {
this.isBurstEnabled = !this.isBurstEnabled
if (this.isBurstEnabled) {
this.startBurst()
}
},
startBurst() {
if (this.capturedList.length >= 9) { if (this.capturedList.length >= 9) {
uni.showToast({ title: '最多拍9张', icon: 'none' }) uni.showToast({ title: '最多拍9张', icon: 'none' })
this.isBurstEnabled = false
return return
} }
this.startBurst()
},
startBurst() {
this.isBurstMode = true this.isBurstMode = true
//
this.doBurstCapture() this.doBurstCapture()
}, },
doBurstCapture() { doBurstCapture() {
if (!this.isBurstMode || this.capturedList.length >= 9) { if (!this.isBurstMode || this.capturedList.length >= 9) {
this.isBurstMode = false this.isBurstMode = false
this.isBurstEnabled = false
return return
} }
var that = this var that = this
this.captureSinglePhoto(function() { this.captureSinglePhoto(function() {
// 1.5 // 1.5
that.burstTimer = setTimeout(function() { that.burstTimer = setTimeout(function() {
that.doBurstCapture() that.doBurstCapture()
}, 1500) }, 1500)
}) })
}, },
stopBurst() { stopBurst() {
this.isBurstMode = false this.isBurstMode = false
this.isBurstEnabled = false
this.clearBurstTimer() this.clearBurstTimer()
}, },
clearBurstTimer() { clearBurstTimer() {
if (this.burstTimer) { if (this.burstTimer) {
clearTimeout(this.burstTimer) clearTimeout(this.burstTimer)
this.burstTimer = null this.burstTimer = null
} }
}, },
// //
capturePhoto() { capturePhoto() {
if (this.capturedList.length >= 9) { if (this.capturedList.length >= 9) {
@ -183,14 +245,48 @@
} }
this.captureSinglePhoto() this.captureSinglePhoto()
}, },
// //
captureSinglePhoto(callback) { captureSinglePhoto(callback) {
// 1. lime-camera Android
if (this.useLimeCamera && this.cameraContext) {
this.limeCapture(callback)
return
}
// 2. WebRTC H5
if (this.useWebRTC && this.mediaStream && this.ctxReady) { if (this.useWebRTC && this.mediaStream && this.ctxReady) {
this.webRTCCapture(callback) this.webRTCCapture(callback)
} else { return
this.systemCapture(callback) }
// 3.
this.systemCapture(callback)
},
// lime-camera
limeCapture(callback) {
var that = this
try {
this.cameraContext.takePhoto({
quality: 'high',
success: function(res) {
that.capturedList.push(res.tempImagePath)
if (callback) { callback() }
},
fail: function(err) {
console.error('lime拍照失败:', err)
if (that.isBurstMode) {
that.isBurstMode = false
}
uni.showToast({ title: '拍照失败', icon: 'none' })
if (callback) { callback() }
}
})
} catch (e) {
console.error('lime拍照异常:', e)
if (callback) { callback() }
} }
}, },
// WebRTC // WebRTC
webRTCCapture(callback) { webRTCCapture(callback) {
if (typeof document === 'undefined') { if (typeof document === 'undefined') {
@ -211,7 +307,8 @@
this.capturedList.push(dataUrl) this.capturedList.push(dataUrl)
if (callback) { callback() } if (callback) { callback() }
}, },
//
//
systemCapture(callback) { systemCapture(callback) {
var that = this var that = this
uni.chooseImage({ uni.chooseImage({
@ -225,18 +322,18 @@
if (callback) { callback() } if (callback) { callback() }
}, },
fail: function() { fail: function() {
//
if (that.isBurstMode) { if (that.isBurstMode) {
that.isBurstMode = false that.isBurstMode = false
that.isBurstEnabled = false
} }
if (callback) { callback() } if (callback) { callback() }
} }
}) })
}, },
deletePhoto(idx) { deletePhoto(idx) {
this.capturedList.splice(idx, 1) this.capturedList.splice(idx, 1)
}, },
confirmCapture() { confirmCapture() {
if (this.capturedList.length === 0) { if (this.capturedList.length === 0) {
uni.showToast({ title: '请先拍照', icon: 'none' }) uni.showToast({ title: '请先拍照', icon: 'none' })
@ -271,6 +368,10 @@
align-items: center; align-items: center;
justify-content: center; justify-content: center;
} }
.cc-lcamera {
width: 100%;
height: 100%;
}
.cc-video { .cc-video {
width: 100%; width: 100%;
height: 100%; height: 100%;
@ -291,22 +392,25 @@
.cc-burst-overlay { .cc-burst-overlay {
position: absolute; position: absolute;
top: 0; left: 0; right: 0; bottom: 0; top: 0; left: 0; right: 0; bottom: 0;
background: rgba(0,0,0,0.3); background: rgba(0,0,0,0.25);
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
z-index: 5; z-index: 5;
pointer-events: none;
} }
.cc-burst-tip { .cc-burst-tip {
color: #fff; color: #fff;
font-size: 40rpx; font-size: 40rpx;
font-weight: bold; font-weight: bold;
text-shadow: 0 2rpx 8rpx rgba(0,0,0,0.6);
} }
.cc-burst-count { .cc-burst-count {
color: rgba(255,255,255,0.8); color: rgba(255,255,255,0.9);
font-size: 32rpx; font-size: 32rpx;
margin-top: 16rpx; margin-top: 16rpx;
text-shadow: 0 2rpx 8rpx rgba(0,0,0,0.6);
} }
.cc-burst-dots { .cc-burst-dots {
color: #409eff; color: #409eff;
@ -318,6 +422,12 @@
0%, 100% { opacity: 0.3; } 0%, 100% { opacity: 0.3; }
50% { opacity: 1; } 50% { opacity: 1; }
} }
.cc-burst-hint {
color: rgba(255,255,255,0.6);
font-size: 24rpx;
margin-top: 20rpx;
text-shadow: 0 2rpx 8rpx rgba(0,0,0,0.6);
}
.cc-dot { .cc-dot {
margin: 0 4rpx; margin: 0 4rpx;
} }