java 长方形正方形_JAVA将长方形图片剪裁成正方形。

JAVA如何将长方形图片剪裁成正方形呢?其实很简单,用到了BufferedImage的getSubimage()方法。不多说,直接上代码:

/**

* 剪裁成正方形

*/

public static BufferedImage getSque(BufferedImage bi) {

int init_width = bi.getWidth();

int init_height = bi.getHeight();

if (init_width != init_height){

int width_height = 0;

int x = 0;

int y = 0;

if (init_width > init_height) {

width_height = init_height;//原图是宽大于高的长方形

x = (init_width-init_height)/2;

y = 0;

} else if (init_width < init_height) {

width_height = init_width;//原图是高大于宽的长方形

y = (init_height-init_width)/2;

x = 0;

}

bi = bi.getSubimage(x, y, width_height, width_height);

}

return bi;

}

非常简单的就将长方形剪裁成正方形了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值