如何使用java压缩图像? [英] How can I compress images using java?

查看:149
本文介绍了如何使用java压缩图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从pagespeed我只获得图像链接和可能的字节数和优化;百分比如,
压缩和调整大小 https://example.com /...ts/xyz.jpg?036861 可以节省212KiB(减少51%)。
压缩 https://example.com/...xyz.png?303584508 可以节省4.4KiB(减少21%)。

From pagespeed I am getting only image link and possible optimizations in bytes & percentage like, Compressing and resizing https://example.com/…ts/xyz.jpg?036861 could save 212KiB (51% reduction). Compressing https://example.com/…xyz.png?303584508 could save 4.4KiB (21% reduction).

例如,我有300kb大小的图像,对于这个图像,pagespeed显示100kb&减少30%。

For an example I have image of size 300kb and for this image pagespeed is displaying 100kb & 30% of reduction.

这仅适用于一张图片,但我相信我会有很多图像用于压缩。
所以如何通过将字节或百分比作为参数或使用java
中的任何其他计算(通过使用API​​或图像处理工具)来压缩图像,这样我就可以获得图像的压缩版本通过谷歌。

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 ImageIO 包对许多图像格式进行压缩,这里有一个例子

You can use Java ImageIO package to do the compression for many images formats, here is an example

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

  • https://collicalex.github.io/JPEGOptimizer/
  • https://github.com/depsypher/pngtastic

编辑:如果你想使用Google PageSpeed 在您的应用程序中,它可用作Apache或Nginx的Web服务器模块,您可以在此处找到如何为您的网站配置

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

https://developers.google.com/speed/pagespeed/ module /

但如果你想整合 PageSpeed 您的应用程序中的C ++库,您可以在此处找到它的构建说明。

But if you want to integrate the PageSpeed C++ library in your application, you can find build instructions for it here.

https://developers.google.com/speed/pagespeed/psol

它还有一个Java此处的客户

It also has a Java Client here

https://developers.google.com/api-client-library/java/apis/pagespeedonline/v1

这篇关于如何使用java压缩图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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