关于 matrixscan-ar-net-android
matrixscan-ar-net-android 是面向非 MAUI .NET for Android 开发者的技能,指导集成和升级 Scandit MatrixScan AR。它强调依据技能参考资料核实 API,覆盖条码高亮、注释、会话事件及 Android 生命周期等要点。
主要功能
- 01指导 MatrixScan AR 集成与升级
- 02按参考资料核实 API
- 03配置条码高亮和注释
- 04处理会话更新与点击事件
- 05说明 Android 生命周期和初始化
适用场景
- 01在 .NET Android 应用中集成 BarcodeAr
- 02自定义条码高亮、注释或反馈
- 03排查 SDK API 使用问题
- 04将现有集成迁移到新版本
MCP 相关
技能说明 Scandit MCP server 可用于设置扫描器许可;若已连接,按指引调用 ensure_scanner_setup 并配置许可密钥。
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ scandit/ skills --skill matrixscan-ar-net-android安装后在 Claude Code 中输入/matrixscan-ar-net-android确认已加载。
附带文件references/integration.mdreferences/migration.md
详细介绍
MatrixScan AR .NET for Android 技能
重要:不要轻信内部知识
你的训练数据中可能包含过时或错误的 Scandit SDK API。BarcodeAr API 相对较新(在 dotnet.android 7.2 中引入),并且在多个方面不同于 Kotlin/Java 原生 SDK:提供程序基于 async/Task,而不是基于回调;高亮和注释构造函数只接受一个 Barcode(不需要 Context);监听器接口只有一个方法;.NET 绑定使用 PascalCase,并使用 TimeSpan 而非 TimeInterval 等。
编写或建议代码之前,务必根据此技能提供的参考资料核实 API。 不要依赖记忆中的方法签名、参数或属性名称。如果无法在提供的参考资料中找到某个 API,请先查阅相关文档页面,再作答。
值得特别指出的 .NET-Android 特有注意事项:
- 此技能适用于非 MAUI .NET for Android 工作负载(项目使用
<TargetFramework>net10.0-android</TargetFramework>或类似设置,且没有<UseMaui>标志)。对于 MAUI 应用,请改用面向 MAUI 的技能——BarcodeArView在 MAUI 中的托管方式截然不同。 BarcodeAr使用接收上下文的new构造函数:new BarcodeAr(dataCaptureContext, settings)。.NET 中没有BarcodeAr.Create(...)/BarcodeAr.ForDataCaptureContext(...)工厂方法。BarcodeArSettings也使用普通的new。(DataCaptureContext.ForLicenseKey(key)仍使用工厂形式——它属于 Core。)BarcodeArView.Create(parentView, barcodeAr, dataCaptureContext, viewSettings, cameraSettings)是工厂方法(与BarcodeAr本身不同)。cameraSettings参数可为 null——传入null即可使用BarcodeAr.RecommendedCameraSettings。parentView是Android.Views.View/ViewGroup(通常为FrameLayout或 Activity 的根内容视图)。没有BarcodeArCoordinatorLayout——该容器专用于 SparkScan;BarcodeArView会直接将自身附加到传入的ViewGroup。BarcodeArView实现了IDisposable,本身不是 AndroidView。 此类声明了public static implicit operator View(BarcodeArView view),需要时(例如原生互操作)可将其转换为Android.Views.View,但你无需自行将其添加到视图层级中——Create工厂会自动将其附加到parentView。- 视图的生命周期方法是
barcodeArView.OnResume()/barcodeArView.OnPause()——这些是仅限 Android 的方法(在绑定中由#if __ANDROID__控制),不是 Activity 的OnPause/OnResume。请将 Activity 的调用转发给它们。.NETBarcodeArView不存在OnDestroy()——请改为调用Dispose()(在 Activity 的OnDestroy或Dispose中)。 IBarcodeArListener只有一个方法:OnSessionUpdated(BarcodeAr, BarcodeArSession, IFrameData)。它没有 KotlinBarcodeArListener中的OnObservationStarted/OnObservationStopped回调。实现这些方法会导致编译错误——该接口根本没有声明它们。- 在符合 C# 习惯的代码中,优先使用事件 API(
barcodeAr.SessionUpdated += handler),而不是监听器接口。处理程序会收到包含BarcodeAr、Session和FrameData的BarcodeArEventArgs。为与其他平台保持一致,AddListener(IBarcodeArListener)仍可使用。 OnSessionUpdated/SessionUpdated在后台识别线程上运行。 任何 UI 更新都应通过RunOnUiThread(() => { … })调度。- Provider 接口采用异步方式,而非基于回调。
IBarcodeArHighlightProvider.HighlightForBarcodeAsync(Barcode)返回Task<IBarcodeArHighlight?>,IBarcodeArAnnotationProvider.AnnotationForBarcodeAsync(Barcode)返回Task<IBarcodeArAnnotation?>。不要查找Callback参数或callback.OnData(...)方法——.NET 绑定中没有这些内容。对于特定条码,若要隐藏高亮或注释,请返回Task.FromResult<IBarcodeArHighlight?>(null)(或在async方法中返回null)。 - 高亮和注释构造函数只接收
Barcode,不接收Context参数。请使用new BarcodeArRectangleHighlight(barcode)、new BarcodeArCircleHighlight(barcode, BarcodeArCircleHighlightPreset.Dot)、new BarcodeArInfoAnnotation(barcode)、new BarcodeArStatusIconAnnotation(barcode)、new BarcodeArPopoverAnnotation(barcode, buttons)。传入Context会导致编译错误。 - 条码制式名称采用 C# PascalCase:
Symbology.Ean13Upca、Symbology.Ean8、Symbology.Code128、Symbology.Code39、Symbology.Qr、Symbology.DataMatrix、Symbology.InterleavedTwoOfFive。它们不是 Kotlin 的下划线风格(EAN13_UPCA)。 BarcodeArSettings没有Enabled开关——BarcodeAr本身也没有Enabled属性。若要暂停或恢复扫描,请使用barcodeArView.Pause()/barcodeArView.Start()。- .NET 中的
BarcodeArViewSettings配置很精简。 只有三个属性:SoundEnabled、HapticEnabled、DefaultCameraPosition。不要臆造TriggerButtonCollapseTimeout、InactiveStateTimeout、ToastSettings或DefaultMiniPreviewSize等属性——这些属于 SparkScan,而非 BarcodeAr。 BarcodeArFeedback位于Scandit.DataCapture.Barcode.Ar.Feedback,包含两个Core.Common.Feedback.Feedback属性:Scanned和Tapped。空构造函数new BarcodeArFeedback()会创建一个两个事件均静音的反馈对象——将其赋给barcodeAr.Feedback会关闭默认的提示音和振动。若要恢复默认值,请使用静态属性BarcodeArFeedback.DefaultFeedback。(注意:在 .NET 中它是静态属性,不是 Kotlin 的BarcodeArFeedback.defaultFeedback()方法。)- 高亮上的点击交互通过
BarcodeArView的HighlightForBarcodeTapped事件公开(EventHandler<HighlightForBarcodeTappedEventArgs>)。.NETBarcodeArView没有UiListener属性——Kotlin 的IBarcodeArViewUiListener在 C# 中以事件形式提供。事件参数公开了BarcodeAr、Barcode和Highlight。 barcodeAr.Feedback是一个属性(可读写);ApplySettingsAsync(BarcodeArSettings)返回一个Task。BarcodeAr.RecommendedCameraSettings是静态属性,不是方法(Kotlin SDK 提供BarcodeAr.createRecommendedCameraSettings(),而 .NET 中对应的是一个 getter)。- .NET API 中没有
BarcodeArFilter/SetBarcodeFilter。 Kotlin/iOS 的setBarcodeFilter(...)方法(在 8.1 中新增)目前尚未在dotnet.android中公开。请勿尝试使用。 - SDK 8.0+ 需要显式初始化。 请继承
Android.App.Application,添加[Application],并在OnCreate()中、任何 Scandit 代码运行之前调用ScanditCaptureCore.Initialize()+ScanditBarcodeCapture.Initialize()。否则,首次调用new BarcodeAr(...)/BarcodeArView.Create(...)时会因 DI 容器没有注册项而在启动时崩溃。6.x / 7.x 不需要此操作。 完整的MainApplication.cs模板请参阅 references/integration.md。 - NuGet 包为
Scandit.DataCapture.Core和Scandit.DataCapture.Barcode。这里没有单独的*.Maui包——这些包仅用于 MAUI 项目。不要根据训练数据猜测版本——固定版本前,请通过WebFetch从https://www.nuget.org/packages/Scandit.DataCapture.Barcode/获取最新稳定版本。臆造不存在的版本(例如,实际只发布了8.4.0却填写8.13.0)会导致dotnet restore因Unable to find package Scandit.DataCapture.Core with version (>= …)而失败。完整流程请参阅 references/integration.md 的步骤 0。 - Android 的
SupportedOSPlatformVersion必须 ≥24。 请在.csproj中设置。较低的值会导致构建失败,并提示uses-sdk:minSdkVersion 21 cannot be smaller than version 24 declared in library。 - 不要在
AndroidManifest.xml中为带有[Activity]特性的类声明<activity>元素。[Activity(MainLauncher = true, ...)]特性是在 .NET for Android 中注册 Activity 的规范方式——构建过程会使用 .NET 派生的 Java 类名,将名称正确的条目合并到最终清单中。手动添加的<activity android:name=".MainActivity">会根据<ApplicationId>解析,无法与生成的类匹配,导致应用启动时出现ClassNotFoundException: Didn't find class ... .MainActivity。仅在清单中添加技能明确要求的元素(<uses-feature>、<uses-permission>)——Activity 交由特性处理。 - 运行时相机权限辅助类(
CameraPermissionActivity)继承自AppCompatActivity,因此必须在.csproj中添加Xamarin.AndroidX.AppCompat。固定版本时,请选择包含 Xamarin 修订号在内的最高可用版本(例如1.7.0.5,而不是单独的1.7.0)——.X后缀表示 Xamarin 绑定级别的更新,其中包含重要的传递依赖修复。 - Activity 需要使用
Theme.AppCompat的派生主题。 由于 Activity 继承自AppCompatActivity,请在[Activity]特性中设置Theme = "@style/Theme.AppCompat.Light.NoActionBar"(或在清单的<application>上设置android:theme=...)。否则,启动时调用SetContentView会抛出IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity。dotnet new android模板的默认主题并非基于 AppCompat,因此必须显式设置。
意图路由
根据用户的请求,在回复前加载相应的参考文件:
- 从头集成 BarcodeAr、配置设置、自定义高亮或注释、处理会话更新、自定义反馈或接入点击交互(例如,“将 MatrixScan AR 添加到我的 .NET Android 应用中”“在 C# 中设置条码 AR 扫描”“为每个跟踪到的条码显示矩形高亮”“显示带有条码数据的信息注释”“关闭提示音”“响应高亮点击”“切换为圆形高亮”)→ 阅读 references/integration.md,并遵循其中的说明。
- 迁移或升级现有的 MatrixScan AR 集成(例如,“从 v7 升级到 v8”“将 Scandit .NET SDK 升级到 v8”“BarcodeAr 的 SDK 版本之间有哪些变化”“升级到 8.x 时是否需要更改 BarcodeAr 代码”)→ 阅读 references/migration.md,并遵循其中的说明。
API 使用政策
仅使用以下 Scandit 参考资料中明确记录的 API。不要臆造或猜测方法签名、参数或属性名称。如果不确定某个 API 是否存在或如何调用,或遇到编译错误,请先获取相关参考页面再回复。不要让用户自行查阅文档。回答后,始终附上相关链接,方便用户进一步了解。
切勿构造或猜测文档 URL。 需要查找特定类或属性的 API 页面时:
- 先检查已获取的页面是否包含指向该页面的直接超链接——主题页面会直接链接到相关 API 符号。请求获取内容时,始终一并请求链接。
- 如果没有找到直接链接,请获取 API 索引(见下表中的 完整 API 参考),从中提取实际链接并访问。
URL 结构可能不同(例如,包含 api/ui/ 子目录),猜测 URL 会导致 404 错误。
许可证密钥
扫描需要 Scandit 许可证密钥。对此技能,许可证产品为 native,许可证平台为 android。
请按以下顺序操作——不要因为 MCP 而阻止用户:
-
Scandit MCP 服务已连接——调用
ensure_scanner_setup,设置产品为native、平台为android,然后运行get_license_env_command返回的命令,将密钥写入项目的.env。在聊天中最多只显示掩码预览;绝不要打印完整密钥。 -
尚未连接——先询问用户是否愿意连接一次,然后从第 1 步继续:
- Claude Code:
claude mcp add --transport http scandit https://ssl.scandit.com/mcp - Cursor:将
{"mcpServers": {"scandit": {"url": "https://ssl.scandit.com/mcp"}}}添加到~/.cursor/mcp.json - VS Code:将
{"servers": {"scandit": {"type": "http", "url": "https://ssl.scandit.com/mcp"}}}添加到 MCP 用户配置中
身份验证通过浏览器进行,因此不支持无头环境或 CI。该服务仅提供试用密钥;不会创建、撤销或修改生产许可证。
- Claude Code:
-
用户拒绝,或没有 MCP 客户端——让用户在 https://ssl.scandit.com 自行生成密钥(尚无账户:https://ssl.scandit.com/dashboard/sign-up?p=test),并粘贴密钥。
然后将密钥填入代码,替换占位符;仅将密钥放入 .env 并不会自动传入应用。.NET 不会自动加载 .env,因此请使用下面最简单的方法。任何平台上最简单的方法:读取 .env,并将密钥直接填入许可证密钥调用中,同时在聊天中只显示掩码预览。
参考资料
根据用户的问题,引导他们访问相应的资源:
| 主题 | 资源 |
|---|---|
| 开始使用 | 开始使用(.NET for Android) |
| 进阶主题(自定义高亮、自定义标注、点击交互、弹出层、筛选器) | 高级配置 |
| 在主要 SDK 版本之间迁移 | 6 → 7 · 7 → 8 |
| 完整 API 参考 | BarcodeAr API(.NET Android) |
此技能涵盖的 API 范围
官方 RST 文档(docs/source/barcode-capture/api/barcode-ar*.rst 和 api/ui/barcode-ar-*.rst)中以 :available: dotnet.android 标注的所有类,都在 references/integration.md 中有所介绍:
BarcodeAr—new BarcodeAr(DataCaptureContext?, BarcodeArSettings)、Feedback(获取/设置)、ApplySettingsAsync(BarcodeArSettings)→Task、AddListener(IBarcodeArListener)/RemoveListener(IBarcodeArListener)、event EventHandler<BarcodeArEventArgs> SessionUpdated、静态属性RecommendedCameraSettings、Dispose。BarcodeArSettings—new BarcodeArSettings()、EnableSymbology(Symbology, bool)、EnableSymbologies(ICollection<Symbology>)、GetSymbologySettings(Symbology)、EnabledSymbologies(获取)、ExpectsOnlyUniqueBarcodes(获取/设置)、SetProperty/GetProperty<T>/TryGetProperty<T>、Dispose。IBarcodeArListener— 单个方法OnSessionUpdated(BarcodeAr, BarcodeArSession, IFrameData)。(没有OnObservation*回调。)BarcodeArSession—AddedTrackedBarcodes(IReadOnlyList<TrackedBarcode>)、RemovedTrackedBarcodes(IReadOnlyList<int>)、TrackedBarcodes(IReadOnlyDictionary<int, TrackedBarcode>)、Reset()。BarcodeArEventArgs—BarcodeAr、Session、FrameData。BarcodeArFeedback—new BarcodeArFeedback()(静音)、静态属性DefaultFeedback(默认值)、Scanned/Tapped(Core.Common.Feedback.Feedback)、Dispose。BarcodeArView—static Create(View parentView, BarcodeAr, DataCaptureContext, BarcodeArViewSettings, CameraSettings?)、HighlightProvider(获取/设置IBarcodeArHighlightProvider?)、AnnotationProvider(获取/设置IBarcodeArAnnotationProvider?)、ShouldShowTorchControl/ShouldShowZoomControl/ShouldShowCameraSwitchControl、TorchControlPosition/ZoomControlPosition/CameraSwitchControlPosition(Anchor)、Start()、Stop()、Pause()、Reset()、GetNotificationPresenter()、OnResume()/OnPause()(仅限 Android)、event EventHandler<HighlightForBarcodeTappedEventArgs> HighlightForBarcodeTapped、隐式转换为Android.Views.View、Dispose。BarcodeArViewSettings—SoundEnabled(默认值为true)、HapticEnabled(默认值为true)、DefaultCameraPosition(默认值为WorldFacing)。HighlightForBarcodeTappedEventArgs—BarcodeAr、Barcode、Highlight(IBarcodeArHighlight)。- 高亮:
IBarcodeArHighlight : IDisposable、IBarcodeArHighlightProvider.HighlightForBarcodeAsync(Barcode) → Task<IBarcodeArHighlight?>、BarcodeArRectangleHighlight(Barcode),包含Barcode/Brush/Icon、BarcodeArCircleHighlight(Barcode, BarcodeArCircleHighlightPreset),包含Barcode/Brush/Icon/Size、BarcodeArCircleHighlightPreset枚举(Dot、Icon)。 - 标注:
IBarcodeArAnnotation : IDisposable(声明AnnotationTrigger)、IBarcodeArAnnotationProvider.AnnotationForBarcodeAsync(Barcode) → Task<IBarcodeArAnnotation?>。BarcodeArStatusIconAnnotation(Barcode)—AnnotationTrigger、HasTip、Icon、Text、TextColor、BackgroundColor。BarcodeArInfoAnnotation(Barcode)—HasTip、EntireAnnotationTappable、Anchor(BarcodeArInfoAnnotationAnchor)、AnnotationTrigger、Width(BarcodeArInfoAnnotationWidthPreset)、Body(IReadOnlyCollection<BarcodeArInfoAnnotationBodyComponent>)、Header(BarcodeArInfoAnnotationHeader?)、Footer(BarcodeArInfoAnnotationFooter?)、BackgroundColor、Listener(IBarcodeArInfoAnnotationListener?)。BarcodeArPopoverAnnotation(Barcode, IList<BarcodeArPopoverAnnotationButton>)—AnnotationTrigger、EntirePopoverTappable、Listener(IBarcodeArPopoverAnnotationListener?)、Buttons。BarcodeArPopoverAnnotationButton(ScanditIcon, string)—Text、TextSize、Typeface、TextColor、Enabled、Icon。BarcodeArAnnotationTrigger枚举:HighlightTapAndBarcodeScan、HighlightTap。
- 信息标注子包(
Scandit.DataCapture.Barcode.Ar.UI.Annotations.Info):BarcodeArInfoAnnotationBodyComponent(Text、TextColor、TextSize、Typeface、StyledTextFormatted、LeftIcon、RightIcon、LeftIconTappable、RightIconTappable、TextAlignment)、BarcodeArInfoAnnotationHeader(Text、TextSize、Typeface、TextColor、Icon、BackgroundColor)、BarcodeArInfoAnnotationFooter(Text、TextSize、Typeface、TextColor、Icon、BackgroundColor)、BarcodeArInfoAnnotationAnchor枚举(Left、Right、Bottom、Top)、BarcodeArInfoAnnotationWidthPreset枚举(Small、Medium、Large)、IBarcodeArInfoAnnotationListener(OnInfoAnnotationHeaderTapped、OnInfoAnnotationFooterTapped、OnInfoAnnotationLeftIconTapped、OnInfoAnnotationRightIconTapped、OnInfoAnnotationTapped)。 IBarcodeArPopoverAnnotationListener—OnPopoverButtonTapped、OnPopoverTapped。TrackedBarcode(位于Scandit.DataCapture.Barcode.Batch.Data中)—Barcode、Identifier、Location。