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

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

Description

Helper methode to create a sub copy of the src input image.

License

Open Source License

Parameter

Parameter Description
src The input image
x Upper left point
y Upper left point
width of sub image
height of sub image

Return

copy of input image

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Christian Hensel//  w  ww . ja v a 2  s. c o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Christian Hensel - initial API and implementation
 *******************************************************************************/

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class Main {
    /**
     * Helper methode to create a sub copy of the src input image.
     * Unfortunately BufferedImage.subImage - Methode returns the same data Array of input image -> changes in subimage would lead
     * to changes in inputImage.
     *
     * @param src
     *           The input image
     * @param x
     *           Upper left point
     * @param y
     *           Upper left point
     * @param width
     *           of sub image
     * @param height
     *           of sub image
     * @return copy of input image
     */
    public static BufferedImage crop(BufferedImage src, int x, int y, int width, int height) {
        BufferedImage returnImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics returnGraphics = returnImage.createGraphics();

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                returnGraphics.setColor(new Color(src.getRGB(x + j, y + i)));
                returnGraphics.drawLine(j, i, j, i);
            }
        }
        return returnImage;
    }
}

Related

  1. crop(BufferedImage input, int x, int y, int width, int height)
  2. crop(BufferedImage original, int newSize)
  3. crop(BufferedImage source, File to, int x1, int y1, int x2, int y2)
  4. crop(BufferedImage source, int startX, int startY, int endX, int endY)
  5. crop(BufferedImage src, int width, int height)
  6. crop(BufferedImage src, Rectangle rect)
  7. crop(final Raster src, final long tx, final long ty, final long minTx, final long maxTy, final int tilesize)
  8. crop4Square(BufferedImage source, File to, int size)
  9. cropAndScaleImage(BufferedImage image, int cropX, int cropY, int cropW, int cropH, int scaleW, int scaleH)