package com.funi.test;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImageRemarkUtil {
private static float alpha = 0.5f;
private static int positionWidth = 150;
private static int positionHeight = 300;
private static Font font = new Font("宋体", Font.BOLD, 72);
private static Color color = Color.red;
public static void setImageMarkOptions(float alpha, int positionWidth,
int positionHeight, Font font, Color color) {
if (alpha != 0.0f)
ImageRemarkUtil.alpha = alpha;
if (positionWidth != 0)
ImageRemarkUtil.positionWidth = positionWidth;
if (positionHeight != 0)
ImageRemarkUtil.positionHeight = positionHeight;
if (font != null)
ImageRemarkUtil.font = font;
if (color != null)
ImageRemarkUtil.color = color;
}
public static void markImageByIcon(String iconPath, String srcImgPath,
String targerPath) {
markImageByIcon(iconPath, srcImgPath, targerPath, null);
}
public static void markImageByIcon(String iconPath, String srcImgPath,
String targerPath, Integer degree) {
OutputStream os = null;
try {
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(
srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
null);
if (null != degree) {
g.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2,
(double) buffImg.getHeight() / 2);
}
ImageIcon imgIcon = new ImageIcon(iconPath);
Image img = imgIcon.getImage();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawImage(img, positionWidth, positionHeight, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(targerPath);
ImageIO.write(buffImg, "JPG", os);
System.out.println("图片完成添加水印图片");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void markImageByText(String logoText, String srcImgPath,
String targerPath) {
markImageByText(logoText, srcImgPath, targerPath, null);
}
public static void markImageByText(String logoText, String srcImgPath,
String targerPath, Integer degree) {
InputStream is = null;
OutputStream os = null;
try {
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(
srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
null);
if (null != degree) {
g.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2,
(double) buffImg.getHeight() / 2);
}
g.setColor(color);
g.setFont(font);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
alpha));
g.drawString(logoText, positionWidth, positionHeight);
g.dispose();
os = new FileOutputStream(targerPath);
ImageIO.write(buffImg, "JPG", os);
System.out.println("图片完成添加水印文字");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String srcImgPath = "d:/1.jpg";
String logoText = "复 印 无 效";
String iconPath = "d:/2.jpg";
String targerTextPath = "d:/qie_text.jpg";
String targerTextPath2 = "d:/qie_text_rotate.jpg";
String targerIconPath = "d:/qie_icon.jpg";
String targerIconPath2 = "d:/qie_icon_rotate.jpg";
System.out.println("给图片添加水印文字开始...");
markImageByText(logoText, srcImgPath, targerTextPath);
markImageByText(logoText, srcImgPath, targerTextPath2, -45);
System.out.println("给图片添加水印文字结束...");
System.out.println("给图片添加水印图片开始...");
setImageMarkOptions(0.3f, 1, 1, null, null);
markImageByIcon(iconPath, srcImgPath, targerIconPath);
markImageByIcon(iconPath, srcImgPath, targerIconPath2, -45);
System.out.println("给图片添加水印图片结束...");
}
}