public class ImageUtil {
/**
* 调整图片大小
* @param is
* @param target
* @param minWidth
* @param minHeight
* @throws Exception
*/
public static void reduceImage(InputStream is,File target,int minWidth,int minHeight) throws Exception{
Image image = ImageIO.read(is);
int width = image.getWidth(null);
int height = image.getHeight(null);
float r1 = (float)minWidth/width;
float r2 = (float)minHeight/height;
int targetWidth = minWidth;
int targetHeight = minHeight;
if(r1>r2){
targetWidth = minWidth;
targetHeight = (int) (height * r1);
}else{
targetHeight = minHeight;
targetWidth = (int) (width * r2);
}
Thumbnails.of(is).size(targetWidth, targetHeight).outputFormat("jpg").toFile(target);
}
/**
* 给图片加水印
* @Title: addWaterImage
* @author: jack_qiu
* @Description: TODO
* @param targetImg 要加水印的图片文件名
* @param waterImg 水印图片文件名
* @param x 水印位置x坐标
* @param y 水印位置y坐标
* @param alpha 透明度(0-1,要加透明水印,水印文件必须是png或gif格式图片)
* @throws Exception
*/
public static void addWaterImage(String targetImg,String waterImg,int x,int y,float alpha) throws Exception{
try {
File file = new File(targetImg);
Image image = ImageIO.read(file);
int width = image.getWidth(null);
int height = image.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, null);
Image waterImage = ImageIO.read(new File(waterImg));//水印文件
int width_wi=waterImage.getWidth(null);
int height_wi=waterImage.getHeight(null);
if(width<=width_wi || height<=height_wi){
throw new Exception("原图的宽、高必须大于水印图的宽、高");
}
int widthDiff = width-width_wi;
int heightDiff = height-height_wi;
if(x<0){
x = widthDiff/2;
}else if(x>widthDiff){
x = widthDiff;
}
if(y<0){
y = heightDiff/2;
}else if(y>heightDiff){
y = heightDiff;
}
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
g.drawImage(waterImage, x, y, width_wi,height_wi,null);//水印文件结束
g.dispose();
ImageIO.write(bufferedImage, "JPEG", file);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 调整图片大小
* @param src
* @param target
* @param minWidth
* @param minHeight
* @throws Exception
*/
public static void reduceImage(File src,File target,int minWidth,int minHeight) throws Exception{
Image image = ImageIO.read(src);
int width = image.getWidth(null);
int height = image.getHeight(null);
float r1 = (float)minWidth/width;
float r2 = (float)minHeight/height;
int targetWidth = minWidth;
int targetHeight = minHeight;
if(r1>r2){
targetWidth = minWidth;
targetHeight = (int) (height * r1);
}else{
targetHeight = minHeight;
targetWidth = (int) (width * r2);
}
Thumbnails.of(src).size(targetWidth, targetHeight).outputFormat("jpg").toFile(target);
}
public static void reduceImage(File src,File target,int maxWidth,int maxHeight,String format){
InputStream is = null;
try {
is = new FileInputStream(src);
reduceImage(is, target, maxWidth, maxHeight, format);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(null!=is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void reduceImage(File src,File target,long maxPixel,String format) throws Exception{
Image image = ImageIO.read(src);
int width = image.getWidth(null);
int height = image.getHeight(null);
int targetWidth = width;
int targetHeight = height;
float r = 1;
long pixelNum = width * height;
if(pixelNum>maxPixel){
r = (float) Math.sqrt((double)pixelNum/maxPixel);
}
targetWidth = (int)(width / r);
targetHeight = (int)(height / r);
Thumbnails.of(cn.hutool.core.util.ImageUtil.toBufferedImage(image)).size(targetWidth, targetHeight)
//.outputQuality(1.0)
.outputFormat(format).toFile(target);
}
public static void reduceImage(InputStream is,File target,int maxWidth,int maxHeight,String format) throws Exception{
Image image = ImageIO.read(is);
int width = image.getWidth(null);
int height = image.getHeight(null);
int targetWidth = width;
int targetHeight = height;
float r = 1;
if(width>maxWidth && width>height){
r = (float)maxWidth/width;
}else if(height>maxHeight){
r = (float)maxHeight/height;
}
targetWidth = (int)(width * r);
targetHeight = (int)(height * r);
Thumbnails.of(cn.hutool.core.util.ImageUtil.toBufferedImage(image)).size(targetWidth, targetHeight)
.outputQuality(1.0)
.outputFormat(format).toFile(target);
}
public static void main(String[] args) {
File src = new File("d:/1.png");
File target = new File("d:/2.png");
reduceImage(src, target, 800, 600, "png");
}
}