更新记录
1.1.0(2026-11-01)
1、兼容蒸汽模式 2、自动对焦 3、缩放 质量自定义
平台兼容性
uni-app(5.26)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| × | √ | × | × | × | √ | √ | √ | × |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | × | × | × | × | × | × |
uni-app x(5.26)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| × | × | √ | √ | × | × |
其他
| 多语言 | 暗黑模式 | 宽屏模式 | 蒸汽模式 |
|---|---|---|---|
| × | × | × | √ |
UTS 原生相机拍照(对焦、缩放、取景框裁切)
原生相机预览、拍照与照片回调,适合证件采集和文档拍摄。附六种取景框、边框颜色与粗细设置,可点击对焦、调整缩放、JPEG 质量、输出长边及取景比例裁切。
| 项目 | 页面 | 平台 |
|---|---|---|
| uni-app x Vapor | .uvue |
Android / iOS |
| uni-app x VDOM | .uvue |
Android / iOS |
版本 1.1.0;HBuilderX 5.26+;Android 6.0+ / iOS 15.0+。普通 uni-app Vue3 使用 austin-camera-view 的 .nvue 入口。
第 1 步:放入插件
将整个 austin-camera-x 目录放到项目的 uni_modules 下,保留 components、utssdk 和 index.uvue。
业务页面导入的是组件文件,完整路径:
import AustinCameraX from '@/uni_modules/austin-camera-x/components/austin-camera-x/austin-camera-x.uvue'
不需要直接导入 utssdk 内的平台文件。
第 2 步:接入拍照页
<template>
<view class="page">
<austin-camera-x ref="cameraRef" class="camera"
cameraPosition="back" flashMode="off" focusMode="continuous"
:zoom="1" :quality="92" :cropToPreview="false" :maxPhotoEdge="0"
:previewCornerRadiusRate="0.12"
@ready="onReady" @captured="onCaptured" @error="onError" />
<button @click="takePhoto">{{ taking ? '正在拍照…' : '拍照' }}</button>
<image v-if="previewSrc.length > 0" class="photo" :src="previewSrc" mode="aspectFit" />
</view>
</template>
<script setup lang="uts">
import { ref, nextTick, ComponentPublicInstance } from 'vue'
import { onShow, onHide } from '@dcloudio/uni-app'
import AustinCameraX from '@/uni_modules/austin-camera-x/components/austin-camera-x/austin-camera-x.uvue'
const cameraRef = ref<ComponentPublicInstance | null>(null)
const ready = ref(false)
const taking = ref(false)
const previewSrc = ref('')
onShow(() => { nextTick(() => { cameraRef.value?.$callMethod('startPreview') }) })
onHide(() => { ready.value = false; taking.value = false; cameraRef.value?.$callMethod('stopPreview') })
function resultData(event: any | null): UTSJSONObject | null {
if (event == null) return null
if (event instanceof UTSJSONObject) {
const object = event as UTSJSONObject
return object.getJSON('detail') ?? object
}
return (event as UniCustomEvent<UTSJSONObject>).detail
}
function onReady(): void { ready.value = true }
function takePhoto(): void {
if (!ready.value || taking.value) return
taking.value = true
cameraRef.value?.$callMethod('takePhoto')
}
function onCaptured(event: any | null): void {
taking.value = false
const result = resultData(event)
if (result == null) return
const imagePath = result.getString('imagePath') ?? ''
const fileURL = result.getString('fileURL') ?? ''
previewSrc.value = fileURL.length > 0 ? fileURL : (imagePath.startsWith('/') ? 'file://' + imagePath : imagePath)
console.log(imagePath, result.getNumber('width'), result.getNumber('height'))
// 上传、保存仍使用本次返回的原始 imagePath / uploadFilePath。
}
function onError(event: any | null): void {
taking.value = false
const error = resultData(event)
uni.showToast({ title: error?.getString('errMsg') ?? '拍照失败', icon: 'none' })
}
</script>
<style>
.page { flex: 1; padding: 20px; }
.camera { width: 300px; height: 400px; }
.photo { width: 300px; height: 220px; }
</style>
组件挂载后自动准备预览并申请相机权限;收到 ready 后拍照。新照片以 captured 完成事件为准,不要轮询文件或在调用 takePhoto() 后立即读取路径。
第 3 步:权限与基座
| 平台 | 配置 | 项目需要做什么 |
|---|---|---|
| Android | 插件清单声明 android.permission.CAMERA,组件运行时申请 |
不必在项目重复添加;允许手机上的相机授权 |
| iOS | 插件 Info.plist 已提供 NSCameraUsageDescription |
可在项目根目录 Info.plist 按业务覆盖相机用途文案 |
拍照到应用自己的文件目录,不需要麦克风或相册权限。首次导入或更新原生代码后,重新制作并安装包含本插件的自定义基座,再运行页面。正式包同样包含本插件和相机用途说明。
第 4 步:打开完整演示页
在项目 pages.json 的 pages 数组中增加:
{
"path": "uni_modules/austin-camera-x/index",
"style": {
"navigationBarTitleText": "原生相机拍照",
"pageOrientation": "auto"
}
}
选择取景框和边框样式,点击画面对焦,再拍照。拍完可查看照片、像素和文件大小,点击照片可放大;“继续拍照”回到相机。对焦方式、缩放、质量、裁切和输出长边位于可展开的“拍照设置”。
| 演示页设置 | 修改位置 |
|---|---|
| 圆角方形、圆形、身份证、文档、银行卡、四角定位 | shapeOptions 的尺寸及圆角;只改变引导框与预览形状 |
| 边框颜色、粗细 | borderColorOptions / borderWidthOptions |
| 前后摄像头、闪光灯 | cameraPosition / flashMode |
| 输出质量、裁切、缩放 | 下方同名组件属性 |
六种取景框属于演示布局。业务自行设置组件的宽高和装饰边框即可;点击对焦由原生预览处理,覆盖在相机上的装饰层应设 :flatten="false" 和 pointer-events: none,让触摸传到原生预览。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
cameraPosition |
string |
front |
front 前摄、back 后摄;演示页显式使用 back |
flashMode |
string |
off |
off 关闭、on 开启、auto 自动;以摄像头实际能力为准 |
previewCornerRadiusRate |
number |
0.12 |
预览圆角与短边的比例,范围 0~0.5;圆形预览需等宽高并设置 0.5 |
focusMode |
string |
continuous |
continuous 持续对焦、auto 单次对焦、locked 设备支持的固定/锁定焦点 |
zoom |
number |
1 |
缩放倍率,范围 1~ready.maxZoom;超出设备范围时限制到可用值 |
quality |
number |
92 |
JPEG 压缩质量,1~100;不等同于照片像素或光学对焦效果 |
cropToPreview |
boolean |
false |
true 时按预览比例居中裁出矩形照片;false 输出完整照片 |
maxPhotoEdge |
number |
0 |
0 保留实际拍摄档位尺寸;正数限制输出最长边,只缩小、不放大。支持 0~32768 |
ready.pictureWidth / pictureHeight 是拍摄档位参考值,未应用旋转、裁切和缩小;iOS 取当前格式的高分辨率尺寸,系统未知时为 0。最终成片以拍照结果 width / height 为准。Android 默认选择不超过 1600 万像素的可用拍摄档位以控制内存,不能将“原尺寸”理解为固定使用传感器最高像素。
裁切根据预览比例居中处理,取景框不会自动识别文档边缘;圆形或圆角仅改变预览外观,JPEG 成片仍是矩形。前摄成片沿用原版行为:Android 不额外镜像,iOS 使用镜像输出。
组件方法
uni-app x 通过 ref.$callMethod() 调用:
cameraRef.value?.$callMethod('focusAt', 0.5, 0.5)
cameraRef.value?.$callMethod('updateZoom', 2)
| 方法 | 参数 | 返回 | 说明 |
|---|---|---|---|
startPreview() |
无 | void |
准备或恢复预览 |
stopPreview() |
无 | void |
停止预览;离开业务页面时调用。停止后修改属性不会自动恢复预览 |
takePhoto() |
无 | void |
拍照,等待完成或失败事件;同一时间只发起一次 |
switchCamera(position) |
front / back |
void |
切换摄像头 |
updateFlashMode(mode) |
off / on / auto |
void |
更新闪光灯模式 |
getCurrentPhotoPath() |
无 | string |
最近一张完成照片的路径,未完成时为空或保留上一张;新照片读取以 captured 为准 |
focusAt(x, y) |
两个 number |
void |
预览区域归一化坐标 0~1;左上角 (0,0)、右下角 (1,1);同时请求对焦及曝光点 |
updateZoom(value) |
number |
void |
更新缩放倍率,实际结果见 zoomchange |
事件
| 事件 | 数据 | 时机 |
|---|---|---|
ready |
下表中的摄像头能力 | 预览可用,可开始拍照;切换摄像头后重新获取能力 |
captured |
完成结果 | 新照片已生成并可读取,推荐使用此事件 |
capture / captureResult |
旧业务兼容的完成通知 | 避免与 captured 同时重复处理同一张照片 |
focus |
x、y、supported |
设置对焦点请求的反馈;supported 表示能力支持,不代表已完成光学对焦 |
zoomchange |
zoom、maxZoom |
实际缩放倍率及上限 |
error |
errCode、errMsg |
权限、配置、相机或拍照失败 |
focus 在平台支持时还会提供 pointSupported(指定区域)、meteringSupported(测光)、success(对焦回调结果)、timedOut(等待超时)。iOS 还会返回 exposureApplied(是否应用测光点);Android 可返回 errMsg(点击对焦请求错误),zoomchange.supported 表示是否支持变焦。字段缺省时不代表成功;固定焦距前摄无法通过软件增加光学对焦能力。
ready 能力字段
| 字段 | 类型 | 说明 |
|---|---|---|
cameraPosition |
string |
当前摄像头位置 |
flashSupported |
boolean |
是否具备闪光灯能力 |
focusSupported |
boolean |
是否支持对焦设置;部分前摄为固定焦距 |
zoomSupported |
boolean |
是否支持变焦 |
maxZoom |
number |
当前摄像头允许的最大倍率,最低为 1 |
pictureWidth / pictureHeight |
number |
当前拍摄档位参考像素,未知时为 0;成片尺寸见完成事件 |
zoom |
number |
当前实际缩放倍率 |
flashMode / focusMode |
string |
平台提供时返回实际应用的模式 |
tapFocusSupported |
boolean |
平台提供时表示是否支持指定对焦区域 |
拍照结果字段
| 字段 | 类型 | 说明 |
|---|---|---|
imagePath |
string |
原有照片路径字段,指向当前 App 可读取的成片 |
width / height |
number |
成片像素尺寸,已反映裁切与缩小 |
sizeBytes |
number |
成片字节数 |
cameraPosition |
string |
本次拍照使用的摄像头 |
flashMode |
string |
本次拍照使用的闪光灯设置 |
filePath / path |
string |
imagePath 的兼容别名 |
uploadFilePath |
string |
本地上传路径,可传给 uni.uploadFile.filePath |
fileURL |
string |
文件 URI,平台提供时可直接用于预览 |
relativePath |
string |
输出目录相对地址:Android 相对 App 的 filesDir,iOS 相对 Documents;不是网络地址 |
cropped |
boolean |
是否实际裁掉像素;启用裁切但原图比例已相同可为 false |
quality |
number |
本次成片使用的 JPEG 质量 |
zoom |
number |
平台提供时返回本次实际缩放倍率 |
扩展结果字段由原生端提供,以实际返回为准。只监听一个完成事件;需要兼容多个别名时按 imagePath 去重。
预览优先使用 fileURL;只有绝对路径时转换图片地址:
const previewSrc = imagePath.startsWith('/') ? 'file://' + imagePath : imagePath
上传仍使用本次返回的原始 imagePath / uploadFilePath。不要拼接 Pandora、AppID 或推测的沙盒目录,也不要将基座生成的旧绝对路径拿到另一次安装中复用。需要长期管理文件时,由业务保存相对标识并在当前应用目录中重新定位;插件不会自动恢复历史绝对地址。
错误码
| 错误码 | 说明 |
|---|---|
11001 |
相机权限未授权 |
11002 |
参数、预览初始化或拍照处理失败,查看 errMsg |
11003 |
当前平台或操作不支持 |
横竖屏
演示页随窗口调整排版,所有设置和拍照按钮均可滚动查看。uni-app x 拍照页的 style.pageOrientation 设为 auto 随设备旋转,固定横屏则用 landscape。
iOS 项目若只允许竖屏,在项目根目录 Info.plist 的 <dict> 内合并以下键,并重新打基座;iPad 对应使用 UISupportedInterfaceOrientations~ipad。已有该键时只补齐横屏项。
<key>UISupportedInterfaceOrientations~iphone</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>

收藏人数:
购买源码授权版(
试用
赞赏(0)
下载 642
赞赏 4
下载 12731366
赞赏 1959
赞赏
京公网安备:11010802035340号