示例#1
0
 /**
  * 等比例图片缩放.
  *
  * @param srcFile 源文件
  * @param destFile 目标文件
  * @param destWidth 目标宽度(>0)
  * @param destHeight 目标高度(>0)
  */
 public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) {
   if (type == Type.jdk) {
     try {
       BufferedImage srcBufferedImage = ImageIO.read(srcFile);
       int srcWidth = srcBufferedImage.getWidth();
       int srcHeight = srcBufferedImage.getHeight();
       int width = destWidth;
       int height = destHeight;
       if (srcHeight >= srcWidth) {
         width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth));
       } else {
         height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight));
       }
       BufferedImage destBufferedImage =
           new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
       Graphics2D graphics2D = destBufferedImage.createGraphics();
       graphics2D.setBackground(BACKGROUND_COLOR);
       graphics2D.clearRect(0, 0, destWidth, destHeight);
       graphics2D.drawImage(
           srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
           (destWidth / 2) - (width / 2),
           (destHeight / 2) - (height / 2),
           null);
       graphics2D.dispose();
       ImageIO.write(destBufferedImage, FileUtil.getExtension(destFile), destFile);
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   } else {
     IMOperation operation = new IMOperation();
     operation.thumbnail(destWidth, destHeight);
     operation.gravity("center");
     operation.background(toHexEncoding(BACKGROUND_COLOR));
     operation.extent(destWidth, destHeight);
     operation.quality((double) DEST_QUALITY);
     operation.addImage(srcFile.getPath());
     operation.addImage(destFile.getPath());
     if (type == Type.graphicsMagick) {
       ConvertCmd convertCmd = new ConvertCmd(true);
       if (graphicsMagickPath != null) {
         convertCmd.setSearchPath(graphicsMagickPath);
       }
       try {
         convertCmd.run(operation);
       } catch (IOException | InterruptedException | IM4JavaException e) {
         throw new RuntimeException(e);
       }
     } else {
       ConvertCmd convertCmd = new ConvertCmd(false);
       if (imageMagickPath != null) {
         convertCmd.setSearchPath(imageMagickPath);
       }
       try {
         convertCmd.run(operation);
       } catch (IOException | InterruptedException | IM4JavaException e) {
         throw new RuntimeException(e);
       }
     }
   }
 }
示例#2
0
  public static void zoom(File srcFile, File destFile, int destWidth, int destHeight) {
    Assert.notNull(srcFile);
    Assert.notNull(destFile);
    Assert.state(destWidth > 0);
    Assert.state(destHeight > 0);
    if (type == Type.jdk) {
      Graphics2D graphics2D = null;
      ImageOutputStream imageOutputStream = null;
      ImageWriter imageWriter = null;
      try {
        BufferedImage srcBufferedImage = ImageIO.read(srcFile);
        int srcWidth = srcBufferedImage.getWidth();
        int srcHeight = srcBufferedImage.getHeight();
        int width = destWidth;
        int height = destHeight;
        if (srcHeight >= srcWidth) {
          width = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth));
        } else {
          height = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight));
        }
        BufferedImage destBufferedImage =
            new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
        graphics2D = destBufferedImage.createGraphics();
        graphics2D.setBackground(BACKGROUND_COLOR);
        graphics2D.clearRect(0, 0, destWidth, destHeight);
        graphics2D.drawImage(
            srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH),
            (destWidth / 2) - (width / 2),
            (destHeight / 2) - (height / 2),
            null);

        imageOutputStream = ImageIO.createImageOutputStream(destFile);
        imageWriter =
            ImageIO.getImageWritersByFormatName(FilenameUtils.getExtension(destFile.getName()))
                .next();
        imageWriter.setOutput(imageOutputStream);
        ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
        imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        imageWriteParam.setCompressionQuality((float) (DEST_QUALITY / 100.0));
        imageWriter.write(null, new IIOImage(destBufferedImage, null, null), imageWriteParam);
        imageOutputStream.flush();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (graphics2D != null) {
          graphics2D.dispose();
        }
        if (imageWriter != null) {
          imageWriter.dispose();
        }
        if (imageOutputStream != null) {
          try {
            imageOutputStream.close();
          } catch (IOException e) {
          }
        }
      }
    } else {
      IMOperation operation = new IMOperation();
      operation.thumbnail(destWidth, destHeight);
      operation.gravity("center");
      operation.background(toHexEncoding(BACKGROUND_COLOR));
      operation.extent(destWidth, destHeight);
      operation.quality((double) DEST_QUALITY);
      operation.addImage(srcFile.getPath());
      operation.addImage(destFile.getPath());
      if (type == Type.graphicsMagick) {
        ConvertCmd convertCmd = new ConvertCmd(true);
        if (graphicsMagickPath != null) {
          convertCmd.setSearchPath(graphicsMagickPath);
        }
        try {
          convertCmd.run(operation);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (IM4JavaException e) {
          e.printStackTrace();
        }
      } else {
        ConvertCmd convertCmd = new ConvertCmd(false);
        if (imageMagickPath != null) {
          convertCmd.setSearchPath(imageMagickPath);
        }
        try {
          convertCmd.run(operation);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (IM4JavaException e) {
          e.printStackTrace();
        }
      }
    }
  }