Java Image Cut cut(String srcImageFile, String result, int x, int y, int width, int height)

Here you can find the source of cut(String srcImageFile, String result, int x, int y, int width, int height)

Description

cut

License

Apache License

Declaration

public static final void cut(String srcImageFile, String result, int x, int y, int width, int height) 

Method Source Code

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

import java.awt.Graphics;

import java.awt.Image;
import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;

import javax.imageio.ImageIO;

public class Main {
    public static final void cut(String srcImageFile, String result, int x, int y, int width, int height) {
        try {/* w  ww  .ja  v  a  2 s . c  o  m*/
            BufferedImage bi = ImageIO.read(new File(srcImageFile));
            int srcWidth = bi.getHeight();
            int srcHeight = bi.getWidth();
            if ((srcWidth > 0) && (srcHeight > 0)) {
                Image image = bi.getScaledInstance(srcWidth, srcHeight, 1);

                ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
                Image img = Toolkit.getDefaultToolkit()
                        .createImage(new FilteredImageSource(image.getSource(), cropFilter));
                BufferedImage tag = new BufferedImage(width, height, 1);
                Graphics g = tag.getGraphics();
                g.drawImage(img, 0, 0, width, height, null);
                g.dispose();

                ImageIO.write(tag, "JPEG", new File(result));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. cropImage(Image image, int x, int y, int width, int height)
  2. cropImage(Image img, int x, int y, int w, int h)
  3. cropImage(Image originalImage, int left, int top, int right, int bottom)
  4. cropImage(URL url, float x, float y, float w, float h, OutputStream out)
  5. cut2(String srcImageFile, String descDir, int rows, int cols)
  6. cutImage(BufferedImage image, int posX, int posY, int width, int height)
  7. cutImage(BufferedImage img, int w, int h)
  8. cutImage(File file, int x, int y, int width, int heigth)