如何在java中减小图像缩略图大小 [英] How to decrease image thumbnail size in java

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

问题描述

我有一个大小为 4.7kb 的图片 a.jpg,当它上传到网络服务器时,它的大小变成了 15.5KB ...

I have an image a.jpg with size 4.7kb, when its upload to web-server, its size become 15.5KB ...

我正在使用以下代码.

BufferedImage bi = ImageIO.read(business.getImage()); //business is sturts2 Form & image instance of File class
            int height = bi.getHeight();
            int width = bi.getWidth();

            if (height > Constants.BIZ_IMAGE_HEIGHT || width > Constants.BIZ_IMAGE_WIDTH) {
                height = Constants.BIZ_IMAGE_HEIGHT;
                width = Constants.BIZ_IMAGE_WIDTH;
            }

            InputStream is = UtilMethod.scaleImage(new FileInputStream(business.getImage()), width, height);
            File f = new File(businessImagesPath, business.getImageFileName());
            UtilMethod.saveImage(f, is);
            is.close();

UtilMethod.scaleImage(..) ... 如下:

public static InputStream scaleImage(InputStream p_image, int p_width, int p_height) throws Exception {

    InputStream imageStream = new BufferedInputStream(p_image);
    Image image = (Image) ImageIO.read(imageStream);

    int thumbWidth = p_width;
    int thumbHeight = p_height;

    // Make sure the aspect ratio is maintained, so the image is not skewed
    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double imageRatio = (double) imageWidth / (double) imageHeight;
    if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
    } else {
        thumbWidth = (int) (thumbHeight * imageRatio);
    }

    // Draw the scaled image
    BufferedImage thumbImage = new BufferedImage(thumbWidth,
            thumbHeight, BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics2D = (Graphics2D) thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, Color.WHITE, null);


    // Write the scaled image to the outputstream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);

    int quality = 85; // Use between 1 and 100, with 100 being highest quality
    quality = Math.max(0, Math.min(quality, 100));

    param.setQuality((float) quality / 100.0f, false);
    encoder.setJPEGEncodeParam(param);        
    encoder.encode(thumbImage);
    ImageIO.write(thumbImage, "png", out);

    ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());
    return bis;
}

使用java保存图像时的任何其他尺寸和质量优化想法.我正在使用 struts2 MVC ... 非常感谢你.

推荐答案

int quality = 85; // Use between 1 and 100, with 100 being highest quality

对于 JPEG 缩略图来说,这似乎是高质量的.尝试大约 60 或 50.

This seems like a high quality for a JPEG thumbnail. Try around 60 or 50.

quality = Math.max(0, Math.min(quality, 100));

嗯?

param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);        

好的..

encoder.encode(thumbImage);
ImageIO.write(thumbImage, "png", out);

但是啊?为什么要设置 JPEGEncodeParam 并存储为 PNG?这甚至有任何影响吗?试试..

But huh? Why set a JPEGEncodeParam and store as a PNG? Does that even have any effect? Try..

ImageIO.write(thumbImage, "jpg", out);

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

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