更新记录
1.0.0(2026-06-30)
新增
xviewWatermark回调 API、xviewWatermarkAsyncPromise API。plain纯文本块 /card卡片水印,支持五位置、背景、圆角、旋转、输出格式与质量等参数。- 卡片扩展:分区背景、标题图片、左侧竖线、长文本换行。
- 平台实现:App Android / iOS / HarmonyOS、Web、微信小程序。
- UTS 类型定义、统一错误码(9020001–9020005)。
- uni-app / uni-app x 示例页(含异步调用示例)。
修复
- Web / H5、微信小程序
UniError模块加载兼容;小程序圆角绘制兼容。 - App Android 半透明背景色显示。
平台兼容性
uni-app(5.06)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | √ | √ | √ | √ | 5.0 | 14 | √ |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| √ | × | × | × | × | × | - | × | × | × | - | - |
uni-app x(5.06)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| √ | √ | 5.0 | 14 | √ | √ |
其他
| 多语言 | 暗黑模式 | 宽屏模式 |
|---|---|---|
| √ | √ | √ |
xview-watermark
跨平台图片水印 UTS 插件。将本地图片渲染为带文字水印的新图片,适用于现场采集、工程巡检、业务留痕等场景。
提供两种调用方式:
| API | 风格 | 适用场景 |
|---|---|---|
xviewWatermark |
回调(success / fail / complete) | 与现有 uni API 风格一致 |
xviewWatermarkAsync |
Promise(async/await) | 现代异步流程、链式组合 |
同一调用请二选一,不要混用 success 与 await。
特性
- 一套 API 覆盖 App 原生、Web、微信小程序;uni-app / uni-app x 均可调用。
plain纯文本块 /card卡片水印,五位置、背景、圆角、旋转、输出格式与质量。- 卡片扩展:标题 / 内容 / 页脚分区、标题图片、左侧竖线、长文本换行。
- 完整 UTS 类型定义与统一错误码(9020001–9020005)。
安装
将 uni_modules/xview-watermark 复制到项目 uni_modules 目录。App 端修改插件后需重新制作自定义基座或云打包。
快速开始
回调方式
import { xviewWatermark } from '@/uni_modules/xview-watermark'
uni.chooseImage({
count: 1,
success: (chooseRes) => {
xviewWatermark({
sourcePath: chooseRes.tempFilePaths[0],
position: 'bottomRight',
lines: [
{ label: '', text: 'xview-watermark', textFontSize: 34, textColor: '#FFFFFF', textStyle: 'bold' },
{ label: '时间', text: '2026-06-30 10:30', labelFontSize: 24, labelColor: '#BAE6FD', textFontSize: 24, textColor: '#E5E7EB' }
],
success: (res) => console.log(res.tempFilePath),
fail: (err) => console.log(err.errCode, err.errMsg)
})
}
})
Promise 方式
import { xviewWatermarkAsync } from '@/uni_modules/xview-watermark'
try {
const res = await xviewWatermarkAsync({
sourcePath: imagePath,
layout: 'card',
cardWidth: 520,
lines: [
{ label: '', text: '工程巡检', section: 'header', textFontSize: 34, textColor: '#FFFFFF', textStyle: 'bold' },
{ label: '施工区域', text: 'A 区演示楼', section: 'content', labelFontSize: 24, textColor: '#111827' }
]
})
console.log(res.tempFilePath)
} catch (err) {
console.log(err.errCode, err.errMsg)
}
API
xviewWatermark(options: XviewWatermarkOptions): void
回调版。成功走 success,失败走 fail,无论成败均触发 complete。
xviewWatermarkAsync(options: XviewWatermarkAsyncOptions): Promise\<XviewWatermarkResult>
Promise 版。参数与回调版相同,不含 success / fail / complete;失败时 reject,错误对象与 fail 回调一致。
| 回调 | Async |
|---|---|
success(res) |
await 返回值 / then(res) |
fail(err) |
catch(err) |
complete() |
finally() |
参数说明
主参数
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| sourcePath | string | 必填 | 原图路径(chooseImage、绝对路径、file://、content://) |
| lines | Array | 必填 | 水印文字行,见 lines |
| outputDir | string | App 缓存 | 输出目录;Web / 小程序不强制指定 |
| fileName | string | 自动生成 | 默认 xview_watermark_时间戳 |
| format | jpg / jpeg / png | jpg | jpeg 统一映射为 jpg |
| quality | number | 92 | JPG 质量 1–100;PNG 忽略 |
| position | string | bottomRight | topLeft / topRight / bottomLeft / bottomRight / center |
| marginX / marginY | number | 28 | 水印块外边距,px |
| paddingX / paddingY | number | 20 / 14 | 背景内边距,px |
| lineSpacing | number | 8 | 行间距,px |
| backgroundColor | string | #99000000 | 支持 #AARRGGBB、#RRGGBB |
| backgroundRadius | number | 12 | 背景圆角,px |
| rotate | number | 0 | 整体旋转角度 |
| layout | plain / card | plain | 布局模式 |
| cardWidth | number | 0 | 卡片固定宽度;card 布局且 > 0 时生效 |
| headerBackgroundColor | string | #8FB9F0 | 卡片标题区背景 |
| contentBackgroundColor | string | #EDEBE6 | 卡片内容区背景 |
| footerBackgroundColor | string | #8FB9F0 | 卡片页脚区背景 |
| headerImagePath | string | - | 标题区左侧图片 |
| headerImageSize | number | 44 | 标题图片尺寸,px |
| headerImageGap | number | 14 | 标题图片与文字间距,px |
| leftLineWidth | number | 0 | 左侧竖线宽度;> 0 显示 |
| leftLineColor | string | #22D3EE | 左侧竖线颜色 |
| success / fail / complete | Function | - | 仅 xviewWatermark 使用 |
lines 配置
每行必填 label、text(无标签时 label 传 '');两者皆空则忽略该行。
| 字段 | 默认 | 说明 |
|---|---|---|
| label / text | 必填 | 标签与内容;冒号由插件绘制 |
| labelColor / labelFontSize / labelStyle | 跟随 text | 标签样式 |
| textColor / textFontSize / textStyle | #FFFFFF / 30 / normal | 内容样式 |
| section | content | header / content / footer,card 布局生效 |
排版规则: 换行仅发生在 text 内容列,不会回流到 label 下方。
布局模式
| 模式 | 说明 |
|---|---|
| plain | 纯文本块(默认) |
| card | 标题 / 内容 / 页脚卡片,配合 section、分区背景色等 |
代码示例
完整可运行代码见仓库示例页。下表为场景与关键参数对照:
| 场景 | 关键参数 | 示例页 |
|---|---|---|
| 基础调用 | lines |
watermark/basic |
| 水印位置 | position、marginX/Y |
watermark/position |
| 背景圆角 | backgroundColor、backgroundRadius、paddingX/Y |
watermark/params |
| 逐行样式 | lines 内 label/text 样式字段 |
watermark/params |
| 旋转 | rotate |
watermark/params |
| 输出格式 | format、quality、fileName |
watermark/output |
| 卡片分区 | layout: 'card'、section、分区背景色 |
watermark/card-section |
| 长文本换行 | cardWidth |
watermark/wrap |
| 左侧竖线 | leftLineWidth、leftLineColor |
watermark/card-line |
| 标题图标 | headerImagePath、headerImageSize |
watermark/card-icon |
| 综合留痕 | 多参数组合 | watermark/style |
| 异步调用 | xviewWatermarkAsync |
watermark/async |
卡片 + 竖线示例:
xviewWatermark({
sourcePath: imagePath,
layout: 'card',
cardWidth: 520,
position: 'bottomLeft',
leftLineWidth: 6,
leftLineColor: '#22D3EE',
headerBackgroundColor: '#CC8FB9F0',
contentBackgroundColor: '#CCEDEBE6',
lines: [
{ label: '', text: '工程巡检', section: 'header', textFontSize: 34, textColor: '#FFFFFF', textStyle: 'bold' },
{ label: '拍摄时间', text: '2026-06-30 11:56', section: 'content', labelFontSize: 24, textColor: '#E5E7EB' }
],
success: (res) => console.log(res.tempFilePath)
})
返回值
type XviewWatermarkResult = {
tempFilePath: string // 输出路径(Web 为 dataURL)
width: number
height: number
size: number // 字节数
format: string // jpg 或 png
}
错误码
fail / catch 接收 XviewWatermarkFail,errSubject 固定为 xview-watermark:
| 码 | 说明 |
|---|---|
| 9020001 | 当前平台不支持 |
| 9020002 | sourcePath 为空 |
| 9020003 | 水印文字为空 |
| 9020004 | 图片读写失败 |
| 9020005 | Android 上下文不可用 |
平台差异与注意事项
App
- 修改插件后需重新打包自定义基座。
- Android 读取相册需声明
READ_MEDIA_IMAGES或READ_EXTERNAL_STORAGE。 - 错误对象继承
UniError;半透明色请使用#AARRGGBB格式。
Web
- 远程图片需满足 CORS;输出为 dataURL,大图注意内存。
- 错误对象实现
XviewWatermarkFail接口(编译为 JS,无全局UniError)。
微信小程序
- 依赖 OffscreenCanvas;低版本基础库可能返回 9020001。
- 圆角矩形使用手动路径绘制,兼容开发者工具与真机差异。
- 控制台
webapi_getwxaasyncsecinfo:fail为开发者工具环境问题,与插件无关。
通用
- 多端共用同一组公开参数;
plain/card、分区、竖线、换行等行为保持一致。 - 业务侧从插件根目录 import,勿直接引用
utssdk/内部文件。
示例工程
| 框架 | 首页 | 示例目录 |
|---|---|---|
| uni-app | pages/index/index.vue |
pages/watermark/*.vue |
| uni-app x | pagesx/index/index.uvue |
pagesx/watermark/*.uvue |
示例覆盖选图预览、位置切换、参数矩阵、卡片样式、异步调用与 uni.previewImage 大图查看。
类型导出
utssdk/interface.uts 导出:
XviewWatermarkOptions · XviewWatermarkAsyncOptions · XviewWatermarkTextLine · XviewWatermarkResult · XviewWatermarkFail · XviewWatermarkPosition · XviewWatermarkFormat · XviewWatermarkLayout · XviewWatermarkTextSection · XviewWatermarkTextStyle · XviewWatermark · XviewWatermarkAsync

收藏人数:
购买源码授权版(
试用
使用 HBuilderX 导入示例项目
赞赏(0)
下载 8
赞赏 0
下载 12371425
赞赏 1927
赞赏
京公网安备:11010802035340号