Java BufferedImage Crop cropToBeSameSize(BufferedImage image1, BufferedImage image2)

Here you can find the source of cropToBeSameSize(BufferedImage image1, BufferedImage image2)

Description

Resize images to be same size.

License

Open Source License

Parameter

Parameter Description
image1 an image.
image2 an image.

Return

a list containing two images with the same dimensions

Declaration

public static List<BufferedImage> cropToBeSameSize(BufferedImage image1, BufferedImage image2) 

Method Source Code

//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);
    }
}

Related

  1. cropImage(final BufferedImage img, int x, int y, int w, int h, double xScale, double yScale)
  2. cropImageRelative(BufferedImage image, double leftCropFactor, double rightCropFactor, double topCropFactor, double bottomCropFactor)
  3. croppedCopy(BufferedImage image, int startX, int startY, int endX, int endY)
  4. cropScale(BufferedImage source, int width, int height)
  5. cropToAspectRatio(BufferedImage image, float aspect)
  6. divideImage(BufferedImage bi, int rows, int cols, int width, int height)
  7. doCrop(BufferedImage bufferedImage, int cropX, int cropY, int width, int height)
  8. drawCropped(JPanel contentPane, ActionListener listener, BufferedImage img, int type, int sx1, int sy1, int sx2, int sy2, int x, int y, int scale)
  9. ImageCrop(String sourcePath, String descPath, int cropX, int cropY, int width, int height)