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

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

Description

crop

License

Open Source License

Declaration

public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException 

Method Source Code


//package com.java2s;
//  it under the terms of the GNU General Public License as published by

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {
    public static ByteArrayOutputStream crop(ByteArrayInputStream bais, int width, int height) throws IOException {
        BufferedImage src = ImageIO.read(bais);
        BufferedImage clipping = crop(src, width, height);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(clipping, "JPG", baos);
        return baos;
    }//from w  ww .ja  v  a  2 s  .c  o  m

    public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException {
        int x = src.getWidth() / 2 - width / 2;
        int y = src.getHeight() / 2 - height / 2;

        //        System.out.println("---" + src.getWidth() + " - " + src.getHeight() + " - " + x + " - " + y);

        BufferedImage clipping = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//src.getType());  
        Graphics2D area = (Graphics2D) clipping.getGraphics().create();
        area.drawImage(src, 0, 0, clipping.getWidth(), clipping.getHeight(), x, y, x + clipping.getWidth(),
                y + clipping.getHeight(), null);
        area.dispose();

        return clipping;
    }
}

Related

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