java scaleabsolute_Java:图片缩放算法(down scaling)

Progressive Bilinear Scaling

We know that a significant problem with the quality of the bilinear approach occurs when the downscale is by more than 50 percent. So what if we compen- sated for that problem by scaling iteratively toward the final size, scaling down by exactly 50 percent each time until the final iteration, when we scale by 50 percent or less? Then we would account for all of the pixels along the way that should figure into the final image.

// 缩小图片,缩小时可以使用改进过的bilinear, bicubic插值算法

// 但是转换透明图片时如果使用单缓冲区会出问题,这时每次都要创建一个缓冲区才可以

public static BufferedImage getFasterDownScaledInstance(BufferedImage img,

int targetWidth,

int targetHeight,

Object hint,

boolean progressive) {

int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB

: BufferedImage.TYPE_INT_ARGB;

BufferedImage ret = (BufferedImage) img;

BufferedImage scratchImage = null;

Graphics2D g2d = null;

int w = 0, h = 0;

int prevW = ret.getWidth();

int prevH = ret.getHeight();

if (progressive) {

// Use multistep technique: start with original size,

// then scale down in multiple passes with drawImage()

// until the target size is reached

w = img.getWidth();

h = img.getHeight();

} else {

// Use one-step technique: scale directly from original

// size to target size with a single drawImage() call

w = targetWidth;

h = targetHeight;

}

do {

if (targetWidth < img.getWidth() && progressive && w > targetWidth) {

// 如果是缩小,宽缩小为原来的一半

w >>>= 1;

w = (w < targetWidth) ? targetWidth : w;

} else {

w = targetWidth;

}

if (targetHeight < img.getHeight() && progressive && h > targetHeight) {

// 如果是缩小,高缩小为原来的一半

h >>>= 1;

h = (h < targetHeight) ? targetHeight : h;

} else {

h = targetHeight;

}

if (scratchImage == null) {

// Use a single scratch buffer for all iterations

// and then copy to the final, correctly sized image before

// returning

scratchImage = new BufferedImage(w, h, type);

g2d = scratchImage.createGraphics();

} else if (type == BufferedImage.TYPE_INT_ARGB && scratchImage != null && g2d != null) {

// 透明图片不能使用单缓存

g2d.dispose();

scratchImage = new BufferedImage(w, h, type);

g2d = scratchImage.createGraphics();

}

g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);

g2d.drawImage(ret, 0, 0, w, h, 0, 0, prevW, prevH, null);

prevW = w;

prevH = h;

ret = scratchImage;

} while (w != targetWidth || h != targetHeight);

if (g2d != null) {

g2d.dispose();

}

// If we used a target size, the results into it

if (targetWidth != ret.getWidth() || targetHeight != ret.getHeight()) {

scratchImage = new BufferedImage(targetWidth, targetHeight, type);

g2d = scratchImage.createGraphics();

g2d.drawImage(ret, 0, 0, null);

g2d.dispose();

ret = scratchImage;

}

return ret;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值