Image 图片处理,缩放、加水印
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
/**
* 名称:ImageUtil.java<br/>
* 描述:图片压缩工具类<br/>
* 最近修改时间:2016年2月24日 下午3:01:44<br/>
*
* @author van
* @since 2016年2月24日
*/
public class ImageUtil {
private static ImageUtil imageUtil;
private BufferedImage image;
private int width;
private int height;
private ImageUtil() {
}
/**
* 方法描述 创建实例
*
* @return
* @modified 2016年2月24日 下午4:27:59 modified by van
*/
public static ImageUtil getInstance() {
if (imageUtil == null) {
imageUtil = new ImageUtil();
}
return imageUtil;
}
/**
* 方法描述 初始化
*
* @param path
* 文件路径或者url路径
* @return
* @modified 2016年2月24日 下午4:25:36 modified by van
*/
public boolean init(String path) {
if (path == null || path.trim().equals("")) {
return false;
}
InputStream inputStream = null;
// path 为文件路径
if (inputStream == null) {
try {
inputStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
// e.printStackTrace();
System.out.println("path is not file path !");
}
}
// path 为URL
if (inputStream == null) {
try {
URL url = new URL(path);
if (url != null) {
inputStream = url.openStream();
}
} catch (MalformedURLException e1) {
// e1.printStackTrace();
System.out.println("path is not url path !");
} catch (IOException e) {
// e.printStackTrace();
System.out.println("path is not url path !");
}
}
if (inputStream == null) {
return false;
}
try {
image = ImageIO.read(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
// image不能为空
if (image == null) {
return false;
}
this.width = this.image.getWidth();
this.height = this.image.getHeight();
return true;
}
/*
* 宽度
*/
public int getWidth() {
return this.width;
}
/*
* 高度
*/
public int getHeight() {
return this.height;
}
/**
* 方法描述 可以正常实现bmp、png、gif转jpg
*
* @param width
* @param height
* @param outPath
* 文件输出路径
* @modified 2016年2月25日 上午9:24:40 modified by van
*/
public void resize(int width, int height) {
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 //TYPE_INT_RGB
// BufferedImage image = new BufferedImage(width,
// height,BufferedImage.SCALE_SMOOTH);
BufferedImage image = new BufferedImage(width, height,
this.image.getType());
//image.getGraphics().drawImage(this.image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, width, height, null); // 绘制缩小后的图
image.getGraphics().drawImage(this.image, 0, 0, width, height, null); // 绘制缩小后的图
image.getGraphics().dispose();
this.image = image;
this.height=height;
this.width=width;
}
public void outputImage(String outPath) {
try {
OutputStream out = getOutputStream(outPath); // 输出到文件流
// 可以正常实现bmp、png、gif转jpg
/*
* JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
* encoder.encode(image); // JPEG编码 out.close();
*/
String ext = outPath.substring(outPath.lastIndexOf(".") + 1,
outPath.length());
ImageIO.write(image, ext.toUpperCase(), out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 方法描述 根据文件路径获取输出流
*
* @param path
* @return
* @throws FileNotFoundException
* @modified 2016年2月24日 下午4:45:51 modified by van
*/
public OutputStream getOutputStream(String path)
throws FileNotFoundException {
if (path == null || "".equals(path.trim())) {
return null;
}
String dir = path.substring(0, path.lastIndexOf("/"));
String file = path.substring(path.lastIndexOf("/") + 1, path.length());
File file2 = new File(dir);
if (!file2.exists()) {
file2.mkdirs();
}
File file3 = new File(file2, file);
return new FileOutputStream(file3);
}
/**
* 方法描述 加文字水印
*
* @param bufferedImage
* @param text
* @param c
* @param font
* @param alpha
* @param startX
* 离图片右边的距离
* @param startY
* 离图片底部的距离
* @modified 2016年2月25日 上午11:20:27 modified by van
*/
public void addWaterText(String text, Color c, Font font, float alpha,
int startX, int startY) {
Graphics2D g = image.createGraphics();
g.setColor(c);
g.setFont(font);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 在指定坐标绘制水印文字
g.drawString(text,width -getLength(text) * font.getSize() - startX, height-startY);
g.dispose();
}
// 添加水印图片
/**
* 方法描述
* @param image
* @param startX
* @param startY
* @param alpha
* @modified 2016年2月25日 下午2:01:54 modified by van
*/
public void addWaterPicture(BufferedImage image,int startX, int startY, float alpha){
Graphics2D g = this.image.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
// 在指定坐标绘制水印文字
g.drawImage(image, width - image.getWidth()-startX, height - image.getHeight() - startY, image.getWidth(), image.getHeight(), null);
g.dispose();
}
/**
* 方法描述
* @param path
* @param startX
* @param startY
* @param alpha
* @modified 2016年2月25日 下午2:07:22 modified by van
*/
public void addWaterPicture(String path,int startX, int startY, float alpha){
File file = new File(path);
if (file==null || !file.exists()) {
return;
}
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(file);
addWaterPicture(bufferedImage, startX, startY, alpha);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 压缩图片
/**
* 方法描述
*
* @param text
* @return
* @modified 2016年2月25日 上午11:06:32 modified by van
*/
public int getLength(String text) {
int length = 0;
for (int i = 0; i < text.length(); i++) {
if (new String(text.charAt(i) + "").getBytes().length > 1) {
length += 2;
} else {
length += 1;
}
}
return length / 2;
}
public static void main(String[] args) {
ImageUtil imageUtil = ImageUtil.getInstance();
imageUtil.init("http://www.pocc.cc/news/2013/08/137783100215.jpg");
imageUtil.resize(640, 640);
// 添加文字水印
//imageUtil.addWaterText("@van", Color.BLACK, new Font("宋体", Font.BOLD, 25), 0.5f, 25, 5);
// 添加水印图片
//dimageUtil.addWaterPicture("D:/images/300x300.jpg", 10, 10, 0.5f);
imageUtil.outputImage("D:/images/300x300.jpg");
System.out.println("Done ...");
}
}