importjava.awt.Color;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.geom.AffineTransform;importjava.awt.image.AffineTransformOp;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.IOException;importjavax.imageio.ImageIO;/*** 裁剪、缩放图片工具类
*@authorCSDN 没有梦想-何必远方*/
public classImgUtils {/*** 缩放图片方法
*@paramsrcImageFile 要缩放的图片路径
*@paramresult 缩放后的图片路径
*@paramheight 目标高度像素
*@paramwidth 目标宽度像素
*@parambb 是否补白*/
public final static void scale(String srcImageFile, String result, int height, int width, booleanbb) {try{double ratio = 0.0; //缩放比例
File f = newFile(srcImageFile);
BufferedImage bi=ImageIO.read(f);
Image itemp= bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);//bi.SCALE_SMOOTH 选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。//计算比例
if ((bi.getHeight() > height) || (bi.getWidth() >width)) {double ratioHeight = (new Integer(height)).doubleValue()/bi.getHeight();double ratioWhidth = (new Integer(width)).doubleValue()/bi.getWidth();if(ratioHeight>ratioWhidth){
ratio=ratioHeight;
}else{
ratio=ratioWhidth;
}
AffineTransformOp op= new AffineTransformOp(AffineTransform//仿射转换
.getScaleInstance(ratio, ratio), null);//返回表示剪切变换的变换
itemp = op.filter(bi, null);//转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
}if (bb) {//补白
BufferedImage image = newBufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);//构造一个类型为预定义图像类型之一的 BufferedImage。
Graphics2D g = image.createGraphics();//创建一个 Graphics2D,可以将它绘制到此 BufferedImage 中。
g.setColor(Color.white);//控制颜色
g.fillRect(0, 0, width, height);//使用 Graphics2D 上下文的设置,填充 Shape 的内部区域。
if (width == itemp.getWidth(null))
g.drawImage(itemp,0, (height - itemp.getHeight(null)) / 2,
itemp.getWidth(null), itemp.getHeight(null),
Color.white,null);elseg.drawImage(itemp, (width- itemp.getWidth(null)) / 2, 0,
itemp.getWidth(null), itemp.getHeight(null),
Color.white,null);
g.dispose();
itemp=image;
}
ImageIO.write((BufferedImage) itemp,"JPEG", new File(result)); //输出压缩图片
} catch(IOException e) {
e.printStackTrace();
}
}/*** 裁剪图片方法
*@parambufferedImage 图像源
*@paramstartX 裁剪开始x坐标
*@paramstartY 裁剪开始y坐标
*@paramendX 裁剪结束x坐标
*@paramendY 裁剪结束y坐标
*@return
*/
public static BufferedImage cropImage(BufferedImage bufferedImage, int startX, int startY, int endX, intendY) {int width =bufferedImage.getWidth();int height =bufferedImage.getHeight();if (startX == -1) {
startX= 0;
}if (startY == -1) {
startY= 0;
}if (endX == -1) {
endX= width - 1;
}if (endY == -1) {
endY= height - 1;
}
BufferedImage result= new BufferedImage(endX - startX, endY - startY, 4);for (int x = startX; x < endX; ++x) {for (int y = startY; y < endY; ++y) {int rgb =bufferedImage.getRGB(x, y);
result.setRGB(x- startX, y -startY, rgb);
}
}returnresult;
}
}