Here you can find the source of cropImage(BufferedImage image, int cropWidth, int cropHeight)
Parameter | Description |
---|---|
image | Input image |
cropWidth | Width required for cropped image |
cropHeight | Height required for cropped image |
public static BufferedImage cropImage(BufferedImage image, int cropWidth, int cropHeight)
//package com.java2s; //License from project: Open Source License import java.awt.image.BufferedImage; public class Main { /**//from w ww.j a v a 2 s . c o m * Method crop an image to the given dimensions. If dimensions are more than the input image size, then the image * gets padded with black color * * @param image Input image * @param cropWidth Width required for cropped image * @param cropHeight Height required for cropped image * @return Cropped image */ public static BufferedImage cropImage(BufferedImage image, int cropWidth, int cropHeight) { BufferedImage retImg = null; int width = 0; int height = 0; width = image.getWidth(); height = image.getHeight(); retImg = new BufferedImage(cropWidth, cropHeight, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < cropWidth; i++) { for (int j = 0; j < cropHeight; j++) { if (i < width && j < height) { retImg.setRGB(i, j, image.getRGB(i, j)); } else { retImg.setRGB(i, j, 0); } } } return retImg; } }