【问题标题】:How to call "libheif.dll" function from a UWP app如何从 UWP 应用程序调用“libheif.dll”函数
【发布时间】:2019-07-11 09:16:00
【问题描述】:

我正在尝试在我的 UWP 应用中使用 "libheif" library 来解码一些 HEIC 图像。 在 heif.h 中,我将闲置的 C++ 代码 extern "C" __declspec(dllexport) 添加到我想从 C# 代码调用的函数中:

    LIBHEIF_API
    extern "C" __declspec(dllexport) struct heif_context* heif_context_alloc(void);

    LIBHEIF_API
    extern "C" __declspec(dllexport) void heif_context_free(struct heif_context*);

    LIBHEIF_API
    extern "C" __declspec(dllexport) struct heif_error heif_context_read_from_file(struct heif_context*, const char* filename,
                                              const struct heif_reading_options*);

    ...

然后我将库编译成 DLL 文件并将其添加到我的 UWP 项目中。在 C# 代码中,我这样做了:

    namespace Libraries
    {
        public class PInvokeCode
        {
          [DllImport("ms-appx:///libheif.dll")]
          public static extern struct heif_context_alloc();

          [DllImport("ms-appx:///libheif.dll")]
          public static extern void heif_context_free(struct heif_context);

          [DllImport("ms-appx:///libheif.dll")]
          public static extern struct heif_error heif_context_read_from_file(struct heif_context*, const char* filename,
                                              const struct heif_reading_options*);

        }
    }

当然,在我的 UWP 中,我允许不安全的代码,并且我将 DLL 设置为内容。但是,我似乎仍然无法使代码正常工作。这是我第一次尝试这样的事情,我不知道我哪里出错了!

如果您需要,这里是libheif.dllheif.h 文件的链接。

【问题讨论】:

  • "...我似乎无法让代码工作...":您需要对此进行扩展,因为您所遇到的问题没有足够的详细信息有问题。
  • 根据您想要对 HEIF 图像执行的操作,您不必自己进行解码。如果您有最新版本的 Windows 和/或安装了适当的 WIC 编解码器,则可以使用 WPF 的 BitmapImage 加载 HEIF,例如:new BitmapImage(new Uri(@"D:\Downloads\surfer_1440x960.heic")),与 UWP 相同
  • @SimonMourier HEIF 容器中可以有多个图像(like this Sample file),我正在尝试使用我的代码访问这些图像和缩略图
  • @RichardCritten 我发现的事情是围绕简单任务(比如用 int 做数学)而不是做复杂任务(将 HEIC 文件推送到 DLL 并获得图像序列作为回报)我只是得到如下错误: DllImport 在此声明类型上无效。它仅对“方法”声明有效。修饰符“静态”和“外部”对此项无效
  • 我建议创建Windows Runtime Components 并引用libheif library 然后重新打包libheif api。将新的运行时组件添加到 uwp 项目中。

标签: c# c++ uwp pinvoke


【解决方案1】:
  1. 您必须确定应用程序是以 32 位还是 64 位运行的 申请。
  2. 然后确保 libheif.dll (main) 可以链接到位于同一目录 libde265.dll 的依赖 dll (解码器)和 libx265.dll(编码器)。
  3. 因此从 https://github.com/mcychan/libheif/releases/tag/1.6.2 如果有的话 上面提到的库不见了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2017-06-21
    • 2023-02-23
    • 1970-01-01
    • 2019-06-30
    相关资源
    最近更新 更多