Here you can find the source of cropImage(Image originalImage, int left, int top, int right, int bottom)
Parameter | Description |
---|---|
originalImage | a parameter |
left | number of pixels to trim from the the left edge |
top | pixels to trim from the top edge |
right | pixels to trim from the right edge |
bottom | pixels to trim from the bottom edge |
public static Image cropImage(Image originalImage, int left, int top, int right, int bottom)
//package com.java2s; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; public class Main { /** Rendering hints - render at highest quality*/ private static final RenderingHints RENDERING_HINTS_HIGH_QUALITY = new RenderingHints(null); /**/* w ww .j a v a 2 s. c o m*/ * Crops the edges of the image, as specified. If the crop dimensions * are negative, the image is grown. * * @param originalImage * @param left number of pixels to trim from the the left edge * @param top pixels to trim from the top edge * @param right pixels to trim from the right edge * @param bottom pixels to trim from the bottom edge * @return cropped image */ public static Image cropImage(Image originalImage, int left, int top, int right, int bottom) { BufferedImage newImage = new BufferedImage(originalImage.getWidth(null) - left - right, originalImage.getHeight(null) - top - bottom, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2d = newImage.createGraphics(); g2d.setRenderingHints(RENDERING_HINTS_HIGH_QUALITY); g2d.drawImage(originalImage, -1 * left, -1 * top, null); g2d.dispose(); return newImage; } }