2014-10-20 78 views
0

我們通過獲取JPEG縮略圖數據成功調整了圖像的大小,但是某些圖像沒有。在沒有使用ImageIO的情況下在java中調整圖像尺寸

解決辦法就是調整圖像大小,以能夠創建一個縮略圖,但只能使用Java 1.4及以下(所以我不能使用ImageIO的,因爲設備不支持它)

是有沒有解決我的問題如何使用ImageIO調整圖像大小?

+0

您可以用'Image.getScaledInstance'檢索一個帶'java.awt.Image'大小的實例。 – John 2014-10-20 08:36:56

+0

爲什麼Java 1.4會導致「OutOfMemoryException」? – haraldK 2014-10-20 09:05:30

+0

@haraldK因爲我們使用它的設備沒有那麼多的內存。 – 2014-10-20 09:18:04

回答

0

java.awt.Image.getScaledInstance(width, height, hints)返回圖像的新縮放實例。

最後一個參數hints更改所使用的縮放方法,這將修改最終的外觀。請參閱下面的代碼段,直接從JDK源代碼中獲取。

/** 
* Use the default image-scaling algorithm. 
* @since JDK1.1 
*/ 
public static final int SCALE_DEFAULT = 1; 

/** 
* Choose an image-scaling algorithm that gives higher priority 
* to scaling speed than smoothness of the scaled image. 
* @since JDK1.1 
*/ 
public static final int SCALE_FAST = 2; 

/** 
* Choose an image-scaling algorithm that gives higher priority 
* to image smoothness than scaling speed. 
* @since JDK1.1 
*/ 
public static final int SCALE_SMOOTH = 4; 

/** 
* Use the image scaling algorithm embodied in the 
* <code>ReplicateScaleFilter</code> class. 
* The <code>Image</code> object is free to substitute a different filter 
* that performs the same algorithm yet integrates more efficiently 
* into the imaging infrastructure supplied by the toolkit. 
* @see  java.awt.image.ReplicateScaleFilter 
* @since  JDK1.1 
*/ 
public static final int SCALE_REPLICATE = 8; 

/** 
* Use the Area Averaging image scaling algorithm. The 
* image object is free to substitute a different filter that 
* performs the same algorithm yet integrates more efficiently 
* into the image infrastructure supplied by the toolkit. 
* @see java.awt.image.AreaAveragingScaleFilter 
* @since JDK1.1 
*/ 
public static final int SCALE_AREA_AVERAGING = 16; 
+0

根據我的研究,我必須爲BufferedImage使用imageIO。有沒有其他的方法可以讓我無法使用ImageIO? – 2014-10-21 05:05:42

0

試試這個代碼,只是把你想要的寬度和高度的大小,它會給你需要的圖像。它迄今爲止發現的最短代碼。對我來說工作得很好。

Bitmap yourBitmap; 
    Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true); 
相關問題