useCamera
相机操作等.
何时使用
当需要对相机进行操作时
API
const [cameraContext, { zoom, start, stop, take, listener }] = useCamera();
参数说明
无
返回值说明
返回值 | 说明 | 类型 |
---|---|---|
cameraContext | camera 上下文 | CameraContext |
zoom | 设置缩放级别 | PromiseAction<number, CameraContext.StartRecordSuccessCallbackResult> |
start | 开始录像 | PromiseOptionalAction<ExcludeOption<CameraContext.StartRecordOption>> |
stop | 结束录像 | PromiseOptionalAction<boolean,CameraContext.StopRecordSuccessCallbackResult> |
take | 拍照 | PromiseOptionalAction<ExcludeOption<CameraContext.TakePhotoOption>, CameraContext.TakePhotoSuccessCallbackResult> |
listener | 获取 Camera 实时帧数据 | (callback: CameraContext.OnCameraFrameCallback) => CameraFrameListener |
代码演示
- React
- Vue
media/useCamera/index
import React from 'react';
import { Camera } from '@tarojs/components';
import { useToast, useCamera, useImage } from 'taro-hooks';
import DemoContent from '@src/components/DemoContent';
import { Button } from '@taroify/core';
import './index.less';
export default () => {
const [cameraContext, { zoom, start, stop, take }] = useCamera();
const [, { preview, previewMedia }] = useImage();
const { show } = useToast({
title: 'useCamera',
duration: 500,
mask: true,
});
const handleStart = async () => {
try {
await start();
show({ title: '开启录制成功' });
} catch (e) {
show({ title: '开启录制失败', icon: 'error' });
}
};
const handleStop = async () => {
try {
const video = await stop();
previewMedia({ sources: [{ url: video.tempVideoPath }] });
show({ title: '关闭录制成功' });
} catch (e) {
show({ title: '关闭录制失败', icon: 'error' });
}
};
const handleTake = async () => {
try {
const { tempImagePath } = await take();
preview({ urls: [tempImagePath] });
show({ title: '拍摄成功' });
} catch (e) {
show({ title: '拍摄失败', icon: 'error' });
}
};
return (
<DemoContent>
<Camera id="demo-camera-id" />
<Button
block
color="primary"
className="gap"
onClick={() => handleStart()}
shape="square"
>
开始录制
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => handleStop()}
shape="square"
>
停止录制
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => handleTake()}
shape="square"
>
拍照
</Button>
</DemoContent>
);
};
media/useCamera/index
<template>
<demo-content>
<camera id="demo-camera-id"></camera>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="handleStart()"
>开始录制</nut-button
>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="handleStop()"
>停止录制</nut-button
>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="handleTake()"
>拍照</nut-button
>
</demo-content>
</template>
<script setup lang="ts">
import { useToast, useCamera, useImage } from 'taro-hooks';
const [cameraContext, { zoom, start, stop, take }] = useCamera();
const [, { preview, previewMedia }] = useImage();
const { show } = useToast({
title: 'useCamera',
duration: 500,
mask: true,
});
const handleStart = async () => {
try {
await start();
show({ title: '开启录制成功' });
} catch (e) {
show({ title: '开启录制失败', icon: 'error' });
}
};
const handleStop = async () => {
try {
const video = await stop();
previewMedia({ sources: [{ url: video.tempVideoPath }] });
show({ title: '关闭录制成功' });
} catch (e) {
show({ title: '关闭录制失败', icon: 'error' });
}
};
const handleTake = async () => {
try {
const { tempImagePath } = await take();
preview({ urls: [tempImagePath] });
show({ title: '拍摄成功' });
} catch (e) {
show({ title: '拍摄失败', icon: 'error' });
}
};
</script>
<style>
#demo-camera-id {
position: relative;
left: auto;
top: auto;
width: 100%;
height: 200px;
margin-bottom: 20px;
}
</style>
Hook 支持度
微信小程序 | H5 | ReactNative |
---|---|---|
✔️ | ✔️ |