Java Image crop(Image source, int x1, int y1, int x2, int y2)

Here you can find the source of crop(Image source, int x1, int y1, int x2, int y2)

Description

crop

License

LGPL

Declaration

public static Image crop(Image source, int x1, int y1, int x2, int y2) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Main {
    public static Image crop(Image source, int x1, int y1, int x2, int y2) {

        BufferedImage bi = toBufferedImage(source);

        BufferedImage img = bi.getSubimage(x1, y1, x2, y2);

        /*//w w  w .  j  av  a2s . co  m
        JFrame frame = new JFrame();
            
        Image cropped = frame.createImage(new FilteredImageSource(source.getSource(), new CropImageFilter(x1, y1, x2, y2)));
            
        frame.dispose();
        */

        return img;
    }

    public static BufferedImage toBufferedImage(Image img) {

        ImageIcon ii = new ImageIcon(img);
        img = ii.getImage();

        BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();

        return bi;
    }
}

Related

  1. createMemoryImage(int width, int height, Color colour, JComponent someComponent)
  2. createQualityResizedImage( Image orginalImage, int width, int height, boolean keepRatio)
  3. createScaledImage(Image inImage, int inWidth, int inHeight)
  4. createShadowPicture(Image buf)
  5. createTiledImage(Image img, int width, int height)
  6. displayImage(Image image)
  7. displayImage(Image img)
  8. displayImage(Image img, String mesg, int locx, int locy, int sizex, int sizey)
  9. divideImage(Object obj, int rows, int cols)