diff --git a/zdxt-web-client/zdxt-efcode-enterprise-app/src/util/w6sMedia.js b/zdxt-web-client/zdxt-efcode-enterprise-app/src/util/w6sMedia.js index e612859d..5a26de1b 100644 --- a/zdxt-web-client/zdxt-efcode-enterprise-app/src/util/w6sMedia.js +++ b/zdxt-web-client/zdxt-efcode-enterprise-app/src/util/w6sMedia.js @@ -151,9 +151,79 @@ const formatLocationText = (loc) => { return [address, coords].filter(Boolean).join(' ') } -// 拍照并加水印(w6s 原生接口 takePhotoAndAddWaterMark) -// 时间与定位由前端拼入 content,timeEnable/locationEnable 置 false, -// 跳过 SDK 内部再次调 getLocation(该调用易超时阻塞拍照流程) +// 去掉 file:// 前缀(w6s 的 toBase64 在 Android 下会自动补 file://,避免重复前缀导致解析失败) +const stripFileScheme = (p = '') => String(p).replace(/^file:\/\//, '') + +// 水印图最长边上限(像素)。原图往往 4000+,整张重新编码是耗时主因, +// 限制到 2048 可大幅缩短耗时并减小上传体积;需要更高清可上调 +const WATERMARK_MAX_SIZE = 2048 + +// 读取水印源图:优先用本地文件 URL(cdvfile:// 由 Cordova 拦截读取),无需把整张图转成 base64; +// 环境/路径不支持时回退到 SDK 的 toBase64 +const loadWatermarkSource = async (filePath) => { + const toImg = (src) => new Promise((resolve, reject) => { + const el = document.createElement('img') + el.onload = () => resolve(el) + el.onerror = () => reject(new Error('水印源图片加载失败')) + el.src = src + }) + const raw = stripFileScheme(filePath) + if (window.resolveLocalFileSystemURL) { + const fileUrl = /^(cdvfile|content):/i.test(String(filePath)) ? filePath : `file://${raw}` + try { + const entry = await new Promise((resolve, reject) => window.resolveLocalFileSystemURL(fileUrl, resolve, reject)) + return await toImg(entry.toURL()) + } catch (e) { + console.warn('[w6sMedia] 本地文件 URL 加载失败, 回退 toBase64:', e) + } + } + const evt = await w6s.image.toBase64({ filePath: raw }) + return toImg(evt?.target?.result || evt?.result || evt) +} + +// 前端给图片叠加水印(时间 + 定位),返回带水印图片的本地文件路径 +// 说明:w6s.image.takePhotoAndAddWaterMark 实测不可用(原生加水印接口无有效返回), +// 改为拍照后用 canvas 在前端叠加水印;用 toBlob 直接落盘,省去 base64 编解码开销 +const addWaterMark = async (filePath, fileName, text) => { + console.log('[w6sMedia] 开始叠加水印, 源图:', filePath, '水印内容:', text) + const img = await loadWatermarkSource(filePath) + + // 按最长边等比缩放,避免整张原图重新编码(编码是耗时主因) + const srcW = img.naturalWidth || img.width + const srcH = img.naturalHeight || img.height + const scale = Math.min(1, WATERMARK_MAX_SIZE / Math.max(srcW, srcH)) + const canvas = document.createElement('canvas') + canvas.width = Math.round(srcW * scale) + canvas.height = Math.round(srcH * scale) + const ctx = canvas.getContext('2d') + ctx.drawImage(img, 0, 0, canvas.width, canvas.height) + + const fontSize = Math.max(16, Math.round(canvas.width * 0.035)) + const pad = Math.round(fontSize * 0.6) + ctx.font = `${fontSize}px sans-serif` + ctx.textAlign = 'left' + ctx.textBaseline = 'bottom' + // 描边提升浅色背景下的可读性 + ctx.lineWidth = Math.max(2, Math.round(fontSize * 0.12)) + ctx.strokeStyle = 'rgba(0, 0, 0, 0.6)' + ctx.fillStyle = '#FF5858' + const x = pad + const y = canvas.height - pad + ctx.strokeText(text, x, y) + ctx.fillText(text, x, y) + + // toBlob 直接产出二进制,避免 toDataURL 的 base64 编码 + const blob = await new Promise((resolve, reject) => { + canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('水印图片编码失败'))), 'image/jpeg', 0.9) + }) + const out = await writeBlobToLocalFile(blob, fileName) + console.log('[w6sMedia] 水印叠加完成, 输出:', out, '大小:', blob.size) + return out +} + +// 拍照并加水印(时间 + 定位由前端拼好后用 canvas 叠加) +// 注:w6s.image.takePhotoAndAddWaterMark 原生接口无有效返回(拍照后一直无数据), +// 因此改为 w6s.image.takePicture + 前端 canvas 叠加水印 export const takePhotoWithWaterMark = async () => { await ensureW6sAuth() console.log('[w6sMedia] 鉴权完成, 准备获取定位') @@ -173,24 +243,28 @@ export const takePhotoWithWaterMark = async () => { const content = [getTimeStr(), locationText].filter(Boolean).join(' ') console.log('[w6sMedia] 水印 content:', content, '准备调起拍照') - return new Promise((resolve, reject) => { - w6s.image.takePhotoAndAddWaterMark({ - content, - fontSize: 14, - color: '#FF5858', - markDisable: false, - timeEnable: false, // 时间已拼入 content - locationEnable: false, // 定位已拼入 content,跳过 SDK 内部 getLocation - success: (res) => { - console.log('[w6sMedia] takePhotoAndAddWaterMark success 原始返回:', res) - resolve(res?.result || res || {}) - }, - fail: (err) => { - console.error('[w6sMedia] takePhotoAndAddWaterMark fail:', err) - reject(err) - }, + // 1. 调起原生拍照,返回 { imageURL(压缩图本地路径), key(原图本地路径), imageInfo, mediaId } + const photo = await w6s.image.takePicture({ editable: false }) + .then((res) => { + console.log('[w6sMedia] takePicture success 原始返回:', res) + const result = res?.result || res || {} + console.log('[w6sMedia] 拍照返回 imageURL:', result?.imageURL, 'key:', result?.key) + return result }) - }) + .catch((err) => { + console.error('[w6sMedia] takePicture fail:', err) + throw err + }) + + // 2. 前端叠加水印,产出带水印的图片本地路径 + const sourcePath = photo.imageURL || photo.key + if (!sourcePath) { + throw new Error('拍照未返回图片路径') + } + const imageURL = await addWaterMark(sourcePath, `wm-photo-${Date.now()}.jpg`, content) + + // 3. 返回结构与原来一致(imageURL 为带水印图片的本地路径,可直接上传) + return { ...photo, imageURL } } // 录制视频(w6s 官方 API,文档 https://open.beeworks.cn/js-sdk/video.html)