Java BufferedImage Clip clip(BufferedImage image, int[] ul, int[] lr)

Here you can find the source of clip(BufferedImage image, int[] ul, int[] lr)

Description

Clip the image

License

Open Source License

Parameter

Parameter Description
image The image
ul upper left
lr lower right

Return

The clipped image

Declaration

public static BufferedImage clip(BufferedImage image, int[] ul, int[] lr) 

Method Source Code

//package com.java2s;
/*//w  w  w  . j  a  va 2 s  .co  m
 * Copyright 1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library 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 Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Clip  the image
     *
     * @param image The image
     * @param ul upper left
     * @param lr lower right
     *
     * @return The clipped image
     */
    public static BufferedImage clip(BufferedImage image, int[] ul, int[] lr) {
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        int w = lr[0] - ul[0];
        int h = lr[1] - ul[1];

        if ((ul[0] + w <= imageWidth) && (ul[1] + h <= imageHeight) && (w > 0) && (h > 0)) {
            return image.getSubimage(ul[0], ul[1], w, h);
        }

        System.err.println("Specified clip width/height:" + w + "/" + h + " outside of image width/height:"
                + imageWidth + "/" + imageHeight);

        return image;
    }
}

Related

  1. subImage(BufferedImage image, int x, int y, int width, int height)
  2. subimage(BufferedImage src, int x, int y, int w, int h)