处理图片的工具类
包括图片的上传、保存、缩放等功能,有需要对图片进行处理时可直接COPY过去用,前段时间写的,懒的写注释,大家凑合看吧。

package net.resume.web.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

public class ImageTools {

public static final String PNG = "png";

public static final String JPEG =
"jpeg";

public static final String JPG =
"jpg";

public static final String GIF =
"gif";

public ImageTools() {
super();
}

public static BufferedImage thumbFilter(BufferedImage source, int width, int height){
BufferedImage thumb = new BufferedImage(width,height,BufferedImage.TYPE_4BYTE_ABGR);

// if(source.getWidth() / (double)width > source.getHeight() / (double)height){
// height = (int)((double)width * source.getHeight()/source.getHeight() / source.getWidth());
// }else{
// width = (int)((double)height * source.getWidth() / source.getHeight());
// }

Graphics2D g = thumb.createGraphics();
g.setColor(new Color(255,255,255,255));
Image buf = source.getScaledInstance(width,height,Image.SCALE_SMOOTH);
g.drawImage(buf,(thumb.getWidth() - width)/2,(thumb.getHeight() - height) / 2, null);

return thumb;
}

public static BufferedImage readImage(InputStream in,String type)throws IOException{
Iterator readers = ImageIO.getImageReadersByFormatName(type);
ImageReader reader = (ImageReader)readers.next();

ImageInputStream iis = ImageIO.createImageInputStream(in);

reader.setInput(iis,true);

BufferedImage img = reader.read(0);

return img;
}

public static BufferedImage readImage(String fileName) throws IOException{
String type = fileName.substring(fileName.lastIndexOf(
".")+1);
return readImage(new FileInputStream(fileName), type);
}

public static BufferedImage readImage(URL url) throws IOException{
String type = url.toString().substring(url.toString().lastIndexOf(
".")+1);
return readImage(url.openStream(), type);
}

public static BufferedImage readImage(File file) throws IOException{
String type = file.getName().substring(file.getName().lastIndexOf(
".")+1);
return readImage(new FileInputStream(file), type);
}


public static void writeImage(BufferedImage image,
String filename) throws IOException {

String type = filename.substring(filename.lastIndexOf(
".")+1);
writeImage(image, type, filename);
}

public static void writeImage(BufferedImage image, String type,
String filename) throws IOException {
File f = new File(filename);
writeImage(image, type, f);
}

public static void writeImage(BufferedImage image, String type, File file)
throws IOException {

Iterator writers = ImageIO.getImageWritersByFormatName(type);
ImageWriter writer = (ImageWriter) writers.next();

//Once an ImageWriter has been obtained, its destination must be set to an ImageOutputStream:

//File f = new File(filename);
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
writer.setOutput(ios);

//Finally, the image may be written to the output stream:

writer.write(image);

}

}
weiking   2006-07-01 17:37:02 评论:0   阅读:579   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

用,就用的漂亮点。文章嘛,借花献佛喽。