gpt4 book ai didi

java - 类似于 TinyPNG for Java 的库 - 压缩图像大小

转载 作者:行者123 更新时间:2023-12-01 00:56:41 25 4
gpt4 key购买 nike

我正在开发一个允许用户上传图像并裁剪它们的网站。
我将每个图像都转换为 .PNG 以获得更好的质量。我遇到的问题是图片大小。

如果我上传一个 200 kb 的图像,在裁剪并使其成为 PNG 后,它有 600 kb。这对我来说不是一个解决方案,因为图像作为 BLOB 存储在数据库中,并且网站加载速度较慢。

我正在尝试找到一种方法来压缩 png,使其尺寸更小,而不会降低质量。

我找不到这个问题的任何库或解决方案。我需要像 TinyPNG 这样的 Java 工具。

我就是这样做的:

    BufferedImage resizedImage = resizeImage(image,extension,width,height); 
System.out.println("dimensiuni:" + resizedImage.getHeight()+ "x" + resizedImage.getWidth());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( resizedImage, "png", baos );
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();

这是 resizeImage 函数:
    public BufferedImage resizeImage(BufferedImage image, String extension, int targetWidth, int targetHeight) {


int type = (image.getTransparency() == Transparency.OPAQUE) ?
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;

BufferedImage ret = (BufferedImage)image;
int w, h;

w = image.getWidth();
h = image.getHeight();


do {
if (w > targetWidth) {
w /= 2;
if (w < targetWidth) {
w = targetWidth;
}
}

if ( h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
}

BufferedImage tmp = new BufferedImage(w, h, type);
Graphics2D g2 = tmp.createGraphics();
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_DEFAULT);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);


g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();

ret = tmp;

tmp.flush();


} while (w != targetWidth || h != targetHeight);

return ret;
}

帮我!!

最佳答案

tinypng他们在网站上解释了它是如何工作的问题,即他们如何压缩图像,如下所示:

How does it work?

Excellent question! When you upload a PNG (Portable Network Graphics) file, similar colors in your image are combined. This technique is called “quantization”. By reducing the number of colors, 24-bit PNG files can be converted to much smaller 8-bit indexed color images. All unnecessary metadata is stripped too. The result better PNG files with 100% support for transparency. Have your cake and eat it too!



绝对他们为此使用了一些库,

您可以使用 http://pngquant.org/用于压缩或缩小图像大小,

在另一个 SO 问题中,有人回答 tinypng也使用 pngquant库,但我不知道它的真相有多远,但你也可以在 Java 中使用这个库,

在 pngquant 网站上,他们提供了 java 库链接,看一下,我提供了相同的 github网站中提供的存储库链接:

https://github.com/ImageOptim/libimagequant/tree/master/org/pngquant

基本上上面的库是用 c/c++写的所以它使用JNI(Java Native Interface)连接 c/c++的接口(interface)与 Java .

关于java - 类似于 TinyPNG for Java 的库 - 压缩图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25032328/

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