Java Image Cut cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH)

Here you can find the source of cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH)

Description

cut Image

License

Open Source License

Declaration

public static BufferedImage cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH) 

Method Source Code


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

import java.awt.Graphics;

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH) {
        int sourceWidth = bufferedImage.getWidth();
        int sourceHeight = bufferedImage.getHeight();

        int dw = targetW, dh = targetH;
        if (targetW > sourceWidth) {
            dw = sourceWidth;/*www .j a v a2s.co m*/
        }
        if (targetH > sourceHeight) {
            dh = sourceHeight;
        }
        int x = 0, y = 0, sw = 0, sh = 0;
        x = (sourceWidth / 2) - (dw / 2);
        y = (sourceHeight / 2) - (dh / 2);
        sw = (sourceWidth / 2) + (dw / 2);
        sh = (sourceHeight / 2) + (dh / 2);

        BufferedImage destination = null;
        destination = new BufferedImage(targetW, targetH, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = destination.getGraphics();
        graphics.drawImage(bufferedImage, 0, 0, dw, dh, x, y, sw, sh, null);
        return destination;
    }
}

Related

  1. cut(String srcImageFile, String result, int x, int y, int width, int height)
  2. cut2(String srcImageFile, String descDir, int rows, int cols)
  3. cutImage(BufferedImage image, int posX, int posY, int width, int height)
  4. cutImage(BufferedImage img, int w, int h)
  5. cutImage(File file, int x, int y, int width, int heigth)
  6. cutImage(String src, String dest, int x, int y, int w, int h)
  7. cutImage(String src, String dest, int x, int y, int w, int h)