有很多Java圖像處理框架可以做到這一點。
以下代碼使用Marvin Framework將圖像的寬度作爲參考將圖像大小調整爲不同的分辨率。計算高度以保持比例:
MarvinImage image = new MarvinImage(uploadedImage);
String fileName = "myfilename.jpg";
MarvinImage scaled = new MarvinImage(1,1)
scale(image, scaled, 1000); // width == 1000
MarvinImageIO.saveImage(scaled, new File(URI.create("file:///server_storage/"+FilenameUtils.removeExtension(fileName)+"_w1000.jpg")).getAbsolutePath());
scale(image, scaled, 500); // width == 500
MarvinImageIO.saveImage(scaled, new File(URI.create("file:///server_storage/"+FilenameUtils.removeExtension(fileName)+"_w500.jpg")).getAbsolutePath());
scale(image, scaled, 250); // width = 250
MarvinImageIO.saveImage(scaled, new File(URI.create("file:///server_storage/"+FilenameUtils.removeExtension(fileName)+"_w250.jpg")).getAbsolutePath());