Java Image Cut cropImage(Image originalImage, int left, int top, int right, int bottom)

Here you can find the source of cropImage(Image originalImage, int left, int top, int right, int bottom)

Description

Crops the edges of the image, as specified.

License

Open Source License

Parameter

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

Return

cropped image

Declaration

public static Image cropImage(Image originalImage, int left, int top, int right, int bottom) 

Method Source Code

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

Related

  1. cropImage(Image image, int x, int y, int width, int height)
  2. cropImage(Image img, int x, int y, int w, int h)
  3. cropImage(URL url, float x, float y, float w, float h, OutputStream out)
  4. cut(String srcImageFile, String result, int x, int y, int width, int height)
  5. cut2(String srcImageFile, String descDir, int rows, int cols)
  6. cutImage(BufferedImage image, int posX, int posY, int width, int height)