0

I have a .jp2 image file that I want to convert to .jpg.

    BufferedImage background = ImageIO.read(new File("images\\"
    + randNum + ".jp2"));
    ImageIO.write(background, "jpg", new File("images\\" + randNum
                + ".jpg"));

I have got this exception :

java.util.ServiceConfigurationError: javax.imageio.spi.ImageWriterSpi:  Provider com.github.jaiimageio.jpeg2000.impl.J2KImageWriterSpi could not be instantiated
 ...
Caused by: java.lang.NoClassDefFoundError: com/github/jaiimageio/impl/common/PackageUtil
 ...
Caused by: java.lang.ClassNotFoundException: com.github.jaiimageio.impl.common.PackageUtil
TiyebM
  • 2,684
  • 3
  • 40
  • 66

2 Answers2

1

Apparently, a conflict occured, I was using classes from different libraries, here I had both jai_imageio and jai-imageio-jpeg2000, I solved this problem by simply removing one of them.

TiyebM
  • 2,684
  • 3
  • 40
  • 66
0

I run this code and it created a new jpg file. I hope it would help you.

package yourPackage;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class ImageConverter {


    public static void main(String[] args) throws IOException {
        int randNum = 1;
        convertImage(randNum);      

    }

    private static void convertImage(int randNum) throws IOException {
        try {
            File foundFile = new File("c:\\images\\" + randNum + ".jp2");   
            BufferedImage background = ImageIO.read(foundFile);
            ImageIO.write(background, "jpg", new File("c:\\images\\" + randNum + ".jpg"));
            System.out.println("jpg file is generated");
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("No file " + randNum +".jp2 found");
        }

    }
}
erolkaya84
  • 1,769
  • 21
  • 28
  • I am still getting the same error and not jpg file is created, do you think there is a conflict in the imports? – TiyebM Aug 23 '16 at 14:01
  • @TiyebBellal I've updated my answer. I hope that would work for you. – erolkaya84 Aug 23 '16 at 14:05
  • I have tried using your method and after debug I found that the exception occurs on this instruction: `BufferedImage background = ImageIO.read(foundFile);` – TiyebM Aug 23 '16 at 14:10
  • @TiyebBellal if you have a file in that specified location, then it should find the file. – erolkaya84 Aug 23 '16 at 14:12
  • I do have the jp2 file not the jpg, but as the error says :J2KImageWriterSpi could not be instantiated, I think it is about the library or access restrictions on it – TiyebM Aug 23 '16 at 14:20
  • Thank you for the effort, finally I removed jai-imageio-jpeg2000 library and it worked! – TiyebM Aug 23 '16 at 17:04