简体   繁体   English

在不使用ImageIO的情况下在Java中调整图像大小

[英]Resize image in java without using ImageIO

We have successfully resized the images by getting the JPEG thumbnail data, but there are certain images that doesn't have one. 我们已经通过获取JPEG缩略图数据成功调整了图像的大小,但是某些图像没有图像。

The solution is just to resize the image to be able to create a thumbnail, but can only use Java 1.4 and below (hence I can't use ImageIO because the device doesn't support it) 解决方案只是调整图像大小以能够创建缩略图,但只能使用Java 1.4及以下版本(因此,由于设备不支持ImageIO,因此我无法使用ImageIO)

Is there any solution to my problem on how to resize an image using ImageIO? 我如何使用ImageIO调整图像大小是否有解决方案?

java.awt.Image.getScaledInstance(width, height, hints) returns a new, scaled instance of the Image. java.awt.Image.getScaledInstance(width, height, hints)返回Image的新的缩放实例。

The last parameter hints changes the scaling methods used, which will modify the resulting appearence. 最后一个参数hints更改了使用的缩放方法,这将修改结果的外观。 See the snippet below, taken directly from the JDK source. 请参见下面的摘录,直接从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;

Try This code, Just put the size you want for Width and height and it will give you required image. 尝试使用此代码,只需在宽度和高度上输入所需的大小,即可获得所需的图像。 Its shortest code I have found till now. 到目前为止,我发现的最短代码。 works fine for me. 对我来说很好。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM