Java Image Cut cutImage(String src, String dest, int x, int y, int w, int h)

Here you can find the source of cutImage(String src, String dest, int x, int y, int w, int h)

Description

cut Image

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
public static boolean cutImage(String src, String dest, int x, int y, int w, int h) 

Method Source Code

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

import java.awt.Rectangle;

import java.awt.image.BufferedImage;

import java.io.File;
import java.io.FileInputStream;

import java.io.InputStream;

import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class Main {

    @SuppressWarnings("unchecked")
    public static boolean cutImage(String src, String dest, int x, int y, int w, int h) {
        try {/*from w  w w .j  a v a  2  s .c o m*/
            Iterator iterator = ImageIO.getImageReadersByFormatName("jpg");
            ImageReader reader = (ImageReader) iterator.next();
            InputStream in = new FileInputStream(src);
            ImageInputStream iis = ImageIO.createImageInputStream(in);
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            x = (x > 0 ? x : 0);
            y = (y > 0 ? y : 0);
            Rectangle rect = new Rectangle(x, y, w, h);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0, param);
            ImageIO.write(bi, "jpg", new File(dest));
            return true;
        } catch (Exception e) {
            // TODO: handle exception  
            e.printStackTrace();
            return false;
        }

    }
}

Related

  1. cutImage(BufferedImage image, int posX, int posY, int width, int height)
  2. cutImage(BufferedImage img, int w, int h)
  3. cutImage(File file, int x, int y, int width, int heigth)
  4. cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH)
  5. cutImage(String src, String dest, int x, int y, int w, int h)