imgscalr是一个用来进行图片裁剪的工具类,官网参见:http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/。
Maven的dependcy加上:
<dependency> <groupId>org.imgscalr</groupId> <artifactId>imgscalr-lib</artifactId> <version>${imgscalr.version}</version> </dependency>
<imgscalr.version>4.2</imgscalr.version>
这里仅用一小段代码说明imgscalr的功能,具体参数含义可参考主页帮助,那里的用法比我这里详细的多:
public class ImageUtil { public static void convertImage(InputStream ins, String newFilename) { BufferedImage originalImage; try { originalImage = ImageIO.read(ins); BufferedImage thumbnail = Scalr.resize(originalImage, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH, 400, 300, Scalr.OP_ANTIALIAS); if (!ImageIO.write(thumbnail, "JPEG", new File(newFilename))) System.out.println("File write failed.");; } catch (IOException e) { e.printStackTrace(); } } }