| | |
| | | import java.io.InputStream;
|
| | |
|
| | | import net.coobird.thumbnailator.Thumbnails;
|
| | | import net.coobird.thumbnailator.Thumbnails.Builder;
|
| | | import net.coobird.thumbnailator.geometry.Positions;
|
| | |
|
| | | /**
|
| | |
| | | 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();
|