C#.NET 中的 JPEG 2000 支持

发布于 2024-07-14 09:21:38 字数 128 浏览 11 评论 0原文

.NET 似乎无法使用 GDI 库打开 JP2 (Jpeg 2000) 文件。 我在谷歌上搜索过,但找不到任何库或示例代码来执行此操作。

有人有什么想法吗? 我真的不想花钱让图书馆来做这件事,除非我必须......

It seems that .NET can't open JP2 (Jpeg 2000) files using the GDI library. I've searched on google but can't find any libraries or example code to do this.

Anybody got any ideas? I don't really want to pay for a library to do it unless I have to..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

兮颜 2024-07-21 09:21:38

似乎我们可以使用 FreeImage (免费)来做到这一点

FIBITMAP dib = FreeImage.LoadEx("test.jp2");
//save the image out to disk    
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
//or even turn it into a normal Bitmap for later use
Bitmap bitmap = FreeImage.GetBitmap(dib);

Seems like we can do it using FreeImage (which is free)

FIBITMAP dib = FreeImage.LoadEx("test.jp2");
//save the image out to disk    
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
//or even turn it into a normal Bitmap for later use
Bitmap bitmap = FreeImage.GetBitmap(dib);
放血 2024-07-21 09:21:38

不久前我一直在寻找类似的东西,如果可以的话,我希望实现一个; 对我的问题的答复暗示没有记录了为 .Net 中的 Image 类使用的 GDI+ 执行此操作的方法。

我相信,如果您正在编写 WPF 应用程序,那么您可以通过 Windows 图像组件编解码器可能已经有一个(询问您本地友好的搜索引擎?)

有一个选项可以使用插件,例如 DotImage 支持 JPEG2000,尽管可能还有更多“加载图像所涉及的努力”。

I was looking for something similar a while back, with a view to implementing one if I could; The responses to my question imply that there is no documented method to do this for GDI+ which the Image class in .Net uses.

I believe that if you're writing a WPF application, then you can extend the list of supported image formats through Windows Imanging Components codecs, and there may be one out there already (ask your local friendly search engine?)

There is an option to use an addon such as DotImage which supports JPEG2000, although there may be more "effort" involved in loading images.

网白 2024-07-21 09:21:38

对于任何遇到这篇旧文章的人来说,Gordon 的上述代码效果很好,但正如 jixtra 指出的那样,您确实会得到一个异常: System.DllNotFoundException: '无法加载 DLL 'FreeImage': 找不到指定的模块。 ' 通过 nuget 安装时。 通过安装 FreeImage-dotnet-core nuget 包并手动将 FreeImage.dll 添加到 bin 文件夹,我可以让它在 .net 4.6.1 中工作。 您可以在此处下载 dll:http://freeimage.sourceforge.net/download.html

我需要更好质量的图像来与 tesseract 一起使用,因此我做了一些小的更改,这对新 jpeg 的质量产生了巨大的影响:

var jp2Format = FREE_IMAGE_FORMAT.FIF_JP2;
var dib = FreeImage.LoadEx("test.jp2", ref jp2Format);

FreeImage.SetResolutionX(dib, 300);
FreeImage.SetResolutionY(dib, 300);
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYSUPERB);

// NOTE: memory needs to be explicitly freed (GC won't do this)
FreeImage.UnloadEx(ref dib);

For anyone coming across this old post, the above code from Gordon works great, but as jixtra pointed out, you will indeed get an exception: System.DllNotFoundException: 'Unable to load DLL 'FreeImage': The specified module could not be found.' when installing via nuget. I was able to get it working in .net 4.6.1 by installing the FreeImage-dotnet-core nuget package and manually adding the FreeImage.dll to the bin folder. You can download the dll here: http://freeimage.sourceforge.net/download.html.

I needed a better quality image to use with tesseract so I made a few minor changes which made a huge difference to the quality of the new jpeg:

var jp2Format = FREE_IMAGE_FORMAT.FIF_JP2;
var dib = FreeImage.LoadEx("test.jp2", ref jp2Format);

FreeImage.SetResolutionX(dib, 300);
FreeImage.SetResolutionY(dib, 300);
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYSUPERB);

// NOTE: memory needs to be explicitly freed (GC won't do this)
FreeImage.UnloadEx(ref dib);
无妨# 2024-07-21 09:21:38

我使用 Leadtools 来显示 JPEG 2000 图像。 他们提供了一个 .NET 库,包括 WPF 和 WinForms 控件来显示图像。 然而,价格相当昂贵。

I've used Leadtools to display JPEG 2000 images. They provide a .NET library including WPF and WinForms controls to display the images. However, there is a reasonably steep price tag.

蓝戈者 2024-07-21 09:21:38

如果您需要没有不安全块的完全托管解决方案,您可以使用 Jpeg2000.Net 库。 免责声明:我正在开发这个库,该库是商业性的。

以下是将 JPEG 2000 图像解码为 TIFF 的基本示例:

string fileName = ...; // path to JPEG 2000 image
using (var image = new J2kImage(fileName))
{
    var options = new J2kDecodingOptions
    {
        UpsampleComponents = true
    };

    // Alternatively, you can decode only part of the image using J2kImage.DecodeArea method
    var imageData = image.Decode(options);
    imageData.Save(tiffFileName, J2kOutputFormat.Tiff);
}

You can use Jpeg2000.Net library if you need a fully managed solution without unsafe blocks. Disclaimer: I am working on this library, the library is commercial.

Here is the basic sample for decoding of JPEG 2000 image to TIFF:

string fileName = ...; // path to JPEG 2000 image
using (var image = new J2kImage(fileName))
{
    var options = new J2kDecodingOptions
    {
        UpsampleComponents = true
    };

    // Alternatively, you can decode only part of the image using J2kImage.DecodeArea method
    var imageData = image.Decode(options);
    imageData.Save(tiffFileName, J2kOutputFormat.Tiff);
}
橘虞初梦 2024-07-21 09:21:38

此外,对于当前最新的开源选项,您可以使用 Emgu CV,它是 OpenCV 的包装器。 C# 中的基本示例代码如下所示:

Mat image = CvInvoke.Imread(@"\Path\To\File.jp2");

Also, for a currently-up-to-date, open-source option you can use Emgu CV, which is a wrapper around OpenCV. Basic example code in C# looks like:

Mat image = CvInvoke.Imread(@"\Path\To\File.jp2");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文