Here you can find the source of cropToBeSameSize(BufferedImage image1, BufferedImage image2)
Parameter | Description |
---|---|
image1 | an image. |
image2 | an image. |
public static List<BufferedImage> cropToBeSameSize(BufferedImage image1, BufferedImage image2)
//package com.java2s; //License from project: Open Source License import java.awt.image.BufferedImage; import java.util.Arrays; import java.util.List; public class Main { /**//from w w w. j a v a 2s . co m * Resize images to be same size. The size is determined by the minimum * height and minimum width of the images. * * @param image1 * an image. * @param image2 * an image. * @return a list containing two images with the same dimensions */ public static List<BufferedImage> cropToBeSameSize(BufferedImage image1, BufferedImage image2) { if (imagesSameSize(image1, image2)) { return Arrays.asList(image1, image2); } int minHeight = Math.min(image1.getHeight(), image2.getHeight()); int minWidth = Math.min(image1.getWidth(), image2.getWidth()); BufferedImage cropped1 = cropImage(image1, minWidth, minHeight); BufferedImage cropped2 = cropImage(image2, minWidth, minHeight); return Arrays.asList(cropped1, cropped2); } /** * Check canvas sizes and resize images to same size * * @return true/false */ public static boolean imagesSameSize(BufferedImage image1, BufferedImage image2) { return (image1.getWidth() == image2.getWidth() && image1.getHeight() == image2.getHeight()); } /** * Crops the image to the given size starting at (0,0) * * @param image * The image to crop * @param width * width in pixels * @param height * height in pixels */ private static BufferedImage cropImage(BufferedImage image, int width, int height) { if (image.getWidth() == width && image.getHeight() == height) { return image; } return image.getSubimage(0, 0, width, height); } }