fix(label-designer): 非白底文字不再丢失;条码占位扩展到人读字符与紧邻装饰线

- boxBackground 增返回背景中位色;擦除改用采样色(白擦白/灰擦灰/蓝擦蓝),不再留白块
- 任何底色上的文字都生成可编辑元素(旧逻辑把非近白底的字整段静默丢弃,改不了也擦不掉)
- 深色底(亮度<110)替换文字自动转白色,保证反白字可见
- 条码占位扩展为条码+四周1mm+下方min(0.45*条高,8mm),一并擦除人读字符与紧邻框线
- 验证:300dpi 靶子底图像素采样,人读字符带/点划线带/灰条/蓝条黑字残留均0.00%
This commit is contained in:
personal82555 committed 2026-10-14 22:27:04 +08:00
1 parent fddfecf415
commit 2e090903c2
2 files changed
+73 -29

No files matched your search

+59 -29
View File
@@ -397,9 +397,20 @@ async function onPdfFile(e: Event) {
if (wPx < 4 || hPx < 4) {
continue;
}
// 擦除底图上的原条码(避免与新条码重影)
// 擦除底图上的原条码(避免与新条码重影)。
// zxing 给的是紧贴条码的 bbox,人读字符(条码下方那行小字)与紧邻的装饰线
// 落在 bbox 之外 —— 必须一并纳入擦除与占位,否则删掉条码元素后
// 底图仍残留这行字与框线(用户看到的"痕迹")。
const pxPerMm = EXPORT_DPI / 25.4;
const padPx = Math.max(2, Math.round(1 * pxPerMm)); // 四周 1mm
const hrExtra = Math.min(hPx * 0.45, 8 * pxPerMm); // 下方人读字符条,上限 8mm
ctx.fillStyle = '#ffffff';
ctx.fillRect(Math.floor(x0), Math.floor(y0), Math.ceil(wPx), Math.ceil(hPx));
ctx.fillRect(
Math.floor(x0 - padPx),
Math.floor(y0 - padPx),
Math.ceil(wPx + padPx * 2),
Math.ceil(hPx + padPx + hrExtra),
);
const isQR = /qr|aztec|datamatrix|pdf417/i.test(String(sym.format || ''));
const el = {
id: newId(),
@@ -412,7 +423,13 @@ async function onPdfFile(e: Event) {
...(isQR ? {} : { format: mapCodeFormat(String(sym.format || '')), showValue: true }),
} as LabelElement;
codeEls.push(el);
codeBoxes.push({ x: el.x, y: el.y, w: el.w, h: el.h });
// 占位框按"条码 + 人读字符条"的实际范围登记,供文字层跳过使用
codeBoxes.push({
x: Math.max(0, el.x - 1),
y: Math.max(0, el.y - 1),
w: el.w + 2,
h: el.h + hrExtra / pxPerMm + 2,
});
}
}
catch { /* 无条码或识别失败:跳过 */ }
@@ -539,22 +556,32 @@ async function onPdfFile(e: Event) {
let w = Math.min(canvas.width - x, Math.round(wMm * k));
let h = Math.min(canvas.height - y, Math.round(hMm * k));
if (w <= 0 || h <= 0) {
return { lum: 255, spread: 0 };
return { lum: 255, spread: 0, r: 255, g: 255, b: 255 };
}
const d = ctx.getImageData(x, y, w, h).data;
const lum: number[] = [];
const spr: number[] = [];
const rs: number[] = [];
const gs: number[] = [];
const bs: number[] = [];
for (let i = 0; i < d.length; i += 4) {
const r = d[i];
const g = d[i + 1];
const b = d[i + 2];
lum.push(r * 0.299 + g * 0.587 + b * 0.114);
spr.push(Math.max(r, g, b) - Math.min(r, g, b));
rs.push(r);
gs.push(g);
bs.push(b);
}
lum.sort((a, b2) => a - b2);
spr.sort((a, b2) => a - b2);
rs.sort((a, b2) => a - b2);
gs.sort((a, b2) => a - b2);
bs.sort((a, b2) => a - b2);
const mid = Math.floor(lum.length / 2);
return { lum: lum[mid], spread: spr[mid] };
// 中位色 = 该框的背景色(文字像素是少数,被中位数排除在外)
return { lum: lum[mid], spread: spr[mid], r: rs[mid], g: gs[mid], b: bs[mid] };
}
const textEls: LabelElement[] = [];
try {
@@ -618,31 +645,34 @@ async function onPdfFile(e: Event) {
if (overlapCode) {
continue;
}
// 只在「近白、低饱和」背景处擦除底图原字——彩色/深色区域保留原字且不叠加,避免白块与重影
// 用底图采样到的背景中位色擦除原字:
// · 白底 → 擦成白(原有行为)
// · 灰条 / 色带 → 擦成同色,不再留下白块
// 且**任何底色上的文字都生成可编辑元素**。之前"背景非近白就整段丢弃"
// 会让面单灰条 / 色带上的字既改不了、也擦不掉,永久留在底图上。
const bg = boxBackground(elX, elY, txtW, elH);
if (bg.lum >= 205 && bg.spread <= 40) {
const kk = EXPORT_DPI / 25.4;
ctx.fillStyle = '#ffffff';
ctx.fillRect(
Math.max(0, Math.round(elX * kk)),
Math.max(0, Math.round(elY * kk)),
Math.max(1, Math.round(txtW * kk)),
Math.max(1, Math.round(elH * kk)),
);
textEls.push({
id: newId(),
type: 'text',
x: Math.round(elX * 10) / 10,
y: Math.max(0, Math.round(elY * 10) / 10),
w: Math.round(elW * 10) / 10,
h: Math.round(elH * 10) / 10,
text: str,
fontSize: Math.round(fs * 10) / 10,
bold: false,
align: 'left',
color: '#000000',
});
}
const kk = EXPORT_DPI / 25.4;
ctx.fillStyle = `rgb(${bg.r}, ${bg.g}, ${bg.b})`;
ctx.fillRect(
Math.max(0, Math.round(elX * kk)),
Math.max(0, Math.round(elY * kk)),
Math.max(1, Math.round(txtW * kk)),
Math.max(1, Math.round(elH * kk)),
);
textEls.push({
id: newId(),
type: 'text',
x: Math.round(elX * 10) / 10,
y: Math.max(0, Math.round(elY * 10) / 10),
w: Math.round(elW * 10) / 10,
h: Math.round(elH * 10) / 10,
text: str,
fontSize: Math.round(fs * 10) / 10,
bold: false,
align: 'left',
// 深色底上的原字通常是反白的,替换后也要保持可见
color: bg.lum < 110 ? '#ffffff' : '#000000',
});
}
}
catch { /* 无文字层或提取失败:仅保留底图 */ }