/** * @param source: source image file * @param output: output image file * @param mode: 0 ratio,1 maxWidth,2 maxHeight,3 maxSide * @param maxSide: maxWidth, maxHeight or maxSide, different by mode * @param ratio: ratio if resized by ratio */ public static void resizeImage(File source, File output, int mode, Integer maxSide, Double ratio) throws IOException { // read image Image sourceImage = ImageIO.read(source); // resize image BufferedImage outputImage = getResizeImage(sourceImage, mode, maxSide, ratio); // wirite image to file wirteImageToFile(outputImage, output); } /** * @param source: source image * @param mode: 0 ratio,1 maxWidth,2 maxHeight,3 maxSide * @param maxSide: maxWidth, maxHeight or maxSide, different by mode * @param ratio: ratio if resized by ratio */ public static BufferedImage getResizeImage(Image source, int mode, Integer maxSide, Double ratio) { int srcImageWidth = source.getWidth(null); int srcImageHeight = source.getHeight(null); int width = 0, height = 0; switch (mode) { case 0: // resize by ratio width = (int) (srcImageWidth * ratio); height = (int) (srcImageHeight * ratio); break; case 1: // resize by max width width = maxSide; height = new BigDecimal(srcImageHeight).divide(new BigDecimal(srcImageWidth), 5, RoundingMode.HALF_EVEN) .multiply(new BigDecimal(width)).intValue(); break; case 2: // resize by max height height = maxSide; width = new BigDecimal(srcImageWidth).divide(new BigDecimal(srcImageHeight), 5, RoundingMode.HALF_EVEN) .multiply(new BigDecimal(height)).intValue(); break; case 3: // resize by max side(between width and height) if (srcImageWidth > srcImageHeight) { width = maxSide; height = maxSide * srcImageHeight / srcImageWidth; } else { width = maxSide * srcImageWidth / srcImageHeight; height = maxSide; } break; } BufferedImage targetBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = targetBufferedImage.createGraphics(); graphics.drawImage(source, 0, 0, width, height, null); graphics.dispose(); return targetBufferedImage; } public static void wirteImageToFile(BufferedImage source, File output) { FileOutputStream out = null; try { out = new FileOutputStream(output); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(source); out.flush(); // 经测试,此方式性能较差,将其注释 // ImageIO.write(source, "jpg", output); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ImageFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != out) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } 【附一种我们项目中用到的实现】 public static void resizeImageByRatio(File source, String outputFolderPath, double ratio) throws IOException { FileUtils.forceMkdir(new File(outputFolderPath)); if (!outputFolderPath.endsWith(File.separator)) { outputFolderPath += File.separator; } String fileName = outputFolderPath + StringUtils.substringBefore(source.getName(), "."); fileName += "_ratio_" + ratio + ".jpg"; resizeImage(source, new File(fileName), 0, 0, ratio); } public static void resizeImageByMaxWidth(File source, String outputFolderPath, int maxSide) throws IOException { FileUtils.forceMkdir(new File(outputFolderPath)); if (!outputFolderPath.endsWith(File.separator)) { outputFolderPath += File.separator; } String fileName = outputFolderPath + StringUtils.substringBefore(source.getName(), "."); fileName += "_width_" + maxSide + ".jpg"; resizeImage(source, new File(fileName), 1, maxSide, null); } public static void resizeImageByMaxHeight(File source, String outputFolderPath, int maxSide) throws IOException { FileUtils.forceMkdir(new File(outputFolderPath)); if (!outputFolderPath.endsWith(File.separator)) { outputFolderPath += File.separator; } String fileName = outputFolderPath + StringUtils.substringBefore(source.getName(), "."); fileName += "_height_" + maxSide + ".jpg"; resizeImage(source, new File(fileName), 2, maxSide, null); } public static void resizeImageByMaxSide(File source, String outputFolderPath, int maxSide) throws IOException { FileUtils.forceMkdir(new File(outputFolderPath)); if (!outputFolderPath.endsWith(File.separator)) { outputFolderPath += File.separator; } String fileName = outputFolderPath + StringUtils.substringBefore(source.getName(), "."); fileName += "_maxside_" + maxSide + ".jpg"; resizeImage(source, new File(fileName), 3, maxSide, null); }