.NET6 中带有 Blazor WASM 的 OpenCV 是否可行?

Is OpenCV with Blazor WASM in .NET6 feasible?

我想使用 Blazor WASM 和 OpenCV 在客户端处理图像。我可以 运行 OpenCV 与 OpenCVSharp4 NuGet 包服务器端,但这不会削减它。如果我尝试在客户端上让它 运行 我得到:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'OpenCvSharp.Internal.NativeMethods' threw an exception. ---> System.DllNotFoundException: OpenCvSharpExtern

我给它的另一个尝试是使用 opencv.js,但是如果我尝试加载它 <script src="opencv.js" async></script> 但失败

Error: Failed to start platform. Reason: TypeError: Module._malloc is not a function at St (blazor.webassembly.js:1)

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'apply') at Module.stackSave (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at ccall (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at Object.wasm_setenv (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at Object.mono_wasm_setenv (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at blazor.webassembly.js:1 at callRuntimeCallbacks (opencv.js:30) at postRun (opencv.js:30) at doRun (opencv.js:30) at run (opencv.js:30) at runCaller (opencv.js:30)

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'apply') at Object.Module._malloc (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at Object.mono_wasm_load_bytes_into_heap (_framework/dotnet.6.0.0.tj42mwroj7.js:1) at blazor.webassembly.js:1

我考虑过将 Native Dependencies 与 .NET6 一起使用,但我也无法让它工作。有什么我缺少的重要内容吗?

使用 opencv-3.4.7.js 更高版本对我不起作用。并且在加载 blazor wasm 之前加载 opencv.js 文件。

<body>
    <div id="app">Loading...</div>

    <script src="/lib/opencv-3.4.7.js"></script>
    <script src="_framework/blazor.webassembly.js"></script>
    <script src="_content/MudBlazor/MudBlazor.min.js"></script>
    <script>navigator.serviceWorker.register('service-worker.js');</script>
</body>

是可行的 参考下面的项目文件

    <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <WasmBuildNative>true</WasmBuildNative>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <RunAOTCompilation>True</RunAOTCompilation>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <RunAOTCompilation>True</RunAOTCompilation>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.1" PrivateAssets="all" />
    <PackageReference Include="OpenCvSharp4" Version="4.5.5.20211231" />
    <PackageReference Include="OpenCvSharp4.runtime.wasm" Version="4.5.5.20211231" />
  </ItemGroup>

</Project>

查看 Sample code