Camera-view
组件类型:UxCameraViewComponentPublicInstance
支持拍照、切换镜头、闪光灯、手电筒功能
平台兼容性
UniApp X
Android | iOS | web | 鸿蒙 Next | 小程序 |
---|---|---|---|---|
√ | √ | √ | x | √ |
UniApp Vue Nvue
Android | iOS | web | 鸿蒙 Next | 小程序 |
---|---|---|---|---|
x | x | √ | x | x |
Events
事件名 | 说明 | 参数 |
---|---|---|
mode | 模式,注意:watermark拍照模式性能没有take好 | |
take | 拍照时触发 |
示例代码
html
<template>
<ux-page :stack="showDoc">
<ux-navbar :title="title" :bold="true">
<template v-slot:right>
<!-- #ifndef MP -->
<ux-button theme="text" icon="/static/tip.png" :icon-size="22" @click="onDoc()"></ux-button>
<!-- #endif -->
</template>
</ux-navbar>
<ux-scroll>
<ux-card direction="column" icon="flag-filled" title="相机预览拍照组件" :bold="true">
<ux-text text="支持拍照、切换镜头、闪光灯、手电筒功能"></ux-text>
</ux-card>
<!-- #ifndef MP -->
<ux-card direction="column" icon="arrowright" title="水印相机" :bold="true">
<view id="watermark" style="height: 400px;">
<ux-camera-view ref="uxCameraViewRef" style="flex: 1;" mode="watermark" @take="onCapture"></ux-camera-view>
<text class="text">水印相机</text>
</view>
</ux-card>
<ux-card direction="row" icon="arrowright" title="功能" :bold="true">
<ux-button :margin="[4]" text="打开" @click="onOpen()"></ux-button>
<ux-button :margin="[4]" text="关闭" @click="onClose()"></ux-button>
<ux-button :margin="[4]" text="拍照" @click="onTake()"></ux-button>
<ux-button :margin="[4]" text="水印拍照" @click="onWatermark()"></ux-button>
<ux-button :margin="[4]" text="切换摄像头" @click="onSwitch()"></ux-button>
<ux-button :margin="[4]" text="闪光灯" @click="onFlash()"></ux-button>
<ux-button :margin="[4]" text="手电筒" @click="onTorch()"></ux-button>
</ux-card>
<ux-placeholder :height="200">
<ux-row justify="center" align="center" style="height: 100%;">
<ux-text prefix-icon="soapbubble-filled" text="真的没有了~"></ux-text>
</ux-row>
</ux-placeholder>
<!-- #endif -->
<!-- #ifdef MP -->
<ux-placeholder :height="200">
<ux-row justify="center" align="center" style="height: 100%;">
<ux-text prefix-icon="soapbubble-filled" text="小程序不支持"></ux-text>
</ux-row>
</ux-placeholder>
<!-- #endif -->
</ux-scroll>
</ux-page>
</template>
<template>
<ux-page :stack="showDoc">
<ux-navbar :title="title" :bold="true">
<template v-slot:right>
<!-- #ifndef MP -->
<ux-button theme="text" icon="/static/tip.png" :icon-size="22" @click="onDoc()"></ux-button>
<!-- #endif -->
</template>
</ux-navbar>
<ux-scroll>
<ux-card direction="column" icon="flag-filled" title="相机预览拍照组件" :bold="true">
<ux-text text="支持拍照、切换镜头、闪光灯、手电筒功能"></ux-text>
</ux-card>
<!-- #ifndef MP -->
<ux-card direction="column" icon="arrowright" title="水印相机" :bold="true">
<view id="watermark" style="height: 400px;">
<ux-camera-view ref="uxCameraViewRef" style="flex: 1;" mode="watermark" @take="onCapture"></ux-camera-view>
<text class="text">水印相机</text>
</view>
</ux-card>
<ux-card direction="row" icon="arrowright" title="功能" :bold="true">
<ux-button :margin="[4]" text="打开" @click="onOpen()"></ux-button>
<ux-button :margin="[4]" text="关闭" @click="onClose()"></ux-button>
<ux-button :margin="[4]" text="拍照" @click="onTake()"></ux-button>
<ux-button :margin="[4]" text="水印拍照" @click="onWatermark()"></ux-button>
<ux-button :margin="[4]" text="切换摄像头" @click="onSwitch()"></ux-button>
<ux-button :margin="[4]" text="闪光灯" @click="onFlash()"></ux-button>
<ux-button :margin="[4]" text="手电筒" @click="onTorch()"></ux-button>
</ux-card>
<ux-placeholder :height="200">
<ux-row justify="center" align="center" style="height: 100%;">
<ux-text prefix-icon="soapbubble-filled" text="真的没有了~"></ux-text>
</ux-row>
</ux-placeholder>
<!-- #endif -->
<!-- #ifdef MP -->
<ux-placeholder :height="200">
<ux-row justify="center" align="center" style="height: 100%;">
<ux-text prefix-icon="soapbubble-filled" text="小程序不支持"></ux-text>
</ux-row>
</ux-placeholder>
<!-- #endif -->
</ux-scroll>
</ux-page>
</template>
ts
<script setup>
import * as plus from '@/uni_modules/ux-plus'
const isFront = ref(false)
const isFlash = ref(false)
const isTorch = ref(false)
const uxCameraViewRef = ref<UxCameraViewElement | null>(null)
function onOpen() {
uxCameraViewRef.value?.open()
}
function onClose() {
uxCameraViewRef.value?.close()
}
function onTake() {
uxCameraViewRef.value?.take(true)
}
function onWatermark() {
// #ifdef APP
const view = uni.getElementById('watermark')!
view.takeSnapshot({
success: (res) => {
console.log(res.tempFilePath);
uni.previewImage({
urls: [res.tempFilePath],
current: res.tempFilePath
})
},
fail: (res) => {
console.log(res)
uni.showToast({
icon: 'error',
title: '截图失败'
})
}
})
// #endif
// #ifndef APP
uni.showToast({
title: '请使用UxFrameAPP体验此功能',
icon: 'none'
})
// #endif
}
function onCapture(e : UTSJSONObject) {
let url = e['path']! as string
uni.previewImage({
urls: [url]
})
}
function onSwitch() {
isFront.value = !isFront.value
uxCameraViewRef.value?.switch(isFront.value)
}
function onFlash() {
isFlash.value = !isFlash.value
uxCameraViewRef.value?.flash(isFlash.value)
}
function onTorch() {
isTorch.value = !isTorch.value
uxCameraViewRef.value?.torch(isTorch.value)
}
const showDoc = ref(false)
function onDoc() {
plus.openWeb({
title: '在线文档',
url: 'https://www.uxframe.cn/component/camera-view.html',
// blur: 1,
success: () => {
showDoc.value = true
},
complete: () => {
showDoc.value = false
}
})
}
const title = ref('')
onLoad((e) => {
title.value = e['title'] ?? ''
})
</script>
<script setup>
import * as plus from '@/uni_modules/ux-plus'
const isFront = ref(false)
const isFlash = ref(false)
const isTorch = ref(false)
const uxCameraViewRef = ref<UxCameraViewElement | null>(null)
function onOpen() {
uxCameraViewRef.value?.open()
}
function onClose() {
uxCameraViewRef.value?.close()
}
function onTake() {
uxCameraViewRef.value?.take(true)
}
function onWatermark() {
// #ifdef APP
const view = uni.getElementById('watermark')!
view.takeSnapshot({
success: (res) => {
console.log(res.tempFilePath);
uni.previewImage({
urls: [res.tempFilePath],
current: res.tempFilePath
})
},
fail: (res) => {
console.log(res)
uni.showToast({
icon: 'error',
title: '截图失败'
})
}
})
// #endif
// #ifndef APP
uni.showToast({
title: '请使用UxFrameAPP体验此功能',
icon: 'none'
})
// #endif
}
function onCapture(e : UTSJSONObject) {
let url = e['path']! as string
uni.previewImage({
urls: [url]
})
}
function onSwitch() {
isFront.value = !isFront.value
uxCameraViewRef.value?.switch(isFront.value)
}
function onFlash() {
isFlash.value = !isFlash.value
uxCameraViewRef.value?.flash(isFlash.value)
}
function onTorch() {
isTorch.value = !isTorch.value
uxCameraViewRef.value?.torch(isTorch.value)
}
const showDoc = ref(false)
function onDoc() {
plus.openWeb({
title: '在线文档',
url: 'https://www.uxframe.cn/component/camera-view.html',
// blur: 1,
success: () => {
showDoc.value = true
},
complete: () => {
showDoc.value = false
}
})
}
const title = ref('')
onLoad((e) => {
title.value = e['title'] ?? ''
})
</script>
css
<style lang="scss">
.text {
position: absolute;
bottom: 10px;
right: 10px;
font-size: 15px;
color: blue;
}
</style>
<style lang="scss">
.text {
position: absolute;
bottom: 10px;
right: 10px;
font-size: 15px;
color: blue;
}
</style>