更新记录
1.0.0(2025-05-27)
新增功能
- 新增方法:
close
、 open
、 takePhoto
、 takePhotoSnapshot
、 takeVideo
、takeVideoSnapshot
、
stopVideo
、 changeZoom
、 changeFacing
、changeFlash
、changeAudio
- 新增事件:
onPictureTaken
、 onVideoTakenStart
、 onVideoTakenEnd
、 onFocusStart
、 onFocusEnd
android端
的所有功能已完成开发、测试
harmony端
计划开发中
- 初始版
问题修复
功能优化
平台兼容性
uni-app
Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
iOS |
鸿蒙 |
√ |
√ |
- |
- |
- |
- |
5.0 |
× |
× |
微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
快应用-华为 |
快应用-联盟 |
× |
× |
× |
× |
× |
× |
- |
× |
× |
× |
× |
uni-app x
Chrome |
Safari |
Android |
iOS |
鸿蒙 |
微信小程序 |
- |
- |
5.0 |
× |
× |
× |
原生自定义相机拍照、视频录制 (ima-camera-view)
原生自定义相机拍照、视频录制 (ima-camera-view)
是基于原生相机开发的UTS插件,支持相机拍照
、视频录制
、可实现点击聚焦
、手势缩放
、
自定义布局
、自定义蒙版
(用于人脸拍照,身份证拍照等)。
⚠️注意️
android 端
已全部完成代码的开发与测试(已完成)
harmony 端
已完成部分代码的开发与实现,待测试无问题后,再进行发布,敬请关注(计划开发中...)
ios 端
由于苹果Developer年费续费到期、开发|发布证书等一系列导致无法进行调试等,所以暂无开发计划(已写有部分代码,需要的话可单独联系「不保证运行,本插件中不存在该代码」)
(暂无计划)
小程序、H5、快应用
暂无开发计划(暂无计划)
需要权限
"android.permission.CAMERA",
"android.permission.RECORD_AUDIO",
"android.permission.READ_EXTERNAL_STORAGE"
"android.permission.WRITE_EXTERNAL_STORAGE"
- 即:在
manifest.json
中的distribute.android.permissions
加入
// 拍摄照片和视频时需要
<uses-permission android:name="android.permission.CAMERA" />
// 拍摄视频时需要Audio.ON(默认)
<uses-permission android:name="android.permission.RECORD_AUDIO" />
// 读取拍照、录像文件文件时需要
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
// 报错拍照、录像文件文件时需要(默认保存到沙盒缓存)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
使用示例【此示例的代码只实现了相机拍照
的逻辑,更多示例请导入项目】
- 新建一个
camera.nvue
的文件
- ⚠️注意️:只能在
.nvue
、.uvue
的文件后缀下才生效,不支持.vue
<template>
<view class="ima-camera" :style="{ width: windowWidth, height: windowHeight }">
<ima-camera-view
ref="cameraRef"
class="camera-view"
:style="{ width: windowWidth + 'px', height: windowHeight + 'px' }"
flash="on"
@onPictureTaken="onPictureTaken"
onFocusStart="onFocusStart"
/>
<view class="camera-menu">
<!--返回键-->
<cover-image @tap="back" class="camera-menu-button back" src="/static/camera/back.png" />
<!--快门键-->
<cover-image
@tap="takePhoto"
class="camera-menu-button shutter"
src="/static/camera/shutter.png"
/>
<!--反转键-->
<cover-image @tap="flip" class="camera-menu-button flip" src="/static/camera/flip.png" />
</view>
</view>
</template>
<script>
let _this = null
export default {
data() {
return {
windowWidth: '', //屏幕可用宽度
windowHeight: '', //屏幕可用高度
facing: 'back'
}
},
onLoad() {
_this = this
this.initCamera()
},
methods: {
//初始化相机
initCamera() {
console.log('初始化相机')
uni.getSystemInfo({
success: (res) => {
_this.windowWidth = res.windowWidth
_this.windowHeight = res.windowHeight
}
})
},
onFocusStart(e) {
console.log('聚焦', e)
},
takePhoto() {
console.log('拍照', this.facing)
this.$refs.cameraRef.takePhoto()
},
//返回
back() {
console.log('返回上一页', this.facing)
uni.navigateBack()
},
//反转
flip() {
console.log('镜头反转', this.facing)
this.facing = this.facing === 'back' ? 'front' : 'back'
this.$refs.cameraRef.changeFacing(this.facing)
},
onPictureTaken(e) {
console.log('拍照结果', e.detail)
_this.snapshotsrc = e.detail?.path || ''
_this.getTakenRes()
uni.navigateBack()
},
//设置
getTakenRes() {
console.log('返回结果给上一页')
let pages = getCurrentPages()
let prevPage = pages[pages.length - 2] //上一个页面
//直接调用上一个页面的setImage()方法,把数据存到上一个页面中去
prevPage.$vm.setImage({ path: _this.snapshotsrc })
}
}
}
</script>
<style lang="scss">
.ima-camera {
justify-content: center;
align-items: center;
.camera-view {
width: 100%;
background: #111;
}
.camera-menu {
position: absolute;
left: 0;
bottom: 0;
width: 750rpx;
height: 180rpx;
z-index: 98;
align-items: center;
justify-content: center;
&-button {
width: 80rpx;
height: 80rpx;
position: absolute;
bottom: 50rpx;
z-index: 99;
align-items: center;
justify-content: center;
}
.back {
left: 30rpx;
}
.shutter {
width: 130rpx;
height: 130rpx;
left: 310rpx;
bottom: 25rpx;
}
.flip {
right: 30rpx;
}
}
}
</style>
Api
属性 |
类型 |
默认值 |
说明 |
平台 |
facing |
String |
"back" |
后置、前置摄像头: back(后置摄像头)、front(前置摄像头) |
android 、harmony |
flash |
String |
"off" |
【android】 闪光灯: off(关闭)、on(开启)、auto(自动)、torch(常开) 【harmony】 闪光灯: off(关闭)、on(开启)、auto(自动) |
android 、harmony |
audio |
String |
"on" |
【android】 音频: on(开启)、off(关闭)、mono(单声道)、stereo(立体声) 【harmony】 音频: on(开启)、off(关闭) |
android 、harmony |
mode |
String |
'picture' |
相机模式: picture(拍照)、video(录视频) |
harmony |
方法
共同 方法
方法名称 |
说明 |
方法参数 |
平台 |
open |
打开摄像头预览 |
无 |
android 、harmony |
close |
关闭摄像头预览 |
无 |
android 、harmony |
takePhoto |
拍照(标准拍照流程) |
无 |
android 、harmony |
takeVideo |
开始录制视频 |
无 |
android 、harmony |
stopVideo |
停止视频录制 |
无 |
android 、harmony |
changeZoom |
设置摄像头缩放级别 |
zoom:缩放倍数(浮点数) |
android 、harmony |
changeFacing |
设置摄像头方向 |
facing:参考api 中的facing 参数 |
android 、harmony |
changeFlash |
设置闪光灯模式 |
flash:参考api 中的flash s参数 |
changeAudio |
设置音频 |
audio:参考api 中的audio 参数 |
android 、harmony |
takePhotoSnapshot |
快照拍照(适用于快速拍照场景) |
无 |
android |
takeVideoSnapshot |
快照方式录制视频 |
duration:可传入时长(0 表示不限制) |
android |
事件
事件名称 |
说明 |
回调参数 |
平台 |
onPictureTaken |
拍照返回数据 |
({path,width,height}: any) => {} |
android 、harmony |
onVideoTakenStart |
录制视频开始事件 |
() => {} |
android 、harmony |
onVideoTakenEnd |
录制视频结束事件 |
({path,size}: any) => {} |
android 、harmony |
onFocusStart |
自动对焦开始 |
({x,y}: any) => {} |
android 、harmony |
onFocusEnd |
自动对焦结束 |
({x,y,focus}: any) => {} |
android 、harmony |