admin
2021-01-27 92f1d85ddc449ce7452f9d52f8b081e6b69c720b
utils/src/main/java/org/yeshi/utils/ImageCropUtil.java
@@ -1,79 +1,79 @@
package org.yeshi.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;
/**
 * 图片裁剪帮助
 *
 * @author Administrator
 *
 */
public class ImageCropUtil {
   /**
    * 剪切图片中间部分(内切)
    * @param sourceImage
    * @param destImage
    * @param ratio
    * @throws FileNotFoundException
    * @throws Exception
    */
   public static void centerCrop(InputStream 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();
      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();
      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());
   }
}
package org.yeshi.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Positions;
/**
 * 图片裁剪帮助
 *
 * @author Administrator
 *
 */
public class ImageCropUtil {
   /**
    * 剪切图片中间部分(内切)
    * @param sourceImage
    * @param destImage
    * @param ratio
    * @throws FileNotFoundException
    * @throws Exception
    */
   public static void centerCrop(InputStream 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();
      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();
      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());
   }
}