I need to convert heic image to jpg image in java.
I need to perform it locally using java without any propriety package or calling external api to convert it. So far tried with twelevemonkey packages and drew packages. Either packages are not available or code that i frequently find online isn't correct below is one of the code i tried:
public class HeicToJpgConverter {
public static void main(String[] args){
try {
// Get the path to the HEIC/HEIF image file.
String heicPath = "input.heic";
// Read the image metadata.
Metadata metadata = ImageMetadataReader.readMetadata(new File(heicPath));
// Get the ExifIFD0 directory.
List<com.drew.metadata.Directory> exifDirectory = (List<com.drew.metadata.Directory>) metadata.getDirectories();
System.out.println("exifDirectory:" + exifDirectory );
System.out.println("exifDirectory size:" + exifDirectory.size() );
// Get the image width and height.+
int width = exifDirectory.get(1).getInt(ExifIFD0Directory.TAG_IMAGE_WIDTH);
int height = exifDirectory.get(1).getInt(ExifIFD0Directory.TAG_IMAGE_HEIGHT);
// Create a new JPG image with the same width and height.
BufferedImage jpgImage = new BufferedImage(255, 255, BufferedImage.TYPE_INT_RGB);
// Convert the HEIC/HEIF image to JPG.
//ImageConverter.convertHeicToJpg(heicPath, jpgImage);
// Save the JPG image.
File jpgFile = new File("output.jpg");
ImageIO.write(jpgImage, "jpg", jpgFile);
} catch (MetadataException | IOException e) {
throw new RuntimeException(e);
} catch (ImageProcessingException e) {
throw new RuntimeException(e);
}
}
following is the error
Task :HeicToJpgConverter.main() FAILED
exifDirectory:[HEIF Directory (3 tags), HEIF Directory (2 tags), File Type Directory (4 tags), File Directory (3 tags)]
exifDirectory size:4
Exception in thread "main" java.lang.RuntimeException: com.drew.metadata.MetadataException: Tag 'Unknown tag (0x0100)' has not been set -- check using containsTag() first
at org.sample.rd.HeicToJpgConverter.main(HeicToJpgConverter.java:46)
Caused by: com.drew.metadata.MetadataException: Tag 'Unknown tag (0x0100)' has not been set -- check using containsTag() first
at com.drew.metadata.Directory.getInt(Directory.java:440)
at org.sample.rd.HeicToJpgConverter.main(HeicToJpgConverter.java:31)
Caused by: com.drew.metadata.MetadataException: Tag 'Unknown tag (0x0100)' has not been set -- check using containsTag() first
Execution failed for task ':HeicToJpgConverter.main()'.
> Process 'command 'C:/Users/raviraj.sankolli/.jdks/temurin-1.8.0_362/bin/java.exe'' finished with non-zero exit value 1
I dont want to use im4java as i cannot install ImageMagick Any help is appreciated
Thanks