diff --git a/CHANGELOG.md b/CHANGELOG.md index aa12430..16e3fa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. See [standa ## Version 2026.10.16-it-tools-pro +### ✨ 图片工具新增「图片编辑」:改图中文字 + 加文字 +- **改图中文字**:一键 OCR 自动定位图内文字(复用本机 rapidocr,带坐标),画布上画红色虚线框+文字标签;点框选中 → 输入新文字 → 「✓ 替换」自动采样框外圈中位色作底色覆盖原字,再在原位生成可编辑文字图层(字号按原文字高度推算,也可「只擦除」不加字) +- **涂改/加字**:画布拖拽框选任意区域按底色涂掉(自动取图/手动指定颜色);「+ 添加文字」放置新文字图层,支持字号/颜色/加粗 +- **图层面板**:所有文字图层(改字内容/字号/颜色/删除)和擦除区域(改底色/删除)统一列表管理,画布上可直接拖动文字调整位置 +- 执行后原尺寸导出(PNG 保 PNG,其他保 JPEG),擦除区与文字层逐像素合成 +- 图片工具功能数 12 → 13,About/关键词/中英文案同步 + ### 📈 「当天最热股票」改为「AI 最热股票」 - 股票列选项卡「最热」→「AI最热」,列标题 → 「AI最热股票」 - 数据源从「全市场涨幅榜」改为**聚合 12 个 AI 概念板块**(人工智能、AI 芯片、算力概念、AI 应用、AI 智能体、多模态 AI、AIGC 概念、CPO 概念、智谱 AI 概念、AI 语料、国产芯片、存储芯片),约 580 只成分股去重 diff --git a/locales/en.yml b/locales/en.yml index 7b56d56..0078de7 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -310,7 +310,7 @@ tools: image-tools: title: Image tools - description: "In-browser image toolbox: compress, convert, resize, crop, rotate, filters, mosaic/redaction, EXIF viewer, QR generate/scan, watermark and stitching. Images never leave your device." + description: "In-browser image toolbox: compress, convert, resize, crop, rotate, filters, mosaic/redaction, image editing (OCR text replacement / add text), EXIF viewer, QR generate/scan, watermark and stitching. Images never leave your device." pdf-tools: title: PDF tools diff --git a/locales/zh.yml b/locales/zh.yml index 71bd5e9..be45ca1 100644 --- a/locales/zh.yml +++ b/locales/zh.yml @@ -307,7 +307,7 @@ tools: image-tools: title: 图片工具 - description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。 + description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、图片编辑(OCR 改文字/加文字)、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。 pdf-tools: title: PDF工具 diff --git a/src/pages/About.vue b/src/pages/About.vue index 5e2c02c..355b276 100644 --- a/src/pages/About.vue +++ b/src/pages/About.vue @@ -53,7 +53,7 @@ const groups: { title: string; items: Item[] }[] = [ { name: '多图拼接', icon: '拼', desc: '横向/纵向长图/九宫格/瀑布流/交错双列/拍立得 6 种方式,网格类 2-4 列可调,间距可调', tag: '改造' }, { name: 'JSON 美化和格式化', icon: 'J', desc: '双栏专业工作台:左侧行号编辑,右侧树视图/语法高亮切换,含 Dict/Get参数/XML/CSV 等 7 种转换', tag: '改造' }, { name: 'PDF工具', icon: 'P', desc: '合并/拆分/旋转/重排/压缩/加密/解密/水印/提取文本/页码/转图片,11 功能全程浏览器本地处理,需授权使用', tag: '授权' }, - { name: '图片工具', icon: '图', desc: '压缩/转换/尺寸/裁剪/旋转/滤镜/打码/EXIF/二维码生成识别/水印/拼接,12 功能本地处理,需授权使用', tag: '授权' }, + { name: '图片工具', icon: '图', desc: '压缩/转换/尺寸/裁剪/旋转/滤镜/打码/图片编辑(OCR改文字+加文字)/EXIF/二维码生成识别/水印/拼接,13 功能本地处理,需授权使用', tag: '授权' }, ], }, { diff --git a/src/tools/image-tools/image-tools.vue b/src/tools/image-tools/image-tools.vue index 628f467..1fe3246 100644 --- a/src/tools/image-tools/image-tools.vue +++ b/src/tools/image-tools/image-tools.vue @@ -22,6 +22,7 @@ const TABS: TabDef[] = [ { key: 'rotate', label: '旋转', desc: '旋转或翻转图片' }, { key: 'filter', label: '滤镜', desc: '为图片应用滤镜效果(灰度/复古/模糊等)' }, { key: 'mosaic', label: '打码', desc: '在图片上拖拽框选区域,马赛克或涂黑打码' }, + { key: 'edit', label: '图片编辑', desc: '修改图中的文字(OCR 自动定位原文字,覆盖底色后替换)或点击图片任意位置添加新文字,可拖动调整' }, { key: 'exif', label: 'EXIF 查看', desc: '查看拍摄设备、时间、参数等 EXIF 信息' }, { key: 'qrgen', label: '二维码生成', desc: '把文字/链接生成为二维码图片', noFile: true }, { key: 'qrread', label: '二维码识别', desc: '识别图片中的二维码/条码内容' }, @@ -132,6 +133,7 @@ function loadMainImage() { qrResult.value = ''; decodeText.value = ''; mosaicBox.value = null; + resetEditState(); mainImg.value = null; const f = files.value[0]; if (!f || tab.value.noFile) { @@ -141,7 +143,10 @@ function loadMainImage() { const img = new Image(); img.onload = () => { mainImg.value = img; - nextTick(drawMosaicCanvas); + nextTick(() => { + drawMosaicCanvas(); + drawEditCanvas(); + }); }; img.src = url; } @@ -272,6 +277,466 @@ function mosaicPos(e: MouseEvent) { return { x: e.clientX - r.left, y: e.clientY - r.top }; } +// ---------- 图片编辑(改文字 + 加文字) ---------- +// 坐标约定:covers / layers / ocrHits 全部存原图像素坐标,渲染时乘 displayScale +interface EditCover { id: string; x: number; y: number; w: number; h: number; color: string } +interface EditLayer { id: string; text: string; x: number; y: number; size: number; color: string; bold: boolean } +interface OcrHit { id: string; text: string; score: number; box: number[][]; x: number; y: number; w: number; h: number; used: boolean } + +const editCanvas = ref(); +const editMode = useStorage<'ocr' | 'add'>('image-tools:edit-mode', 'ocr'); +const editCovers = ref([]); +const editLayers = ref([]); +const ocrHits = ref([]); +const ocrBusy = ref(false); +const ocrDone = ref(false); +const selLayerId = ref(null); +const selOcrId = ref(null); +const replaceText = ref(''); +const coverColor = ref('#ffffff'); +const coverAuto = ref(true); +const addText = useStorage('image-tools:edit-add-text', '新文字'); +const addSize = useStorage('image-tools:edit-add-size', 32); +const addColor = useStorage('image-tools:edit-add-color', '#d03050'); +const addBold = useStorage('image-tools:edit-add-bold', false); +let editDragging = false; +let editDragLayer: EditLayer | null = null; +let editDragOff = { x: 0, y: 0 }; +let editDrawing = false; +let editNewBox: { x1: number; y1: number; x2: number; y2: number } | null = null; + +const selLayer = computed(() => editLayers.value.find(l => l.id === selLayerId.value) ?? null); +const unusedOcr = computed(() => ocrHits.value.filter(h => !h.used)); + +function editPos(e: MouseEvent) { + const c = editCanvas.value!; + const r = c.getBoundingClientRect(); + return { x: e.clientX - r.left, y: e.clientY - r.top }; +} + +// 采样文字框外圈像素的中位色,作为覆盖底色(白底/灰底都自然) +function sampleBoxColor(box: { x: number; y: number; w: number; h: number }): string { + const img = mainImg.value; + if (!img) { + return coverColor.value; + } + try { + const c = document.createElement('canvas'); + c.width = img.naturalWidth; + c.height = img.naturalHeight; + const ctx = c.getContext('2d', { willReadFrequently: true })!; + ctx.drawImage(img, 0, 0); + const pad = Math.max(3, Math.round(Math.min(box.w, box.h) * 0.35)); + const sx = Math.max(0, Math.round(box.x - pad)); + const sy = Math.max(0, Math.round(box.y - pad)); + const sw = Math.min(c.width - sx, Math.round(box.w + pad * 2)); + const sh = Math.min(c.height - sy, Math.round(box.h + pad * 2)); + if (sw <= 0 || sh <= 0) { + return coverColor.value; + } + const d = ctx.getImageData(sx, sy, sw, sh).data; + const rs: number[] = []; + const gs: number[] = []; + const bs: number[] = []; + for (let py = 0; py < sh; py++) { + for (let px = 0; px < sw; px++) { + const ix = sx + px; + const iy = sy + py; + // 跳过框内(文字本身),只取外圈 + if (ix >= box.x && ix < box.x + box.w && iy >= box.y && iy < box.y + box.h) { + continue; + } + const i = (py * sw + px) * 4; + rs.push(d[i]); + gs.push(d[i + 1]); + bs.push(d[i + 2]); + } + } + if (!rs.length) { + return coverColor.value; + } + const med = (a: number[]) => { + a.sort((p, q) => p - q); + return a[a.length >> 1]; + }; + return `rgb(${med(rs)}, ${med(gs)}, ${med(bs)})`; + } + catch { + return coverColor.value; + } +} + +async function runEditOcr() { + const f = files.value[0]; + if (!f) { + errorMsg.value = '请先选择图片'; + return; + } + ocrBusy.value = true; + errorMsg.value = ''; + try { + const form = new FormData(); + form.append('image_file', f, f.name || 'image.png'); + const res = await fetch('/api/ocr', { method: 'POST', body: form, headers: licenseHeaders('image-tools') }); + if (!res.ok) { + throw new Error(res.status === 403 ? 'OCR 需要授权或试用' : `OCR 服务 HTTP ${res.status}`); + } + const data = await res.json().catch(() => ({})); + const hits: OcrHit[] = []; + for (const k of Object.keys(data)) { + const pts = data[k]?.dt_boxes; + if (!Array.isArray(pts) || pts.length < 4) { + continue; + } + const text = String(data[k]?.rec_txt ?? '').trim(); + if (!text) { + continue; + } + const xs = pts.map((p: any) => Number(p[0])); + const ys = pts.map((p: any) => Number(p[1])); + const x = Math.min(...xs); + const y = Math.min(...ys); + hits.push({ + id: `ocr-${k}`, + text, + score: Number(data[k]?.score ?? 0), + box: pts, + x, + y, + w: Math.max(...xs) - x, + h: Math.max(...ys) - y, + used: false, + }); + } + ocrHits.value = hits; + ocrDone.value = true; + if (!hits.length) { + errorMsg.value = '未识别到文字,可截图更清晰后重试,或用「添加文字」模式'; + } + else { + statusMsg.value = `识别到 ${hits.length} 处文字,点击虚线框或列表项即可替换`; + genHist.add({ title: `编辑 OCR:${f.name}`, detail: `${hits.length} 处文字` }); + } + nextTick(drawEditCanvas); + } + catch (e: any) { + errorMsg.value = `识别失败:${e?.message ?? e}`; + ocrDone.value = false; + nextTick(drawEditCanvas); + } + finally { + ocrBusy.value = false; + } +} + +// 选中一处 OCR 文字:盖底色 + 在原位创建可编辑文字图层(内容预填原文) +function applyOcrReplace(hit: OcrHit) { + if (hit.used) { + return; + } + const color = coverAuto.value ? sampleBoxColor({ x: hit.x, y: hit.y, w: hit.w, h: hit.h }) : coverColor.value; + const padX = Math.max(1, Math.round(hit.w * 0.02)); + const padY = Math.max(1, Math.round(hit.h * 0.08)); + editCovers.value.push({ + id: `c${Date.now()}${Math.random().toString(36).slice(2, 6)}`, + x: Math.max(0, hit.x - padX), + y: Math.max(0, hit.y - padY), + w: hit.w + padX * 2, + h: hit.h + padY * 2, + color, + }); + const layer: EditLayer = { + id: `l${Date.now()}${Math.random().toString(36).slice(2, 6)}`, + text: replaceText.value.trim() || hit.text, + x: hit.x, + y: hit.y + hit.h * 0.06, + size: Math.max(10, Math.round(hit.h * 0.82)), + color: '#000000', + bold: false, + }; + editLayers.value.push(layer); + hit.used = true; + selLayerId.value = layer.id; + selOcrId.value = null; + replaceText.value = ''; + coverColor.value = color.startsWith('rgb') ? '#ffffff' : color; + nextTick(drawEditCanvas); +} + +// 仅擦除(盖底色,不加字) +function applyOcrErase(hit: OcrHit) { + if (hit.used) { + return; + } + const color = coverAuto.value ? sampleBoxColor({ x: hit.x, y: hit.y, w: hit.w, h: hit.h }) : coverColor.value; + const padX = Math.max(1, Math.round(hit.w * 0.02)); + const padY = Math.max(1, Math.round(hit.h * 0.08)); + editCovers.value.push({ + id: `c${Date.now()}${Math.random().toString(36).slice(2, 6)}`, + x: Math.max(0, hit.x - padX), + y: Math.max(0, hit.y - padY), + w: hit.w + padX * 2, + h: hit.h + padY * 2, + color, + }); + hit.used = true; + selOcrId.value = null; + nextTick(drawEditCanvas); +} + +function removeCover(id: string) { + editCovers.value = editCovers.value.filter(c => c.id !== id); + drawEditCanvas(); +} + +function removeLayer(id: string) { + editLayers.value = editLayers.value.filter(l => l.id !== id); + if (selLayerId.value === id) { + selLayerId.value = null; + } + drawEditCanvas(); +} + +function resetEditState() { + editCovers.value = []; + editLayers.value = []; + ocrHits.value = []; + ocrDone.value = false; + selLayerId.value = null; + selOcrId.value = null; + replaceText.value = ''; + editNewBox = null; +} + +function drawEditCanvas() { + const c = editCanvas.value; + const img = mainImg.value; + if (!c || !img) { + return; + } + const maxW = 640; + const scale = Math.min(1, maxW / img.naturalWidth); + displayScale.value = scale; + c.width = Math.round(img.naturalWidth * scale); + c.height = Math.round(img.naturalHeight * scale); + const ctx = c.getContext('2d')!; + ctx.drawImage(img, 0, 0, c.width, c.height); + // 1) 底色覆盖(擦除原文字) + for (const cv of editCovers.value) { + ctx.fillStyle = cv.color; + ctx.fillRect(cv.x * scale, cv.y * scale, cv.w * scale, cv.h * scale); + } + // 2) 未处理的 OCR 虚线框 + for (const h of ocrHits.value) { + if (h.used) { + continue; + } + const selected = h.id === selOcrId.value; + ctx.strokeStyle = selected ? '#2563eb' : '#d03050'; + ctx.lineWidth = selected ? 2.5 : 1.5; + ctx.setLineDash([6, 4]); + ctx.strokeRect(h.x * scale, h.y * scale, h.w * scale, h.h * scale); + ctx.setLineDash([]); + // 框顶小标签 + const label = h.text.length > 12 ? `${h.text.slice(0, 12)}…` : h.text; + ctx.font = `${Math.max(10, Math.round(11 / Math.max(scale, 0.4)))}px sans-serif`; + const tw = ctx.measureText(label).width + 8; + const ly = h.y * scale - 16; + ctx.fillStyle = selected ? '#2563eb' : 'rgba(208,48,80,0.92)'; + ctx.fillRect(h.x * scale, ly < 0 ? h.y * scale : ly, tw, 15); + ctx.fillStyle = '#fff'; + ctx.textBaseline = 'middle'; + ctx.fillText(label, h.x * scale + 4, (ly < 0 ? h.y * scale : ly) + 8); + } + // 3) 文字图层 + for (const l of editLayers.value) { + ctx.font = `${l.bold ? 'bold ' : ''}${l.size * scale}px "Microsoft YaHei","PingFang SC","Noto Sans CJK SC",sans-serif`; + ctx.fillStyle = l.color; + ctx.textBaseline = 'top'; + ctx.fillText(l.text, l.x * scale, l.y * scale); + if (l.id === selLayerId.value) { + const w = ctx.measureText(l.text).width; + ctx.strokeStyle = '#18a058'; + ctx.lineWidth = 1.5; + ctx.setLineDash([5, 3]); + ctx.strokeRect(l.x * scale - 4, l.y * scale - 3, w + 8, l.size * scale * 1.25 + 6); + ctx.setLineDash([]); + } + } + // 4) 拖拽框选预览(add 模式手动框选覆盖) + if (editNewBox) { + const b = editNewBox; + ctx.strokeStyle = '#d03050'; + ctx.lineWidth = 2; + ctx.setLineDash([6, 4]); + ctx.strokeRect(Math.min(b.x1, b.x2), Math.min(b.y1, b.y2), Math.abs(b.x2 - b.x1), Math.abs(b.y2 - b.y1)); + ctx.setLineDash([]); + } +} + +function layerHitTest(ix: number, iy: number): EditLayer | null { + const c = editCanvas.value; + if (!c) { + return null; + } + const ctx = c.getContext('2d')!; + for (let i = editLayers.value.length - 1; i >= 0; i--) { + const l = editLayers.value[i]; + ctx.font = `${l.bold ? 'bold ' : ''}${l.size}px "Microsoft YaHei","PingFang SC",sans-serif`; + const w = ctx.measureText(l.text).width; + const h = l.size * 1.25; + if (ix >= l.x - 6 && ix <= l.x + w + 6 && iy >= l.y - 6 && iy <= l.y + h + 6) { + return l; + } + } + return null; +} + +function ocrHitTest(ix: number, iy: number): OcrHit | null { + for (const h of ocrHits.value) { + if (!h.used && ix >= h.x - 3 && ix <= h.x + h.w + 3 && iy >= h.y - 3 && iy <= h.y + h.h + 3) { + return h; + } + } + return null; +} + +function onEditDown(e: MouseEvent) { + if (!editCanvas.value) { + return; + } + const p = editPos(e); + const s = displayScale.value; + const ix = p.x / s; + const iy = p.y / s; + // 1) 点到已有文字图层 → 选中并拖动 + const lh = layerHitTest(ix, iy); + if (lh) { + selLayerId.value = lh.id; + editDragging = true; + editDragLayer = lh; + editDragOff = { x: ix - lh.x, y: iy - lh.y }; + drawEditCanvas(); + return; + } + // 2) OCR 模式:点到虚线框 → 替换 + if (editMode.value === 'ocr') { + const oh = ocrHitTest(ix, iy); + if (oh) { + selOcrId.value = oh.id; + replaceText.value = ''; + drawEditCanvas(); + return; + } + return; + } + // 3) add 模式空白处:开始框选(松手后按框擦除)或直接加字 + if (editMode.value === 'add') { + editDrawing = true; + editNewBox = { x1: p.x, y1: p.y, x2: p.x, y2: p.y }; + } +} + +function onEditMove(e: MouseEvent) { + if (!editCanvas.value) { + return; + } + const p = editPos(e); + const s = displayScale.value; + if (editDragging && editDragLayer) { + editDragLayer.x = Math.max(0, Math.min(editCanvas.value.width / s, p.x / s - editDragOff.x)); + editDragLayer.y = Math.max(0, Math.min(editCanvas.value.height / s, p.y / s - editDragOff.y)); + drawEditCanvas(); + return; + } + if (editDrawing && editNewBox) { + editNewBox.x2 = p.x; + editNewBox.y2 = p.y; + drawEditCanvas(); + } +} + +function onEditUp() { + if (editDragging) { + editDragging = false; + editDragLayer = null; + return; + } + if (editDrawing && editNewBox) { + const b = editNewBox; + editDrawing = false; + editNewBox = null; + const s = displayScale.value; + const x = Math.round(Math.min(b.x1, b.x2) / s); + const y = Math.round(Math.min(b.y1, b.y2) / s); + const w = Math.round(Math.abs(b.x2 - b.x1) / s); + const h = Math.round(Math.abs(b.y2 - b.y1) / s); + if (w >= 6 && h >= 6) { + editCovers.value.push({ + id: `c${Date.now()}${Math.random().toString(36).slice(2, 6)}`, + x, + y, + w, + h, + color: coverAuto.value ? sampleBoxColor({ x, y, w, h }) : coverColor.value, + }); + } + drawEditCanvas(); + } +} + +function addTextLayer() { + const img = mainImg.value; + const text = addText.value.trim(); + if (!img) { + return; + } + if (!text) { + errorMsg.value = '请输入要添加的文字'; + return; + } + errorMsg.value = ''; + const layer: EditLayer = { + id: `l${Date.now()}${Math.random().toString(36).slice(2, 6)}`, + text, + x: Math.round(img.naturalWidth * 0.08), + y: Math.round(img.naturalHeight * 0.08), + size: addSize.value, + color: addColor.value, + bold: addBold.value, + }; + editLayers.value.push(layer); + selLayerId.value = layer.id; + nextTick(drawEditCanvas); +} + +async function doEdit() { + const img = mainImg.value!; + if (!editCovers.value.length && !editLayers.value.length) { + throw new Error('还没有任何修改:先「识别文字」点框替换,或在「添加文字」模式下框选/加字'); + } + const c = loadCanvasFromImg(img); + const ctx = c.getContext('2d')!; + for (const cv of editCovers.value) { + ctx.fillStyle = cv.color; + ctx.fillRect(cv.x, cv.y, cv.w, cv.h); + } + for (const l of editLayers.value) { + ctx.font = `${l.bold ? 'bold ' : ''}${l.size}px "Microsoft YaHei","PingFang SC","Noto Sans CJK SC","Source Han Sans SC",sans-serif`; + ctx.fillStyle = l.color; + ctx.textBaseline = 'top'; + ctx.fillText(l.text, l.x, l.y); + } + const keepPng = files.value[0].type === 'image/png'; + const blob = await canvasToBlob(c, keepPng ? 'image/png' : 'image/jpeg', 94); + setOutput(blob, `${baseName(files.value[0])}-edited.${extOf(blob.type)}`, { + title: `编辑 ${files.value[0].name}`, + detail: `擦除 ${editCovers.value.length} 处 · 文字 ${editLayers.value.length} 个`, + }); + resultDim.value = `${c.width}×${c.height}`; +} + + function onMosaicDown(e: MouseEvent) { if (tab.value.key !== 'mosaic') { return; @@ -336,6 +801,9 @@ async function execute() { else if (k === 'mosaic') { await doMosaic(); } + else if (k === 'edit') { + await doEdit(); + } else if (k === 'exif') { await doExif(); } @@ -856,7 +1324,10 @@ function fit() { rootStyle.value = { height: `${height}px`, flex: '1 1 100%' }; } -watch(tabKey, () => nextTick(drawMosaicCanvas)); +watch(tabKey, () => nextTick(() => { + drawMosaicCanvas(); + drawEditCanvas(); +})); onMounted(() => { fit(); @@ -923,6 +1394,27 @@ onUnmounted(() => { /> + +