任何图像格式的java图像压缩(jpg,PNG,gif) [英] java image compression for any image format(jpg, PNG, gif)

查看:545
本文介绍了任何图像格式的java图像压缩(jpg,PNG,gif)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面给出的代码来对jpeg图像进行图像压缩。

I am using the code given below for image compression of jpeg image.

File input = new File("digital_image_processing.jpg");
BufferedImage image = ImageIO.read(input);

File compressedImageFile = new File("compress.jpg");  
OutputStream os = new FileOutputStream(compressedImageFile);

 Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpg");
 ImageWriter writer = (ImageWriter) writers.next();

 ImageOutputStream ios = ImageIO.createImageOutputStream(os);
 writer.setOutput(ios);

 ImageWriteParam param = writer.getDefaultWriteParam();

 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
 param.setCompressionQuality(0.05f);

 writer.write(null, new IIOImage(image, null, null), param);

但是当我通过更改ImageIO.getImageWritersByFormatName(png)来尝试PNG格式时;
它给我一个不支持压缩的错误。

But when I tried for for PNG format by changing ImageIO.getImageWritersByFormatName("png"); It is giving me error that compression not supported.

那么我应该如何修改上面的代码以支持所有图像格式压缩

So how should i modify the above code so as to support all image format compression

推荐答案

您的代码的问题在于您使用了ImageWriter.getDefaultWriteParam()。来自ImageWriter.java的以下引用:

The problem with your code is that you used ImageWriter.getDefaultWriteParam(). From the following quote from ImageWriter.java:


public ImageWriteParam getDefaultWriteParam()

public ImageWriteParam getDefaultWriteParam()

为包含默认值的
文件格式返回相应类型的新ImageWriteParam对象,即,如果未指定ImageWriteParam对象,则将使用
的值。这是
作为调整一些参数和
的起点非常有用,否则单独保留默认设置。

Returns a new ImageWriteParam object of the appropriate type for this file format containing default values, that is, those values that would be used if no ImageWriteParam object were specified. This is useful as a starting point for tweaking just a few parameters and otherwise leaving the default settings alone.

默认实现构造并返回一个新的
ImageWriteParam对象,不允许平铺,渐进式
编码或压缩,并且将针对当前的
区域设置进行本地化(即,通过调用新的$ b $将获得的内容) b ImageWriteParam(getLocale())。

The default implementation constructs and returns a new ImageWriteParam object that does not allow tiling, progressive encoding, or compression, and that will be localized for the current Locale (i.e., what you would get by calling new ImageWriteParam(getLocale()).

单个插件可以返回ImageWriteParam的实例,并启用
其他可选功能,或者它们可能返回实例
ImageWriteParam的插件特定子类。

Individual plug-ins may return an instance of ImageWriteParam with additional optional features enabled, or they may return an instance of a plug-in specific subclass of ImageWriteParam.

实际行为取决于特定ImageWriteParam的个别实现。我相信原因JPG图像的工作原理是JPG的ImageWriteParam为默认的ImageWriteParam设置了 canWriteCompressed protected字段,但对于PNG图像,由于某些原因它没有不要这样做。

The actual behavior depends on individual implementation of specific ImageWriteParam. I believe the reason JPG image works is that the ImageWriteParam for JPG set the canWriteCompressed protected field for the default ImageWriteParam but for PNG image, for some reason it doesn't do that.

如果你看一下com.sun.imageio.plugins.png.PNGImageWriteParam.java,你会发现它确实没有设置它。

If you look at the com.sun.imageio.plugins.png.PNGImageWriteParam.java, you will find it indeed doesn't set it.

为了使你的代码能够正常工作,你可以这样做:

In order to make your code work generally, you can do like this:

  File input = new File("digital_image_processing.jpg");
  BufferedImage image = ImageIO.read(input);

  File compressedImageFile = new File("compress.jpg");  
  OutputStream os = new FileOutputStream(compressedImageFile);

  Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpg");
  ImageWriter writer = (ImageWriter) writers.next();

  ImageOutputStream ios = ImageIO.createImageOutputStream(os);
  writer.setOutput(ios);

  ImageWriteParam param = writer.getDefaultWriteParam();
  // Check if canWriteCompressed is true
  if(param.canWriteCompressed()) {
     param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
     param.setCompressionQuality(0.05f);
  }
  // End of check
  writer.write(null, new IIOImage(image, null, null), param);

更新:我试图找到一种方法让你很好-tune在ImageIO框架内压缩PNG图像。但不幸的是,鉴于目前的实施,似乎几乎不可能。 Java PNG编写器插件具有固定的过滤 - 自适应过滤和固定的deflater级别 - 9这是它可以做的最好的事情。

Update: I was trying to find out a way for you to fine-tune PNG image compression within the framework of ImageIO. But unfortunately, given the current implementation, it seems next to impossible. Java PNG writer plugin has fixed filtering - adaptive filtering and fixed deflater level - 9 which is the best thing it could do.

这篇关于任何图像格式的java图像压缩(jpg,PNG,gif)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆