2012-10-14 332 views
6

我有一個尺寸爲800x800的圖像,它的大小爲170 kb。我想調整這個圖像的大小,說600x600。調整大小後,我希望縮小圖像大小。我怎樣才能做到這一點?在Java中調整圖像大小以縮小圖像大小

+0

你可以看看http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928 – MadProgrammer

+0

1)*「調整大小後我想要圖像大小要減小。「*當存儲在內存中,或序列化爲PNG,JPG或GIF?第一個將導致字節數的減少,第二個通常不會。 2)圖像的格式是什麼? (PNG,GIF,JPEG ..) –

+0

如果格式爲JPEG,則通過提高壓縮率來減少磁盤上的圖像字節大小,最有可能更有效。有關示例源代碼,請參見['ImageCompressionDemo'](http://stackoverflow.com/a/5998015/418556)。 –

回答

13

你不需要一個完整的圖像處理庫來調整圖像大小。

推薦的方法是使用漸進雙線性縮放,像這樣(隨意使用這種方法是在你的代碼):

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它是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編碼。

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

我想調整大小後縮小圖像大小(以字節爲單位),以便我可以通過套接字發送它。 dataoutputstream說編碼的字符串(Base64)太大。 – Madeyedexter

+0

Base64編碼JPEG字節。你可以使用'ImageIO'來做到這一點。 – LanguagesNamedAfterCofee

+0

@丹尼看到我的編輯 – LanguagesNamedAfterCofee

3

使用一些支持Java處理圖像處理的好框架。 IamageJ

Java中還有一些基本支持,如MemoryImageSourcePixelGrabber

+0

我檢查出來了。我想在我的Java程序中執行此操作! – Madeyedexter

+0

我使用Core Java中的基本支持信息更新了答案。希望有所幫助。 –

5

您可以使用100%的Java,Apache2的開源imgscalr library(單一靜態類),做這樣的事情:

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")); 
} 

注意:如果上面看起來很奇怪,靜進口允許我使用的調整大小直接調用,而無需指定Scalr.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(imgscalr以下DEP項與庫玩弄在Maven中央回購):

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

我不明白許可證。我可以在個人網站上使用它嗎? – Ced

+0

另外如果圖像更小,它會放大嗎? – Ced