Java examples for 2D Graphics:BufferedImage Crop
crop BufferedImage To Height
import java.awt.AlphaComposite; import java.awt.BasicStroke; import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Paint; import java.awt.Polygon; import java.awt.RadialGradientPaint; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.geom.Area; import java.awt.image.BufferedImage; import java.awt.image.ConvolveOp; import java.awt.image.Kernel; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import javax.imageio.ImageIO; import javax.swing.JFrame; import org.apache.log4j.Logger; public class Main{ public static BufferedImage cropToHeight(BufferedImage in, int maxHeight) { if (in.getHeight() <= maxHeight) return in; BufferedImage result = new BufferedImage(in.getWidth(), maxHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D gr = result.createGraphics(); int cropTotal = in.getHeight() - maxHeight; int sourceY1 = cropTotal >> 1; int sourceY2 = in.getHeight() - cropTotal + sourceY1; gr.drawImage(in, 0, 0, result.getWidth(), result.getHeight(), 0 /*sx1*/, sourceY1 /*sy1*/, in.getWidth() /* sx2 */, sourceY2 /* sy2 */, null); return result; }/*from w ww. j a v a 2 s.co m*/ }