How to manipulate HEIF images in java for viewing, metadata, and conversion without paid APIs?

An application that needs to handle .heic (High Efficiency Image File) images, but I’m encountering significant challenges. I attempted to use the Nokia HEIF library (HEIF - High Efficiency Image File Format), but my success has been limited. While I can load an image in the HEIF format and save it, that’s all I’m able to accomplish. I can’t extract metadata, view the image, or convert it to another format. This issue persists even with various images provided by the library’s creators, so it seems unrelated to any specific image file.

My question is, is there an effective way to manipulate HEIF images in Java? I am aware of some paid APIs but I’m looking for a solution that doesn’t involve such services. Specifically, I need functionalities like viewing the image, extracting metadata, and converting the image to other formats, all within the Java environment.

1 Like

@roman Working with HEIF images in Java can indeed be challenging due to the format’s complexity. If the Nokia HEIF library isn’t meeting your needs, you might need to look at alternative libraries or solutions.

One option is to explore libraries like libheif, a more comprehensive library for handling HEIF images. It’s written in C, but you can use Java Native Interface (JNI) to integrate it into your Java application.

How would I integrate a C library like libheif with my Java application using JNI?

Integrating libheif via JNI involves writing some native C code that interfaces with the libheif library and then creating corresponding Java native methods. Here’s a high-level overview of the steps:

  1. Write C functions that use libheif functionalities to load, process, and convert HEIF images.
  2. Compile this C code into a shared library (.dll, .so, or .dylib depending on your platform).
  3. In your Java application, declare native methods corresponding to the C functions.
  4. Load the shared library at runtime in your Java application using System.loadLibrary.

This process allows your Java application to call functions written in C, utilizing libheif.

I hope this may resolve your query.

Thank you

Is there a more straightforward method or a Java library that can handle HEIF images directly?

Direct handling of HEIF images in Java is somewhat limited due to the format’s complexity and lack of widespread support in standard imaging libraries. However, you could periodically check for updates or new releases in popular Java imaging libraries, as support for new formats like HEIF is continually evolving.

As of now, using a JNI approach with a C library like libheif is one of the more viable solutions for comprehensive HEIF image manipulation in Java.

Thank you