java如何改变图片大小_如何在Java中调整图片大小?

您好,我有一个QR码图像,我想调整它的大小,当我尝试使用此代码将其调整为小图像时,我总是得到模糊的图像,并且扫描时QR码不再有效,但是当我使用相同的代码将大小调整为大尺寸图像时,它可以正常工作:

public BufferedImage getScaledInstance(BufferedImage img,

int targetWidth,

int targetHeight,

Object hint,

boolean higherQuality)

{

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

BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;

BufferedImage ret = (BufferedImage)img;

int w, h;

if (higherQuality) {

// Use multi-step 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 (higherQuality && w > targetWidth) {

w /= 2;

if (w < targetWidth) {

w = targetWidth;

}

}

if (higherQuality && h > targetHeight) {

h /= 2;

if (h < targetHeight) {

h = targetHeight;

}

}

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

Graphics2D g2 = tmp.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);

// g2.setRenderingHint(RenderingHints.KEY_DITHERING, hint);

g2.drawImage(ret, 0, 0, w, h, null);

g2.dispose();

ret = tmp;

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

return ret;

}

有什么问题,我不清楚,请至少给我一个提示,谢谢。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值