Skip to content

t-camera

Camera 相机

组件介绍

t-camera 是一个跨平台相机封装组件,统一承接 Web 和原生端的相机实现,支持拍照、录像、切换前后摄像头和闪光灯控制。适合图片采集、扫码辅助、视频录制和本地媒体预览等场景。

平台兼容

HarmonyH5AndroidiOS小程序UTS

基础用法

基础接入时,只需要监听拍照和录像完成事件即可:

vue
<t-camera
  ref="camreaRef"
  class="w-100% h-550 r-20 bg-#fff mtb-30"
  @take="getPhoto"
  @recoder="recoder"
></t-camera>
vue
<script setup lang="ts">
import { ref, ComponentPublicInstance } from 'vue'

const urlSrc = ref('')
const recoderUrl = ref('')
const camreaRef = ref<ComponentPublicInstance | null>(null)

const recoder = (request: string) => {
  recoderUrl.value = request
}

const getPhoto = (result: string) => {
  urlSrc.value = result
}
</script>

如果需要通过实例控制相机,可以继续调用组件暴露的方法:

vue
<t-button type="primary" @click="front">前置摄像头</t-button>
<t-button type="primary" @click="back">后置摄像头</t-button>
<t-button type="primary" @click="open">打开相机</t-button>
<t-button type="warning" @click="close">关闭相机</t-button>
<t-button type="success" @click="take">拍照</t-button>

Props

属性名说明类型默认值
path点击组件后跳转的页面路径;为空时仅响应事件string''
hover是否启用点击效果booleanfalse
type组件主题类型,可选 infoprimaryerrorwarningsuccessstring'primary'
disabled是否禁用booleanfalse
effect组件显示主题,可选 normaldarklightplainstring'normal'
size组件尺寸,可选 largemediumsmallministring'medium'
flash闪光灯模式,可选 autoonoffstring'auto'

Events

事件名说明回调参数
take拍照完成时触发string,图片地址
recoder录像结束时触发string,视频地址或 blob url

Slots

当前组件未声明插槽。

Methods

方法名说明
recoder手动转发录像完成事件给外部
flashOn打开闪光灯
flashOff关闭闪光灯
getPhoto手动转发拍照结果给外部
front切换到前置摄像头
back切换到后置摄像头
open打开相机
close关闭相机
start开始录像
stops停止录像
take执行拍照
vue
<script setup lang="ts">
import { ref, ComponentPublicInstance } from 'vue'

const camreaRef = ref<ComponentPublicInstance | null>(null)

const callCameraMethod = (method: string) => {
  camreaRef.value?.$callMethod(method)
}

const front = () => callCameraMethod('front')
const back = () => callCameraMethod('back')
const open = () => callCameraMethod('open')
const close = () => callCameraMethod('close')
const take = () => callCameraMethod('take')
const flashOn = () => callCameraMethod('flashOn')
const flashOff = () => callCameraMethod('flashOff')
const start = () => callCameraMethod('start')
const stops = () => callCameraMethod('stops')
</script>