thumbnails java_JAVA 图片处理(基于Thumbnails)

参考:

获取图片的宽度和高度尺寸

/**

* 获取图片的宽度和高度尺寸

* @param img

* @return

*/

public static int[] getSize(File img){

BufferedImage image = null;

try {

image = ImageIO.read(img);

} catch (IOException e) {

e.printStackTrace();

}

int width = image.getWidth();

int height = image.getHeight();

int[] size = {width,height};

return size;

}

自定义参数缩放图片

/**

* 功能:图片缩放

* @param source 原图像

* @param target 目标图像

* @param width 缩放后的图片宽度

* @param height 缩放后的图片高度

* @param scaleType 缩放类型,是按高度,还是按宽度,是以缩放最小为准还是以缩放最大为准

* @param forceAble 当图片大小小于规定的尺寸,是否强制将图片放大失真

* @param quality

*/

public static void scale(File source,File target,int width,int height,int scaleType,boolean forceAble,float quality){

//按比例缩放

int size[] = getSize(source);

int w = size[0];

int h = size[1];

double wScale = getScale(width, w);

double hScale = getScale(height,h);

double scale = 1.0;

//System.out.println(width+"--"+height+"--"+w+"--"+h);

/*

* width height

* ----- = --------

* w h

*/

switch (scaleType) {

case SCALETYPE_WIDTH:

scale = wScale;

break;

case SCALETYPE_HEIGHT:

scale = hScale;

break;

case SCALETYPE_MIN:

scale = Math.min(wScale,hScale);

break;

case SCALETYPE_MAX:

scale = Math.max(wScale, hScale);

break;

default:

scale = Math.min(wScale, hScale);

break;

}

try{

//如果按照大于1的比例进行缩放(其实是放大),这里会根据参数决定forceAble决定

if(scale<=1 || scale > 1 && forceAble){

//缩放原图

Thumbnails.of(source).width(width).outputQuality(quality).toFile(target);

}

else{

//复制原图

Thumbnails.of(source).scale(1).outputQuality(quality).toFile(target);

}

}

catch(IOException e){

e.printStackTrace();

}

}

调用案例:

//将图片按照300的宽度比进行缩放,设置高度不会影响结果

ImageUtil.scale(file, target, 300,400,ImageUtil.SCALETYPE_WIDTH,false,1.0f);

//将图片按照400的宽度比进行缩放,设置宽度不会影响结果

ImageUtil.scale(file, target, 300,400,ImageUtil.SCALETYPE_HEIGHT,false,1.0f);

自定义位置剪裁图片 常用来头像剪裁

/**

* 功能:图片剪裁

* 按规定的宽度和高度进行剪裁,可用来头像剪裁

* @param source 原图像

* @param target 目标图像

* @param width 剪裁后的宽度

* @param height 剪裁后的高度

* @param posX 位置x

* @param posY 位置y

* @param scaleAble 是否先缩放后剪裁

* @param quality 剪裁后的图片质量

*/

public static void clip(File source,File target,int width,int height,int posX,int posY,boolean scaleAble,float quality){

int size[] = getSize(source);

int w = size[0];

int h = size[1];

double wScale = getScale(width, w);

double hScale = getScale(height,h);

double maxScale = Math.max(wScale, hScale);

//scale为1.0表示原图剪裁,否则就缩放(扩大)

double scale = scaleAble ? maxScale : 1.0;

try {

QzFileUtil.createFile(target);

//先按比例缩放存到内存

BufferedImage image = Thumbnails.of(source).scale(scale).asBufferedImage();

//后从中心剪裁

Thumbnails.of(image).size(width,height).sourceRegion(posX,posY, width, height).outputQuality(quality).toFile(target);

} catch (IOException e) {

e.printStackTrace();

}

}

调用案例

//直接在原图上剪裁(100,100,200,300)

ImageUtil.clip(file, target, 200, 300, 100, 100, 0, 1.0f);

//先将原图缩放到400宽,后进行剪裁(100,100,200,300)

ImageUtil.clip(file, target, 200, 300, 100, 100, 400, 1.0f);

从中心位置剪裁图像

/**

* 将原图按规定的宽度和高度剪裁

* @param source

* @param target

* @param width

* @param height

* @param scaleAble 是否先缩放后剪裁

*/

public static void clipBySize(File source,File target,int width,int height,boolean scaleAble, float quality){

int size[] = getSize(source);

int w = size[0];

int h = size[1];

double wScale = getScale(width, w);

double hScale = getScale(height, h);

double maxScale = Math.max(wScale, hScale);

double scale = scaleAble ? maxScale : 1.0;

if(!target.getParentFile().exists()){

target.getParentFile().mkdirs();

}

try {

//先按比例缩放存到内存

BufferedImage image = Thumbnails.of(source).scale(scale).asBufferedImage();

//后从中心剪裁

Thumbnails.of(image).size(width, height).sourceRegion(Positions.CENTER, width, height).outputQuality(quality).toFile(target);

} catch (IOException e) {

e.printStackTrace();

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值