crop BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Crop

Description

crop BufferedImage

Demo Code


//package com.java2s;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage crop(BufferedImage in, int inx, int iny,
            int w, int h) {
        return crop(in, inx, iny, w, h, 0, 0, -1, -1);
    }//from   w ww  . jav a2  s. c  o  m

    public static BufferedImage crop(BufferedImage in, int inx, int iny,
            int inw, int inh, int outx, int outy, int outw, int outh) {
        if (outw < 0) {
            outw = inw;
        }
        if (outh < 0) {
            outh = inh;
        }
        BufferedImage out = new BufferedImage(outw, outh,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = out.createGraphics();

        g.drawImage(in, outx, outy, outx + inw, outy + inh, inx, iny, inx
                + inw, iny + inh, null);
        return out;
    }
}

Related Tutorials