2014-10-20 75 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); 
相关问题