📄 watermark.java
字号:
package net.javaok.framework.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* @author 侯明强
*
*/
public final class WaterMark {
private static List logoImgs;
private static List bgImgs;
private static boolean flag;
public static List getBgImgs() {
return bgImgs;
}
/**
* 把图片印刷到图片上
*
* @param pressImg --
* 水印文件
* @param targetImg --
* 目标文件
* @param x
* --x坐标
* @param y
* --y坐标
* @param out
* 输出流(可以来自HttpServletReponse的输出)
*/
public final static void pressImage(String pressImg, String targetImg,
int x, int y,OutputStream out) {
try {
//目标文件
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
//水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, (wideth - wideth_biao) / 2,
(height - height_biao) / 2, wideth_biao, height_biao, null);
//水印文件结束
g.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打印文字水印图片
*
* @param pressText
* --文字
* @param targetImg --
* 目标图片
* @param fontName --
* 字体名
* @param fontStyle --
* 字体样式
* @param color --
* 字体颜色
* @param fontSize --
* 字体大小
* @param x --
* 偏移量(从右下角算起)
* @param y --
* 偏移量(从右下角算起)
*
* @param out
* 输出流(可以来自HttpServletReponse的输出)
*/
public static void pressText(String pressText, String targetImg,
String fontName, int fontStyle, int color, int fontSize, int x,
int y,OutputStream out) {
try {
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
g.setColor(new Color(color));
g.setFont(new Font(fontName, fontStyle, fontSize));
g.drawString(pressText, wideth - fontSize - x, height - fontSize
/ 2 - y);
g.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
/**
* 设置背景水印图片的路径并把图片加载到内存
*
* @param bgImgUrls
* 背景水印图片的相对路径集合
* @param path
* 图片组所在的文件夹路径
*/
public static void setBgImgs(List bgImgUrls, String path) {
bgImgs = new ArrayList();
for (Iterator it = bgImgUrls.iterator(); it.hasNext();) {
String tem = (String) it.next();
ImageIcon waterIcon = new ImageIcon(path + tem);
bgImgs.add(waterIcon.getImage());
}
}
public static List getLogoImgs() {
return logoImgs;
}
/**
* 设置图标水印图片的路径并把图片加载到内存
*
* @param logoImgUrls
* 图标水印图片的相对路径集合
* @param path
* 图片组所在的文件夹路径
*/
public static void setLogoImgs(List logoImgUrls, String path) {
logoImgs = new ArrayList();
for (Iterator it = logoImgUrls.iterator(); it.hasNext();) {
String tem = (String) it.next();
ImageIcon waterIcon = new ImageIcon(path + tem);
logoImgs.add(waterIcon.getImage());
}
}
/**
* 图片中添加图标水印并输出到指定流
*
* @param data
* @param out
* @param channel
* @return
* @throws Exception
*/
public static boolean createLogoMark(byte[] data, FileOutputStream out,
String channel) throws Exception {
int i = (int) (logoImgs.size() * Math.random());
return createMark(data, out, (Image) logoImgs.get(i), true);
}
/** */
/**
* 图片中添加背景水印并输出到指定流
*
* @param data
* @param out
* @param channelName
* @return
* @throws Exception
*/
public static boolean createBgMark(byte[] data, FileOutputStream out,
String channelName) throws Exception {
int i = (int) (bgImgs.size() * Math.random());
return createMark(data, out, (Image) bgImgs.get(i), false);
}
/**
* 生成随机水印并输出到指定流
*
* @param data
* @param out
* @return
* @throws Exception
*/
public static boolean createRandomMark(byte[] data, FileOutputStream out)
throws Exception {
int i = 0;
Image temImg = null;
if (!flag) {
i = (int) (logoImgs.size() * Math.random());
temImg = (Image) logoImgs.get(i);
} else {
i = (int) (bgImgs.size() * Math.random());
temImg = (Image) bgImgs.get(i);
}
flag = !flag;
return createMark(data, out, temImg, flag);
}
/**
* 生成水印并输出到指定流
*
* @param data
* @param out
* @param waterImg
* 水印图片的类型(背景或图标),应与isLogoImg参数一致
* @param isLogoImg
* 等于true 时生成图标水印,否则为背景水印
* @return
* @throws Exception
*/
private static boolean createMark(byte[] data, FileOutputStream out,
Image waterImg, boolean isLogoImg) throws Exception {
ImageIcon imgIcon = new ImageIcon(data);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
if (width < 200 || height < 200) {// 小图片不加水印真接输入,如头像图片
BufferedOutputStream fout = null;
ByteArrayInputStream in = new ByteArrayInputStream(data);
try {
byte[] b = new byte[1024 * 10];
fout = new BufferedOutputStream(out);
while (in.read(b) > 0) {
out.write(b);
}
out.flush();
out.close();
in.close();
return true;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -