Java Image Cut cutImage(File file, int x, int y, int width, int heigth)

Here you can find the source of cutImage(File file, int x, int y, int width, int heigth)

Description

cut Image

License

Open Source License

Declaration

public static void cutImage(File file, int x, int y, int width, int heigth) throws IOException 

Method Source Code

//package com.java2s;

import java.awt.Rectangle;

import java.awt.image.BufferedImage;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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 {
    public static void cutImage(File file, int x, int y, int width, int heigth) throws IOException {
        String fileName = file.getName();
        String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
        Iterator<?> iterator = ImageIO.getImageReadersByFormatName(prefix);
        ImageReader reader = (ImageReader) iterator.next();
        InputStream in = new FileInputStream(file);
        ImageInputStream iis = ImageIO.createImageInputStream(in);
        reader.setInput(iis, true);/*  w w  w .  j  a va 2s. com*/
        ImageReadParam param = reader.getDefaultReadParam();
        Rectangle rect = new Rectangle(x, y, width, heigth);
        param.setSourceRegion(rect);
        BufferedImage bi = reader.read(0, param);
        ImageIO.write(bi, prefix, file);
        iis.close();
        in.close();
    }
}

Related

  1. cropImage(URL url, float x, float y, float w, float h, OutputStream out)
  2. cut(String srcImageFile, String result, int x, int y, int width, int height)
  3. cut2(String srcImageFile, String descDir, int rows, int cols)
  4. cutImage(BufferedImage image, int posX, int posY, int width, int height)
  5. cutImage(BufferedImage img, int w, int h)
  6. cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH)
  7. cutImage(String src, String dest, int x, int y, int w, int h)
  8. cutImage(String src, String dest, int x, int y, int w, int h)