可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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"); } } }
回答2:
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.