package com.dcsm.business.util;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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;
import javax.imageio.stream.ImageOutputStream;
public class ImageTools {
/**
* 图片截取
*
* @param file
* @param newName
* @param path
* @param x
* @param y
* @param width
* @param height
* @return author:caobo date:Oct 14, 2009 12:38:05 PM
*/
public static File cutting(File file, String newName, String path, int x,
int y, int width, int height) {
ImageOutputStream out = null;
InputStream is = null;
ImageInputStream iis = null;
try {
String endName = file.getName();
endName = endName.substring(endName.lastIndexOf(".") + 1);
Iterator<ImageReader> readers = ImageIO
.getImageReadersByFormatName(endName);
ImageReader reader = (ImageReader) readers.next();
is = new FileInputStream(file);
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
File newFile = new File(path);
if (!newFile.exists())
newFile.mkdirs();
newFile = new File(path, newName);
out = ImageIO
.createImageOutputStream(new FileOutputStream(newFile));
ImageIO.write(bi, endName, out);
file = newFile;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
iis.close();
is.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
public static void main(String args[]) {
File file = new File("F:/logo.png");
cutting(file, "a.png", "f:/", 0, 0, 60, 50);
}
}
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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;
import javax.imageio.stream.ImageOutputStream;
public class ImageTools {
/**
* 图片截取
*
* @param file
* @param newName
* @param path
* @param x
* @param y
* @param width
* @param height
* @return author:caobo date:Oct 14, 2009 12:38:05 PM
*/
public static File cutting(File file, String newName, String path, int x,
int y, int width, int height) {
ImageOutputStream out = null;
InputStream is = null;
ImageInputStream iis = null;
try {
String endName = file.getName();
endName = endName.substring(endName.lastIndexOf(".") + 1);
Iterator<ImageReader> readers = ImageIO
.getImageReadersByFormatName(endName);
ImageReader reader = (ImageReader) readers.next();
is = new FileInputStream(file);
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Rectangle rect = new Rectangle(x, y, width, height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
File newFile = new File(path);
if (!newFile.exists())
newFile.mkdirs();
newFile = new File(path, newName);
out = ImageIO
.createImageOutputStream(new FileOutputStream(newFile));
ImageIO.write(bi, endName, out);
file = newFile;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
iis.close();
is.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
public static void main(String args[]) {
File file = new File("F:/logo.png");
cutting(file, "a.png", "f:/", 0, 0, 60, 50);
}
}