使用java将图像大小MB缩小到KB

时间:2016-08-08 06:51:27

标签: java image jai

我需要将图像尺寸MB减小到KB而不使用图像质量,因此我按照此链接decrease image resolution in java

但是,它会抛出错误

    Error: One factory fails for the operation "jpeg"
    Occurs in: javax.media.jai.ThreadSafeOperationRegistry
    java.lang.reflect.InvocationTargetException
        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 javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
        at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
        at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
        at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:110)
        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 javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
        at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
        at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
        at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:830)
        at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
        at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:899)
        at ImageCompressor.compress(ImageCompressor.java:31)
        at ImageCompressor.main(ImageCompressor.java:46)
    Caused by: java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException
        at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:53)
        at com.sun.media.jai.opimage.CodecRIFUtil.create(CodecRIFUtil.java:96)
        at com.sun.media.jai.opimage.JPEGRIF.create(JPEGRIF.java:52)
        ... 22 more
    Caused by: java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.ImageFormatException
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 25 more
    Error: One factory fails for the operation "stream"
    Occurs in: javax.media.jai.ThreadSafeOperationRegistry
    java.lang.reflect.InvocationTargetException
        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 javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
        at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
        at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
        at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:830)
        at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
        at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:899)
        at ImageCompressor.compress(ImageCompressor.java:31)
        at ImageCompressor.main(ImageCompressor.java:46)
    Caused by: javax.media.jai.util.ImagingException: All factories fail for the operation "jpeg"
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1695)
        at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
        at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
        at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:110)
        ... 13 more
    Caused by: java.lang.reflect.InvocationTargetException
        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 javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
        ... 16 more
    Caused by: java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException
        at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:53)
        at com.sun.media.jai.opimage.CodecRIFUtil.create(CodecRIFUtil.java:96)
        at com.sun.media.jai.opimage.JPEGRIF.create(JPEGRIF.java:52)
        ... 22 more
    Caused by: java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.ImageFormatException
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 25 more
    Exception in thread "main" javax.media.jai.util.ImagingException: All factories fail for the operation "stream"
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1695)
        at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
        at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
        at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:830)
        at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
        at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:899)
        at ImageCompressor.compress(ImageCompressor.java:31)
        at ImageCompressor.main(ImageCompressor.java:46)
    Caused by: java.lang.reflect.InvocationTargetException
        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 javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
        ... 7 more
    Caused by: javax.media.jai.util.ImagingException: All factories fail for the operation "jpeg"
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1695)
        at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
        at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
        at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:110)
        ... 13 more
    Caused by: java.lang.reflect.InvocationTargetException
        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 javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
        ... 16 more
    Caused by: java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException
        at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:53)
        at com.sun.media.jai.opimage.CodecRIFUtil.create(CodecRIFUtil.java:96)
        at com.sun.media.jai.opimage.JPEGRIF.create(JPEGRIF.java:52)
        ... 22 more
    Caused by: java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.ImageFormatException
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 25 more
    Caused by:
    java.lang.reflect.InvocationTargetException
        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 javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
        at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
        at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
        at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:830)
        at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
        at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:899)
        at ImageCompressor.compress(ImageCompressor.java:31)
        at ImageCompressor.main(ImageCompressor.java:46)
    Caused by: javax.media.jai.util.ImagingException: All factories fail for the operation "jpeg"
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1695)
        at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
        at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
        at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:110)
        ... 13 more
    Caused by: java.lang.reflect.InvocationTargetException
        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 javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
        at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
        ... 16 more
    Caused by: java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException
        at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:53)
        at com.sun.media.jai.opimage.CodecRIFUtil.create(CodecRIFUtil.java:96)
        at com.sun.media.jai.opimage.JPEGRIF.create(JPEGRIF.java:52)
        ... 22 more
    Caused by: java.lang.ClassNotFoundException: com.sun.image.codec.jpeg.ImageFormatException
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 25 more

我添加了jai_codec.jar,jai_core.jar,mlibwrapper_jai.jar。

java版:java-1.8.0-openjdk-1.8.0.71

os:fedora 23。

建议我如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

常规

您如何在不降低图像质量的情况下减小尺寸?较小的图像具有较少的像素,因此它们不能提供较大图像的所有信息。因此,质量必须随着尺寸(通常)而减小。

如果你想保持尽可能多的图像质量,你也不应该选择JPG,因为JPG的想法是通过丢失接缝不那么重要的信息进行压缩。 PNG甚至GIF(GIF仅适用于最多256种颜色)都是无损压缩,但在大多数情况下,文件大小不会像JPG那样缩小。

解决您的问题:

Open JDK似乎在JPG压缩方面存在问题。所以我的建议是切换到oracle JDK。