gpt4 book ai didi

Java:压缩 JPEG 图像

转载 作者:行者123 更新时间:2023-11-29 05:01:43 25 4
gpt4 key购买 nike

我尝试使用从 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)

最佳答案

如果你用谷歌搜索

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

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

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

关于Java:压缩 JPEG 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31914631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com