如何使用java压缩图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44565500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I compress images using java?
提问by Darshit Patel
From pagespeed I am getting only image link and possible optimizations in bytes & percentage like, Compressing and resizing https://example.com/…ts/xyz.jpg?036861could save 212KiB (51% reduction). Compressing https://example.com/…xyz.png?303584508could save 4.4KiB (21% reduction).
从 pagespeed 我只得到图像链接和可能的字节和百分比优化,例如,压缩和调整https://example.com/...ts/xyz.jpg?036861可以节省 212KiB(减少 51%)。压缩https://example.com/...xyz.png?303584508可以节省 4.4KiB(减少 21%)。
For an example I have image of size 300kb and for this image pagespeed is displaying 100kb & 30% of reduction.
例如,我有一个大小为 300kb 的图像,对于这个图像,pagespeed 显示 100kb 和 30% 的减少。
This is only for one image but I am sure I will have lots of images for compression. so how can I compress image by passing bytes or percentage as a parameter or using anyother calculations in java (by using API or image-processing Tool) so,that I can get compressed version of image as suggested by google.
这仅适用于一张图像,但我相信我会有很多图像进行压缩。那么如何通过将字节或百分比作为参数传递或使用 Java 中的任何其他计算(通过使用 API 或图像处理工具)来压缩图像,以便我可以获得谷歌建议的图像压缩版本。
Thanks in advance.
提前致谢。
回答by fujy
You can use Java ImageIO
package to do the compression for many images formats, here is an example
您可以使用 JavaImageIO
包对多种图像格式进行压缩,这是一个示例
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.*;
public class Compresssion {
public static void main(String[] args) throws IOException {
File input = new File("original_image.jpg");
BufferedImage image = ImageIO.read(input);
File compressedImageFile = new File("compressed_image.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); // Change the quality value you prefer
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();
}
}
You can find more details about it here
您可以在此处找到有关它的更多详细信息
Also there are some third party tools like these
还有一些像这样的第三方工具
EDIT:If you want to use Google PageSpeed
in your application, it is available as web server module either for Apache or Nginx, you can find how to configure it for your website here
编辑:如果您想PageSpeed
在您的应用程序中使用 Google ,它可以作为 Apache 或 Nginx 的 Web 服务器模块使用,您可以在此处找到如何为您的网站配置它
https://developers.google.com/speed/pagespeed/module/
https://developers.google.com/speed/pagespeed/module/
But if you want to integrate the PageSpeed
C++ library in your application, you can find build instructions for it here.
但是如果你想PageSpeed
在你的应用程序中集成C++ 库,你可以在这里找到它的构建说明。
https://developers.google.com/speed/pagespeed/psol
https://developers.google.com/speed/pagespeed/psol
It also has a Java Client here
它也有一个 Java 客户端在这里
https://developers.google.com/api-client-library/java/apis/pagespeedonline/v1
https://developers.google.com/api-client-library/java/apis/pagespeedonline/v1
回答by Tim Cooper
There is colour compression ("compression quality") and there is resolution compression ("resizing"). Fujy's answer deals with compression quality, but this is not where the main savings come from: the main savings come from resizing down to a smaller size. E.g. I got a 4mb photo to 207K using the maximum compression quality using fujy's answer, and it looked awful, but I got it down to 12K using a reasonable quality but a smaller size.
有颜色压缩(“压缩质量”)和分辨率压缩(“调整大小”)。Fujy 的回答涉及压缩质量,但这不是主要节省的地方:主要节省来自缩小到更小的尺寸。例如,我使用 fujy 的答案使用最大压缩质量将 4mb 的照片压缩到 207K,看起来很糟糕,但我使用合理的质量但较小的尺寸将其降低到 12K。
So the above code should be used for "compression quality", but this is my recommendation for resizing:
所以上面的代码应该用于“压缩质量”,但这是我调整大小的建议:
https://github.com/rkalla/imgscalr/blob/master/src/main/java/org/imgscalr/Scalr.java
https://github.com/rkalla/imgscalr/blob/master/src/main/java/org/imgscalr/Scalr.java
I wish resizing was part of the standard Java libraries, but it seems it's not, (or there are image quality problems with the standard methods?). But Riyad's library is really small - it's just one class. I just copied this class into my project, because I never learnt how to use Maven, and it works great.
我希望调整大小是标准 Java 库的一部分,但似乎不是,(或者标准方法存在图像质量问题?)。但利雅得的图书馆真的很小——只有一个班级。我只是将这个类复制到我的项目中,因为我从未学习过如何使用 Maven,而且它运行良好。
回答by tgallei
As a solution for this problem I can recommend the API of TinyPNG.
You can use it for compressing as well as resizing the image.
作为这个问题的解决方案,我可以推荐 TinyPNG 的 API。
您可以使用它来压缩和调整图像大小。
Documentation:tinypng.com/developers/reference/java