Java Image Cut cropImage(Image img, int x, int y, int w, int h)

Here you can find the source of cropImage(Image img, int x, int y, int w, int h)

Description

crop Image

License

Open Source License

Declaration

public static Image cropImage(Image img, int x, int y, int w, int h) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;

import java.awt.image.BufferedImage;

public class Main {
    public static Image cropImage(Image image, Shape shape, int xOffset, int yOffset) {
        BufferedImage out = createImage(shape.getBounds());
        Graphics g = out.getGraphics();

        g.setClip(shape);/*from   w w  w  . ja  v a 2s .  co m*/
        g.drawImage(image, -xOffset, -yOffset, null);
        g.dispose();

        return out;
    }

    public static Image cropImage(Image img, int x, int y, int w, int h) {
        BufferedImage out = createImage(w, h);
        Graphics g = out.getGraphics();
        BufferedImage sub = ((BufferedImage) img).getSubimage(x, y, w, h);

        g.drawImage(sub, 0, 0, w, h, null);
        g.dispose();

        return out;
    }

    public static BufferedImage createImage(Rectangle bounds) {
        return createImage(bounds, BufferedImage.TYPE_INT_ARGB);
    }

    public static BufferedImage createImage(Rectangle bounds, int type) {
        return createImage(bounds.width, bounds.height, type);
    }

    public static BufferedImage createImage(Image image) {
        return createImage(image, BufferedImage.TYPE_INT_ARGB);
    }

    public static BufferedImage createImage(Image image, int type) {
        return createImage(image.getWidth(null), image.getHeight(null), type);
    }

    public static BufferedImage createImage(int width, int height) {
        return createImage(width, height, BufferedImage.TYPE_INT_ARGB);
    }

    public static BufferedImage createImage(int width, int height, int type) {
        return new BufferedImage(width, height, type);
    }
}

Related

  1. cropImage(Image image, int x, int y, int width, int height)
  2. cropImage(Image originalImage, int left, int top, int right, int bottom)
  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)