Java:压缩JPEG图像

时间:2015-08-10 08:11:29

标签: java image-processing javax.imageio

我尝试使用codegeeks

中的代码压缩JPEG

这是我实施的内容,

   public File compressJPEGImage(String filePath, float ratio) {

    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter jpegWriter = writers.next();

    // Set the compression quality to float value like 0.8
    ImageWriteParam param = jpegWriter.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionQuality(ratio);

    // Write the image to a file
    File filepath = new File(filePath + "/" + imageModel.getId() + ".jpeg");
    FileImageOutputStream out = null;
    try {
        out = new FileImageOutputStream(filepath);
        jpegWriter.setOutput(out);
        jpegWriter.write(null, new IIOImage(image, null, null), param);
        jpegWriter.dispose();
        out.close();
    } catch (IOException e) {
        log.error("Error Compressingthe JPEG image" + e.getMessage());
    }
    return filepath;
}

当我写一个测试用例来验证它时。我得到以下例外,任何人都可以修复此错误

java.lang.UnsatisfiedLinkError: com.sun.imageio.plugins.jpeg.JPEGImageWriter.initWriterIDs(Ljava/lang/Class;Ljava/lang/Class;)V
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.initWriterIDs(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.<clinit>(JPEGImageWriter.java:186)
at com.sun.imageio.plugins.jpeg.JPEGImageWriterSpi.createWriterInstance(JPEGImageWriterSpi.java:96)
at javax.imageio.spi.ImageWriterSpi.createWriterInstance(ImageWriterSpi.java:351)
at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:843)
at javax.imageio.ImageIO$ImageWriterIterator.next(ImageIO.java:827)
at com.quixey.media.service.converter.ImageConversion.compressJPEGImage(ImageConversion.java:111)
at com.quixey.media.service.converter.ImageConversionTest.test_CompressImage(ImageConversionTest.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

1 个答案:

答案 0 :(得分:3)

如果你谷歌

 java.lang.UnsatisfiedLinkError: com.sun.imageio.plugins.jpeg.JPEGImageWriter.initWriterID` 

你会发现这是Open JDK 8 versions中的已知错误。

作为一种解决方法,您可以使用JDK 7,或尝试切换到更高版本的JDK 8。