图像在Java中调整大小以减小图像大小 [英] Image Resizing in Java to reduce image size

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

问题描述

我有一张说尺寸为800x800的图片,尺寸为170 kb。我想调整此图像的大小,说600x600。调整大小后,我希望减少图像大小。我该怎么做?

I have an Image of say dimensions 800x800 which has a size of 170 kb. I want to resize this image to say 600x600. After resizing i want the image size to be reduced. How can i do this?

推荐答案

您不需要一个完整的图像处理库来简单地调整图像大小。

You don't need a full blown Image processing library for simply resizing an image.

推荐的方法是使用渐进双线性缩放,就像这样(随意使用此代码中的方法):

The recommended approach is by using progressive bilinear scaling, like so (feel free to use this method as is in your code):

public BufferedImage scale(BufferedImage img, int targetWidth, int targetHeight) {

    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;
    BufferedImage scratchImage = null;
    Graphics2D g2 = null;

    int w = img.getWidth();
    int h = img.getHeight();

    int prevW = w;
    int prevH = h;

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

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

        if (scratchImage == null) {
            scratchImage = new BufferedImage(w, h, type);
            g2 = scratchImage.createGraphics();
        }

        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(ret, 0, 0, w, h, 0, 0, prevW, prevH, null);

        prevW = w;
        prevH = h;
        ret = scratchImage;
    } while (w != targetWidth || h != targetHeight);

    if (g2 != null) {
        g2.dispose();
    }

    if (targetWidth != ret.getWidth() || targetHeight != ret.getHeight()) {
        scratchImage = new BufferedImage(targetWidth, targetHeight, type);
        g2 = scratchImage.createGraphics();
        g2.drawImage(ret, 0, 0, null);
        g2.dispose();
        ret = scratchImage;
    }

    return ret;

}

肮脏的富客户端

基于在您的评论中,您可以降低质量并编码JPEG字节,如下所示:

Based on your comment, you can reduce quality and encode JPEG bytes like so:

image 是BufferedImage。

image is the BufferedImage.

ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next();

ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.2f); // Change this, float between 0.0 and 1.0

writer.setOutput(ImageIO.createImageOutputStream(os));
writer.write(null, new IIOImage(image, null, null), param);
writer.dispose();

现在,因为 os ByteArrayOutputStream ,你可以对它进行Base64编码。

Now, since os is a ByteArrayOutputStream, you can Base64 encode it.

String base64 = Base64.encode(os.toByteArray());

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

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