繁体   English   中英

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

[英]Resize image in java without using ImageIO

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

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

我如何使用ImageIO调整图像大小是否有解决方案?

java.awt.Image.getScaledInstance(width, height, hints)返回Image的新的缩放实例。

最后一个参数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;

尝试使用此代码,只需在宽度和高度上输入所需的大小,即可获得所需的图像。 到目前为止,我发现的最短代码。 对我来说很好。

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

暂无
暂无

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

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