簡體   English   中英

在 Java 中調整圖像大小以減小圖像大小

[英]Image Resizing in Java to reduce image size

我有一個尺寸為 800x800 的圖像,其大小為 170 kb。 我想將此圖像調整為 600x600。 調整大小后,我希望縮小圖像大小。 我怎樣才能做到這一點?

您不需要一個完整的圖像處理庫來簡單地調整圖像大小。

推薦的方法是使用漸進式雙線性縮放,就像這樣(在您的代碼中隨意使用此方法):

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;

}

Filthy Rich Clients修改和清理原始代碼。


根據您的評論,您可以降低質量並對 JPEG 字節進行編碼,如下所示:

image是緩沖image

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();

現在,由於osByteArrayOutputStream ,您可以對它進行 Base64 編碼。

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

您可以使用 100% Java、Apache2 開源imgscalr 庫(單個靜態類)並執行以下操作:

import org.imgscalr.Scalr.*; // static imports are awesome with the lib

... some class code ...

// This is a super-contrived method name, I just wanted to incorporate
// the details from your question accurately.
public static void resizeImageTo600x600(BufferedImage image) {
    ImageIO.write(resize(image, 600), "JPG", new File("/path/to/file.jpg"));
}

注意:如果上面看起來很奇怪,靜態導入允許我直接使用resize調用而不指定Scalr.resize(...)

此外,如果寫出的縮放圖像的質量看起來不夠好(雖然它會很快寫出),您可以對resize方法使用更多參數,如下所示:

public static void resizeImageTo600x600(BufferedImage image) {
    ImageIO.write(resize(image, Method.ULTRA_QUALITY, 600), "JPG", new File("/path/to/file.jpg"));
}

.. 你甚至可以對結果應用一個 BufferedImageOp 來柔化它,以防縮小使圖像看起來鋸齒狀:

public static void resizeImageTo600x600(BufferedImage image) {
    ImageIO.write(resize(image, Method.ULTRA_QUALITY, 600, Scalr.OP_ANTIALIAS), "JPG", new File("/path/to/file.jpg"));
}

您只需在 Maven POM 中添加以下 dep 條目(imgscalr 在 Maven 中央存儲庫中)即可開始使用該庫:

<dependency> 
  <groupId>org.imgscalr</groupId>
  <artifactId>imgscalr-lib</artifactId>
  <version>4.2</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>

使用一些支持通過 Java 進行圖像處理的良好框架,例如IimageJ

Java 中還通過一些類(例如MemoryImageSourcePixelGrabber )提供了一些基本支持。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM