曝光补偿不受支持或设置失败时静默回退

This commit is contained in:
tfarcraw 2026-11-18 17:13:54 +08:00
parent f171940382
commit fbe5a0090b
5 changed files with 16 additions and 9 deletions

View File

@ -20,7 +20,7 @@ pwsh -File tools/Start-HandAiD3.ps1
## 操作流程
1. 在“运行设置”中选择 UVC 或海康相机,新建或选择配方。设置页保留相机、配方和抽帧参数。
2. 配方编辑器包含“步骤、区域、全局设置、SOP流程视频、Agent助手”五页。在原始相机画面上绘制区域;相机设置独立于配方,在“运行设置”中点击相机后面的“配置”按钮打开专用窗口。USB 分辨率、目标帧率、视频格式和曝光补偿使用下拉菜单选择,列表提供常用配置,不代表设备均支持;海康相机可设置曝光时间。设置按设备保存在 workspace/camera-settings.json,保存后下次开启采集、取图或录像预览时生效。SOP 日志显示驱动回读模式,状态栏分别显示 USB 实测采集和渲染 fps。曝光补偿以 EV 为单位,负值压低自动曝光;选择“不修改”保留当前值,0 EV 清除补偿。当前通过 Windows MSMF 对应设备的 KS 扩展接口写入并回读确认,不支持的设备、步长或平台会明确报错;可改回“不修改”继续采集。
2. 配方编辑器包含“步骤、区域、全局设置、SOP流程视频、Agent助手”五页。在原始相机画面上绘制区域;相机设置独立于配方,在“运行设置”中点击相机后面的“配置”按钮打开专用窗口。USB 分辨率、目标帧率、视频格式和曝光补偿使用下拉菜单选择,列表提供常用配置,不代表设备均支持;海康相机可设置曝光时间。设置按设备保存在 workspace/camera-settings.json,保存后下次开启采集、取图或录像预览时生效。SOP 日志显示驱动回读模式,状态栏分别显示 USB 实测采集和渲染 fps。曝光补偿以 EV 为单位,负值压低自动曝光;选择“不修改”保留当前值,0 EV 清除补偿。当前通过 Windows MSMF 对应设备的 KS 扩展接口写入并回读确认,不支持的设备、步长或平台,以及曝光补偿设置失败时,均静默跳过,不提示、不阻断采集。
3. 步骤名描述操作,“说明”填写送检单帧能看到的静态完成状态,AI 用说明判断图片。触发规则支持手在区域内、手进入后离开区域、A→B 移动和持续计时。
4. 送检可选整张画面、固定区域或跟随手部;跟随手部可以设置扩展距离、最小范围和位置保留时间。
5. 保存、应用后开始采集。规则满足只触发送图;AI 确认且完成概率达标后才推进。步骤说明为空、接口失败或证据不足时不会按规则直接完成。

View File

@ -25,7 +25,7 @@
<TextBlock Text="曝光补偿" VerticalAlignment="Center" />
<ComboBox x:Name="ExposureCompensationPicker" Grid.Column="1" ItemsSource="{Binding ExposureCompensations}" SelectedItem="{Binding SelectedExposureCompensation, Mode=TwoWay}" HorizontalAlignment="Stretch" />
</Grid>
<TextBlock Text="自动曝光偏亮可先试 −0.33 或 −0.67 EV;0 EV 为无补偿,“不修改”保留相机当前值。设备不支持时开启采集会提示,改回“不修改”即可。" TextWrapping="Wrap" Foreground="{StaticResource FgDim}" FontSize="12" />
<TextBlock Text="自动曝光偏亮可先试 −0.33 或 −0.67 EV;0 EV 为无补偿,“不修改”保留相机当前值。" TextWrapping="Wrap" Foreground="{StaticResource FgDim}" FontSize="12" />
<TextBlock Text="列表为常用配置,不代表当前相机均支持。选择“驱动默认”可保留默认模式;实际协商结果见运行日志。" TextWrapping="Wrap" Foreground="{StaticResource FgDim}" FontSize="12" />
</StackPanel>
<StackPanel IsVisible="{Binding IsIndustrial}" Spacing="8">

View File

@ -146,13 +146,11 @@ public class UvcCameraService : ICameraService
if (options.Height is { } height) Set(VideoCaptureProperties.FrameHeight, height, "高度");
if (options.Fps is { } fps) Set(VideoCaptureProperties.Fps, fps, "目标帧率");
var exposureDescription = "";
if (options.ExposureCompensationEv is { } ev)
{
if (!OperatingSystem.IsWindows() || capture.Get(VideoCaptureProperties.Backend) != (double)VideoCaptureAPIs.MSMF)
throw new NotSupportedException("曝光补偿目前需要 Windows MSMF 采集后端;请在相机配置中选择“不修改”继续采集");
var actualEv = WindowsExposureCompensation.Set(Current?.Index ?? throw new InvalidOperationException("相机未连接"), ev);
if (options.ExposureCompensationEv is { } ev && OperatingSystem.IsWindows()
&& capture.Get(VideoCaptureProperties.Backend) == (double)VideoCaptureAPIs.MSMF
&& Current is { } current
&& WindowsExposureCompensation.TrySet(current.Index, ev) is { } actualEv)
exposureDescription = $";曝光补偿回读 {actualEv:+0.##;-0.##;0} EV";
}
var actualWidth = capture.Get(VideoCaptureProperties.FrameWidth);
var actualHeight = capture.Get(VideoCaptureProperties.FrameHeight);
var actualFps = capture.Get(VideoCaptureProperties.Fps);

View File

@ -13,7 +13,14 @@ internal static unsafe class WindowsExposureCompensation
private static readonly Guid VideoCapture = new("8AC3587A-4AE7-42D8-99E0-0A6013EEF90F");
private static readonly Guid SymbolicLink = new("58F0AAD8-22BF-4F8A-BB3D-D2C4978C6E2F");
public static double Set(int index, double ev)
/// <summary>曝光补偿是可选功能,驱动不支持或应用失败时静默跳过。</summary>
public static double? TrySet(int index, double ev)
{
try { return Set(index, ev); }
catch (Exception) { return null; }
}
private static double Set(int index, double ev)
{
if (!OperatingSystem.IsWindows()) throw new NotSupportedException("当前平台尚不支持 USB 曝光补偿");
using var device = OpenDevice(index);

View File

@ -597,6 +597,8 @@ internal static class Smoke
var rejectedRange=false;try{thirds.Encode(-3);}catch(ArgumentOutOfRangeException){rejectedRange=true;}
Check(rejectedRange,"Requested EV is checked against the actual device range");
var native=typeof(UvcCameraService).Assembly.GetType("SopHelper.Engine.Camera.WindowsExposureCompensation")!;
Check(native.GetMethod("TrySet")!.Invoke(null,[-1,-1d]) is null,
"Unavailable exposure compensation silently returns without throwing or writing to a camera");
Check(System.Runtime.InteropServices.Marshal.SizeOf(native.GetNestedType("EvPayload",System.Reflection.BindingFlags.NonPublic)!)==56,
"Native EV payload layout matches the Windows SDK");
var lifetime=(IClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;