不使用ImageIO在java中调整图像大小

时间:2014-10-20 08:32:41

标签: java image resize javax.imageio

我们已经通过获取JPEG缩略图数据成功调整了图像的大小,但是某些图像没有。[/ p>

解决方案只是调整图像大小以便能够创建缩略图,但只能使用Java 1.4及更低版本(因此我无法使用ImageIO,因为设备不支持它)

关于如何使用ImageIO调整图像大小,我的问题是否有解决方法?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

尝试此代码,只需将您想要的尺寸放入宽度和高度,它就会为您提供所需的图像。它迄今为止发现的最短代码。对我来说很好。

    Bitmap yourBitmap;
    Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);