admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
utils/src/main/java/org/yeshi/utils/ImageCropUtil.java
@@ -6,6 +6,7 @@
import java.io.InputStream;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;
/**
@@ -28,6 +29,33 @@
         throws FileNotFoundException, Exception {
      if (sourceImage==null)
         throw new FileNotFoundException();
      BufferedImage image = Thumbnails.of(sourceImage).scale(1.0f).asBufferedImage();
      int width = image.getWidth();
      int height = image.getHeight();
      int newWidth = 0;
      int newHeight = 0;
      if (width > ratio * height) {// 以高为主
         newHeight = height;
         newWidth = (int) (newHeight * ratio);
      } else {// 以宽为主
         newWidth = width;
         newHeight = (int) (newWidth / ratio);
      }
      if (destImage.exists())
         destImage.delete();
      Thumbnails.of(sourceImage).sourceRegion(Positions.CENTER, newWidth, newHeight).scale(1.0)
            .toFile(destImage.getPath());
   }
   public static void centerCrop(File sourceImage, File destImage, float ratio)
         throws FileNotFoundException, Exception {
      if (sourceImage==null)
         throw new FileNotFoundException();
      BufferedImage image = Thumbnails.of(sourceImage).scale(1.0f).asBufferedImage();
      int width = image.getWidth();
      int height = image.getHeight();