package org.jimmy.autotranslate20181022.utils;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Utils {
public static void zoomInImage(String srcPath, String newPath, int maxWidth, int maxHeight) throws Exception {
BufferedImage bufferedImage = null;
File file = new File(srcPath);
bufferedImage = ImageIO.read(file);
if(bufferedImage != null){
bufferedImage = zoomInImage(bufferedImage, maxWidth, maxHeight);
ImageIO.write(bufferedImage, "jpg", new File(newPath));
System.out.println("生成图片完成!");
}
}
private static BufferedImage zoomInImage(BufferedImage originalImage, int maxWidth, int maxHeight) {
BufferedImage newImage = new BufferedImage(maxWidth, maxHeight, originalImage.getType());
Graphics g = newImage.getGraphics();
g.drawImage(originalImage, 0, 0, maxWidth, maxHeight, null);
g.dispose();
return newImage;
}
}
不需要对宽和高进行判断,在传参时直接给定宽高就行了.如width * 2, height * 2,width * 0.6, height * 0.6,如果宽高比例不同,图片失真会比较大,太过放大图片也会明显失真.