Java BufferedImage Crop crop(BufferedImage input, int x, int y, int width, int height)

Here you can find the source of crop(BufferedImage input, int x, int y, int width, int height)

Description

crop

License

Open Source License

Declaration

public static BufferedImage crop(BufferedImage input, int x, int y, int width, int height) 

Method Source Code

//package com.java2s;
/*/*from   ww w .j ava2  s.  c  o  m*/
 * Copyright 2015 Laszlo Balazs-Csiki
 *
 * This file is part of Pixelitor. Pixelitor is free software: you
 * can redistribute it and/or modify it under the terms of the GNU
 * General Public License, version 3 as published by the Free
 * Software Foundation.
 *
 * Pixelitor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Pixelitor. If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage crop(BufferedImage input, int x, int y, int width, int height) {
        assert input != null;

        if (width <= 0) {
            throw new IllegalArgumentException("width = " + width);
        }
        if (height <= 0) {
            throw new IllegalArgumentException("height = " + height);
        }
        BufferedImage output = new BufferedImage(width, height, input.getType());
        Graphics2D g = output.createGraphics();
        AffineTransform t = AffineTransform.getTranslateInstance(-x, -y);
        g.transform(t);
        g.drawImage(input, null, 0, 0);
        g.dispose();

        return output;
    }
}

Related

  1. adjustRectToFitImage(BufferedImage image, Rectangle cropRect)
  2. autoCrop(BufferedImage source)
  3. autoCrop(BufferedImage source, double tolerance)
  4. crop(BufferedImage image, Rectangle clip)
  5. crop(BufferedImage original, int newSize)
  6. crop(BufferedImage source, File to, int x1, int y1, int x2, int y2)
  7. crop(BufferedImage source, int startX, int startY, int endX, int endY)
  8. crop(BufferedImage src, int width, int height)