diff --git a/desktop/README.md b/desktop/README.md
index 7e4818d..79acfef 100644
--- a/desktop/README.md
+++ b/desktop/README.md
@@ -846,8 +846,9 @@ reg query "HKCU\Software\Classes\yuncang\shell\open\command"
ffmpeg(RTSP, tcp) → 灰度 rawvideo 帧 → 读帧线程只留最新一帧(按 DecodeFps 限速)
→ OpenCV BarcodeDetector 整图检测(cv::barcode,输出条码四角点,带旋转/透视)
→ 无候选 = 画面里没面单,直接丢掉
- → 有候选:逐个透视矫正成轴对齐小图 → ZXing 解 CODE_128 → CleanBarcode 校验
- → 命中入队(扫描线程不等) → 上报线程:写记录 + 立即上报 + 通知安卓播报
+ → 有候选:**逐个**(不是解出第一个就停)透视矫正成轴对齐小图 → ZXing 解 CODE_128 → CleanBarcode 校验
+ → 和「条码历史」对比:新码立即入队、旧码只续在场
+ → 上报线程:写记录 + 立即上报 + 通知安卓播报
```
- **为什么用 OpenCV 检测而不是自己找 ROI**:现场面单是**竖放或斜着**摆的(实测四角点
@@ -869,6 +870,13 @@ ffmpeg(RTSP, tcp) → 灰度 rawvideo 帧 → 读帧线程只留最新一帧(
- **RTSP 通道序号**:库里存的是设备通道号(数字通道从 33 起),RTSP 要 1 起序号,
所以按 `channelNo-32 → channelNo` 依次探测,成功后**记住**(日志 `码流就绪:主码流 2304x1296(序号 3201)`);
- 条码过滤:4~64 位、只含常见条码字符、**至少 6 位数字**(压掉摄像头误识别噪声);
+- **多张面单同框(一次解出多个条码)**:一帧里的每个候选都要解,只认第一个必然漏单
+ (角点顺序由检测器决定,跟「哪张是新面单」无关)。解出的码记进**条码历史**(旧 → 新,最多 4 个,
+ 超过 45 秒没再出现的自动淘汰,阈值与「面单离开」一致),再按这个规则上报:
+ 1. 历史里没有的**新码 → 立即上报**(同帧出现几个新码就报几个,不压帧、不投票);
+ 2. 历史里已有的**旧码 → 只刷新在场**,不重复建档/播报(ScanService 本来就按「通道|条码」去重);
+ 3. 两个码都在历史里时,上报/续期的是**最后出现的那个**,不是检测器先解出来的那个;
+ 已有的码只更新时间、不重排,避免同框两码每帧互相顶掉导致上报抖动。
- 同一张面单连续命中节流到 500ms 一次:足够维持「在场」状态,又不会刷上报;
- 断流自动重连(3 秒),连续失败按 3/10 次节流打日志;不影响扫码枪链路。
@@ -880,6 +888,7 @@ ffmpeg(RTSP, tcp) → 灰度 rawvideo 帧 → 读帧线程只留最新一帧(
| `LastCvMs` | OpenCV 整图检测耗时 |
| `LastDecodeMs` | 透视矫正 + ZXing 解码耗时 |
| `RoiText` | 矫正后的条码尺寸(如 `条码 937x90`),或 `无候选` |
+| `RecentCodes` | 条码历史(旧 → 新,`A \| B`):末尾是最后出现的那个,用来核对多码同框时上报的是哪个 |
| `RoiRatio` | 矫正+解码耗时(毫秒) |
| `FrameLag` / `FrameDropped` | 取帧时的积压帧数 / 累计丢弃旧帧数(正常应接近 0) |
| `FramesRead` / `FrameThrottled` / `FramesDecoded` | 管道读到 / 限速丢弃 / 实际处理 的帧数 |
diff --git a/desktop/Services/Agent/AgentFiles.cs b/desktop/Services/Agent/AgentFiles.cs
index 1e6cede..de86702 100644
--- a/desktop/Services/Agent/AgentFiles.cs
+++ b/desktop/Services/Agent/AgentFiles.cs
@@ -201,6 +201,9 @@ public sealed class AgentLabelCameraStatus
/// 最近一帧自动定位到的条码区域(ROI w x h;未定位时为空文案)
public string RoiText { get; set; } = "";
+ /// 条码历史(旧 → 新,最多 4 个;末尾是最后出现的那个)
+ public string RecentCodes { get; set; } = "";
+
/// 最近一帧「透视矫正 + ZXing 解码」耗时(毫秒)
public double RoiRatio { get; set; }
diff --git a/desktop/Services/Agent/AgentHost.cs b/desktop/Services/Agent/AgentHost.cs
index 07751cf..30fa015 100644
--- a/desktop/Services/Agent/AgentHost.cs
+++ b/desktop/Services/Agent/AgentHost.cs
@@ -533,6 +533,7 @@ public static class AgentHost
LastWaitMs = runtime.LastWaitMs,
LastLoopMs = runtime.LastLoopMs,
RoiText = runtime.RoiText,
+ RecentCodes = runtime.RecentCodes,
RoiRatio = runtime.RoiRatio,
FrameLag = runtime.FrameLag,
FrameDropped = runtime.FrameDropped,
diff --git a/desktop/Services/Scan/LabelScanWorker.cs b/desktop/Services/Scan/LabelScanWorker.cs
index 23e2c26..c749442 100644
--- a/desktop/Services/Scan/LabelScanWorker.cs
+++ b/desktop/Services/Scan/LabelScanWorker.cs
@@ -41,6 +41,12 @@ public sealed class LabelCameraRuntime
/// 最近识别到的条码
public string LastCode { get; set; } = string.Empty;
+ ///
+ /// 条码历史(旧 → 新,最多 4 个;末尾是**最后出现**的那个)。
+ /// 多张面单同框时靠它判断「这个码是新出现的,还是刚才就已经有了」。
+ ///
+ public string RecentCodes { get; set; } = string.Empty;
+
///
/// **首次识别时间**:也就是这条记录真正上报/落库用的时间。
/// 面单一直摆在拍摄区不会刷新(重复命中不改时间),避免界面看起来「时间一直在跳」。
@@ -403,6 +409,24 @@ internal sealed class LabelCameraRunner
/// 上一次成功解出的条码(用于判断「还是同一张面单」)
private string _lastHitCode = string.Empty;
+ ///
+ /// 条码历史(旧 → 新,末尾 = 最后出现的那个)。
+ ///
+ /// 为什么要有它:一帧里可能同时解出**多个**条码(两张面单并排)。
+ /// 只读一个必然漏单,而「谁先解出来」取决于检测器给的角点顺序、跟谁是新面单无关,
+ /// 所以必须记住刚才出现过哪些码,才能分辨出「刚进来的新码」和「一直在画面里的旧码」。
+ ///
+ private readonly List<(string Code, DateTime At)> _codeHistory = new();
+
+ /// 上一帧看得见的码:区分「一直在画面里」和「离开又回来」,后者才算最后出现
+ private readonly HashSet _visibleLastFrame = new(StringComparer.Ordinal);
+
+ ///
+ /// 条码历史保留条数。至少要 2 个才能做「新码 / 旧码」的对比;留 4 个是为了
+ /// 画面里同时出现两三张面单(或一两帧误读)时,不会把仍然在场的码挤出去、下一帧又被当成新码重报。
+ ///
+ private const int CodeHistorySize = 4;
+
public LabelCameraRunner(LabelCameraTarget target, Func> report, Action changed)
{
_target = target;
@@ -920,6 +944,9 @@ internal sealed class LabelCameraRunner
var path = "退避";
var candidates = 0;
+ // 本帧解出的**所有**条码(去重、按解出顺序)。多张面单同框时一个都不能丢。
+ var frameCodes = new List(4);
+
if (!backoffSkipped)
{
decodeWatch.Restart();
@@ -949,15 +976,23 @@ internal sealed class LabelCameraRunner
Runtime.LastCvMs = decodeWatch.Elapsed.TotalMilliseconds;
decodeWatch.Restart();
- for (var i = 0; i < candidates && code is null; i++)
+ // ⚠️ 一帧里的**每个**候选都要解,不能解出第一个就停:
+ // 角点顺序由检测器决定,跟「哪张是新面单」没有关系,
+ // 只认第一个 → 两张面单并排时必然只报旧码、漏掉新码。
+ for (var i = 0; i < candidates; i++)
{
var text = DecodeQuad(reader, frameMat, points!, i,
out var warpW, out var warpH, out var rasterMs, out var decodeMs);
// 与扫码枪同一条合法性校验:条码长度/字符集不合规的不算命中(压掉误读)
- code = LabelScanWorker.CleanBarcode(text);
- if (code is not null)
+ var decoded = LabelScanWorker.CleanBarcode(text);
+ if (decoded is null) continue;
+
+ if (!frameCodes.Contains(decoded)) frameCodes.Add(decoded);
+
+ if (code is null)
{
+ code = decoded;
decodeWatch.Stop();
Runtime.LastDecodeMs = decodeWatch.Elapsed.TotalMilliseconds;
Runtime.RoiText = $"条码 {warpW}x{warpH}";
@@ -966,6 +1001,8 @@ internal sealed class LabelCameraRunner
}
}
+ if (frameCodes.Count > 1) path = $"OpenCV解码x{frameCodes.Count}";
+
if (code is null)
{
decodeWatch.Stop();
@@ -981,11 +1018,17 @@ internal sealed class LabelCameraRunner
path = _steadyHits >= SteadyBackoffAfter ? "稳态退避" : "失败退避";
}
+ // 条码历史:本帧解出的**每个**码(不管以前出现过没有)都刷新「最后出现时间」,
+ // 顺便算出哪些是历史里没有的新码
+ var now = DateTime.Now;
+ var fresh = RememberCodes(frameCodes, now);
+ var current = CurrentCode(frameCodes);
+
// 计数维护:命中就累计稳态、清失败;真跑过但没结果就累计失败、清稳态
- if (code is not null)
+ if (current is not null)
{
- _steadyHits = string.Equals(code, _lastHitCode, StringComparison.Ordinal) ? _steadyHits + 1 : 1;
- _lastHitCode = code;
+ _steadyHits = string.Equals(current, _lastHitCode, StringComparison.Ordinal) ? _steadyHits + 1 : 1;
+ _lastHitCode = current;
_failStreak = 0;
}
else if (!backoffSkipped && !sceneChanged)
@@ -997,33 +1040,123 @@ internal sealed class LabelCameraRunner
Runtime.LastPath = path;
Runtime.RoiFailStreak = _failStreak;
Runtime.SteadyHits = _steadyHits;
+ Runtime.RecentCodes = string.Join(" | ", _codeHistory.Select(x => x.Code));
- if (code is null) continue;
+ if (frameCodes.Count == 0) continue;
- // 同一张面单在视野里会连续命中:节流到 500ms 一次,足够维持「在场」状态又不刷上报
- var now = DateTime.Now;
- if (code == _lastSeenCode && (now - _lastReportAt).TotalMilliseconds < 500) continue;
- _lastSeenCode = code;
- _lastReportAt = now;
-
- // 这一条是不是「面单离开过、刚回来」后的第一次上报(真正的新一轮打包)。
+ // 这一帧是不是「面单离开过、刚回来」后的第一次上报(真正的新一轮打包)。
// 锁存时间最多认 60 秒:超过说明中间又走了别的流程,不再算这次重新打包。
var afterAbsence = _repackDetectedAt is DateTime repackAt
&& now >= repackAt
&& now - repackAt <= TimeSpan.FromSeconds(60);
if (afterAbsence) _repackDetectedAt = null;
- // 最近命中/最近条码每次都刷新,但「首次识别时间」只认记录里的时间(面单不拿走就不变)
- Runtime.LastCode = code;
- Runtime.LastHitAt = now;
- Runtime.LastError = null;
+ // 上报规则(多码同框时按这个走):
+ // 1. **历史里没有的新码 → 立即上报**(同帧出现几个新码就报几个,一个都不漏);
+ // 2. 历史里已有的旧码 → 只刷新在场,不重复建档/播报;
+ // 3. 两个码都在历史里时,真正上报/续期的是**最后出现的那个**(current),
+ // 不是检测器先解出来的那个。
+ foreach (var item in frameCodes)
+ {
+ var isFresh = fresh.Contains(item);
+ var isCurrent = string.Equals(item, current, StringComparison.Ordinal);
+ if (!isFresh && !isCurrent) continue;
- // 上报丢给独立线程:扫描线程只入队,不等写记录/上报/通知安卓的结果。
- // 这就是「管道 + 缓冲层 + 不关心是否到底」的做法:任何一环慢都只影响上报,不拖识别。
- EnqueueReport(code, Runtime.LastDecodeMs, Runtime.LastCvMs, afterAbsence);
+ // 同一个码一直在画面里:节流到 500ms 一次,只续「在场」,不重复上报
+ if (!isFresh
+ && string.Equals(item, _lastSeenCode, StringComparison.Ordinal)
+ && (now - _lastReportAt).TotalMilliseconds < 500) continue;
+
+ ReportHit(item, afterAbsence, now);
+ }
}
}
+ ///
+ /// 把本帧解出的条码登记进历史,返回「本帧新出现」的码。
+ ///
+ /// 排序规则(末尾 = 最后出现的那个):
+ /// - **一直在画面里**的码(上一帧也看得见)只更新时间、**不重排** ——
+ /// 否则同框的两个码每帧按解出顺序互相顶掉,「当前码」就会来回跳、上报跟着抖;
+ /// - **离开过又回来**的码挪到末尾(它才是最后出现的那个);
+ /// - 超过 没再出现的码直接淘汰 ——
+ /// 之后再出现就是新的一轮打包,按新码上报(与 ScanService 的在场判定同一个阈值)。
+ ///
+ private List RememberCodes(List frameCodes, DateTime now)
+ {
+ var fresh = new List(frameCodes.Count);
+
+ for (var i = _codeHistory.Count - 1; i >= 0; i--)
+ {
+ if (now - _codeHistory[i].At >= LabelScanWorker.RepackAbsence) _codeHistory.RemoveAt(i);
+ }
+
+ foreach (var item in frameCodes)
+ {
+ var index = _codeHistory.FindIndex(x => string.Equals(x.Code, item, StringComparison.Ordinal));
+ if (index < 0)
+ {
+ fresh.Add(item);
+ _codeHistory.Add((item, now));
+ continue;
+ }
+
+ if (_visibleLastFrame.Contains(item))
+ {
+ _codeHistory[index] = (item, now);
+ continue;
+ }
+
+ // 上一帧没看见 → 现在回来了:它就是「最后出现」的那个
+ _codeHistory.RemoveAt(index);
+ _codeHistory.Add((item, now));
+ }
+
+ _visibleLastFrame.Clear();
+ foreach (var item in frameCodes) _visibleLastFrame.Add(item);
+
+ while (_codeHistory.Count > CodeHistorySize)
+ {
+ // 淘汰最旧的那个,但**本帧还看得见的不能淘汰**:
+ // 淘汰掉的话下一帧它又成了「新码」,会被重复上报一次。
+ var evict = _codeHistory.FindIndex(x => !frameCodes.Contains(x.Code));
+ if (evict < 0) break;
+ _codeHistory.RemoveAt(evict);
+ }
+
+ return fresh;
+ }
+
+ ///
+ /// 本帧的「当前码」= 需要上报/续期的那个:历史里**最后出现**、且本帧还看得见的码。
+ ///
+ /// 两个码都在历史里时,这里给出的永远是后出现的那个,而不是检测器先解出来的那个。
+ ///
+ private string? CurrentCode(List frameCodes)
+ {
+ for (var i = _codeHistory.Count - 1; i >= 0; i--)
+ {
+ if (frameCodes.Contains(_codeHistory[i].Code)) return _codeHistory[i].Code;
+ }
+
+ return null;
+ }
+
+ /// 记一次命中并丢进上报队列(扫描线程只入队,不等写记录/上报/通知安卓)
+ private void ReportHit(string code, bool afterAbsence, DateTime now)
+ {
+ _lastSeenCode = code;
+ _lastReportAt = now;
+
+ // 最近命中/最近条码每次都刷新,但「首次识别时间」只认记录里的时间(面单不拿走就不变)
+ Runtime.LastCode = code;
+ Runtime.LastHitAt = now;
+ Runtime.LastError = null;
+
+ EnqueueReport(code, Runtime.LastDecodeMs, Runtime.LastCvMs, afterAbsence);
+ }
+
+
///
/// 把 OpenCV 检测到的第 index 个四角点区域做透视矫正,交给 ZXing 解 Code128。
///