import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
* @ClassName:WaterMarkServiceImpl.java
* @Description:图片水印<br>
* 图片水印实现思路:<br>
* 1.创建缓存图片对象--BufferedImage<br>
* 2.创建java绘图工具对象--Graphics2D<br>
* 3.使用绘图工具对象将原图绘制到缓存图片对象<br>
* 4.使用绘图工具将水印(文字或图片)绘制到缓存图片对象<br>
* 5.创建图片编码工具类--JPEGImageEncoder(由于java7以后jdk默认读取不到JPEGImageEncoder的rt.jar使用ImageIO实现编码输出)<br>
* 6.使用图片编码工具类数据缓存图像到目标图片文件<br>
* @author yunfei.qi
* @date 2016年12月15日 上午11:30:12
*/
public class WaterMarkServiceImpl implements WaterMarkService{
//水印文字
public static final String MARK_TEXT="测试水印";
//字体
public static final String FONT_NAME="华文行楷";
//字体样式
public static final int FONT_STYLE=Font.BOLD;
//字体大小 单位像素
public static final int FONT_SIZE=120;
//文字颜色
public static final Color FONT_COLOR=Color.BLUE;
//文字水印透明度 30%
public static float ALPHA=0.3F;
//绘制位置横坐标
public static final int X=120;
//绘制位置纵坐标
public static final int Y=120;
public static final String BASE_PATH="C:\\tempDir\\";
public static final String MARK_LOGO_IMAGE="C:\\tempDir\\logo.png";
public static final String FORMAT_NAME="jpg";
/**
* 文字水印
* @param image
* @param imageFileName
* @return
*/
public String waterMarkBySingleText(File image,String imageFileName){
String logoFileName="2_logo_single_text_"+imageFileName;
OutputStream outs=null;
try {
//通过ImageIO获取图像文件的像素大小 即宽/高
Image imageTemp=ImageIO.read(image);
int width=imageTemp.getWidth(null);
int height=imageTemp.getHeight(null);
//1.创建缓存图片对象--BufferedImage
/**
* new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
* width width of the created image
* height height of the created image
* imageType type of the created image
*/
BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//2.创建java绘图工具对象--Graphics2D
Graphics2D graphics2D=bufferedImage.createGraphics();
//3.使用绘图工具对象将原图绘制到缓存图片对象
/**
* img the specified image to be drawn. This method does nothing if img is null.
* x the x coordinate.
* y the y coordinate.
* width the width of the rectangle.
* height the height of the rectangle.
* observer object to be notified as more of the image is converted.
*/
graphics2D.drawImage(imageTemp, 0, 0, width,height,null);
//设置水印文字 字体 样式 大小
graphics2D.setFont(new Font(FONT_NAME, FONT_STYLE, FONT_SIZE));
///设置水印文字颜色
graphics2D.setColor(FONT_COLOR);
/**
* 获取水印文本的高和宽度
* 高度:即文字的像素大小 -->FONT_SIZE
* 宽度:中文字符 1:1 即中文字符-->FONT_SIZE 英文字符1:2-->FONT_SIZE/2
*/
int waterMarkWidth=FONT_SIZE*getTextLength(MARK_TEXT);
int waterMarkHeight=FONT_SIZE;
//计算图片尺寸和水印尺寸差异
int widthDiff=width-waterMarkWidth;
int heightDiff=height-waterMarkHeight;
int x=X;
int y=Y;
if(x>widthDiff){
x=widthDiff;
}
if(y>heightDiff){
y=heightDiff;
}
//设置水印透明度 0.3
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
//4.使用绘图工具将水印(文字或图片)绘制到缓存图片对象
graphics2D.drawString(MARK_TEXT, x, y+FONT_SIZE);
//释放工具
graphics2D.dispose();
outs=new FileOutputStream(BASE_PATH+logoFileName);
//5.创建图片编码工具类--JPEGImageEncoder
ImageIO.write(bufferedImage, FORMAT_NAME, outs);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(outs!=null){
try {
outs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return logoFileName;
}
/**
* 获取文本宽度
* @param text
* @return
*/
public static int getTextLength(String text){
if(text==null || "".equals(text)){
return 0;
}
int length=text.length();
for(int i=0;i0){
length++;
}
}
length=length%2==0?length/2:length/2+1;
return length;
}
/**
* @param image
* @param imageFileName
* @return
*/
public String waterMarkBySingleImage(File image,String imageFileName){
String logoFileName="logo_single_image_"+imageFileName;
OutputStream outs=null;
String logoPath=MARK_LOGO_IMAGE;
try {
//通过ImageIO获取图像文件的像素大小 即宽/高
Image imageTemp=ImageIO.read(image);
int width=imageTemp.getWidth(null);
int height=imageTemp.getHeight(null);
//1.创建缓存图片对象--BufferedImage
/**
* new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
* width width of the created image
* height height of the created image
* imageType type of the created image
*/
BufferedImage bufferedImage=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//2.创建java绘图工具对象--Graphics2D
Graphics2D graphics2D=bufferedImage.createGraphics();
//3.使用绘图工具对象将原图绘制到缓存图片对象
/**
* img the specified image to be drawn. This method does nothing if img is null.
* x the x coordinate.
* y the y coordinate.
* width the width of the rectangle.
* height the height of the rectangle.
* observer object to be notified as more of the image is converted.
*/
graphics2D.drawImage(imageTemp, 0, 0, width,height,null);
File logo=new File(logoPath);
Image logoImage=ImageIO.read(logo);
/**
* 获取水印图片的高和宽度
*/
int waterMarkWidth=logoImage.getWidth(null);
int waterMarkHeight=logoImage.getHeight(null);
//计算图片尺寸和水印尺寸差异
int widthDiff=width-waterMarkWidth;
int heightDiff=height-waterMarkHeight;
int x=X;
int y=Y;
if(x>widthDiff){
x=widthDiff;
}
if(y>heightDiff){
y=heightDiff;
}
//设置水印透明度 0.3
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
//4.使用绘图工具将水印(文字或图片)绘制到缓存图片对象
graphics2D.drawImage(logoImage, x, y,null);
//释放工具
graphics2D.dispose();
outs=new FileOutputStream(BASE_PATH+logoFileName);
//5.创建图片编码工具类--JPEGImageEncoder
ImageIO.write(bufferedImage, FORMAT_NAME, outs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(outs!=null){
try {
outs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return logoFileName;
}}