java给图片加水印
Posted 宇宙磅礴而冷漠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java给图片加水印相关的知识,希望对你有一定的参考价值。
以下提供两个方法
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;
public class TxtWatermark {
// 水印透明度
private static float alpha = 0.8f;
// 水印文字大小
public static final int FONT_SIZE = 20;
// 水印文字颜色
private static Color color = new Color(220,220,220);
/**
* 获取文本长度。汉字为1:1,英文和数字为2:1
*/
private static int getTextLength (String text) {
int length = text.length ();
for (int i = 0; i < text.length (); i++) {
String s = String.valueOf (text.charAt (i));
if (s.getBytes ().length > 1) {
length++;
}
}
length = length % 2 == 0 ? length / 2 : length / 2 + 1;
return length;
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
*
* @param logoText
* @param srcImgPath
* @param targerPath
*
*/
public static void ImageByMultipleText (String logoText, String srcImgPath, String targerPath) {
InputStream is = null;
OutputStream os = null;
try {
// 源图片
String suffix=srcImgPath.split("\\\\.")[1];
Image srcImg = ImageIO.read (new File (srcImgPath));
int width = srcImg.getWidth (null);// 原图宽度
int height = srcImg.getHeight (null);// 原图高度
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);
// 设置水印旋转
g.rotate (Math.toRadians(45));
// 设置水印文字颜色
g.setColor (color);
// 设置水印文字Font
// g.setFont (font);
//计算字体大小以200像素为例
double multiple=1;
if(width>548) {
multiple=width/548.8;
}else {
multiple=height/548.8;
}
int newSize=(int) (FONT_SIZE*multiple);
if(newSize<20) {
newSize=20;
}
Font newFont = new Font ("微软雅黑", Font.BOLD, newSize);
g.setFont(newFont);
// 设置水印文字透明度
g.setComposite (AlphaComposite.getInstance (AlphaComposite.SRC_ATOP, alpha));
int markWidth = newSize * getTextLength (logoText);// 字体长度
int rowsNumber = height/markWidth+1;
int columnsNumber = width/markWidth+1;
if(rowsNumber<1) {
rowsNumber=1;
}
if(columnsNumber<1) {
columnsNumber=1;
}
// 循环添加水印
for(int j=0;j<rowsNumber;j++){
for(int i=0;i<columnsNumber;i++){
g.drawString(logoText, (int) (i*markWidth + j*markWidth + width*0.1), -i*markWidth + j*markWidth);//画出水印,并设置水印位置
}
}
// 释放资源
g.dispose ();
// 生成图片
os = new FileOutputStream (targerPath);
ImageIO.write (buffImg, suffix, 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 ();
}
}
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
*
* @param logoText
* @param srcImgPath
* @param targerPath
*
*/
public static void ImageByMultipleText (String logoText,String suffix,InputStream is, OutputStream os) {
try {
// 源图片
Image srcImg = ImageIO.read (is);
int width = srcImg.getWidth (null);// 原图宽度
int height = srcImg.getHeight (null);// 原图高度
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);
// 设置水印旋转
g.rotate (Math.toRadians(45));
// 设置水印文字颜色
g.setColor (color);
// 设置水印文字Font
// g.setFont (font);
//计算字体大小以200像素为例
double multiple=1;
if(width>548) {
multiple=width/548.8;
}else {
multiple=height/548.8;
}
int newSize=(int) (FONT_SIZE*multiple);
if(newSize<20) {
newSize=20;
}
Font newFont = new Font ("微软雅黑", Font.BOLD, newSize);
g.setFont(newFont);
// 设置水印文字透明度
g.setComposite (AlphaComposite.getInstance (AlphaComposite.SRC_ATOP, alpha));
int markWidth = newSize * getTextLength (logoText);// 字体长度
int rowsNumber = height/markWidth+1;
int columnsNumber = width/markWidth+1;
if(rowsNumber<1) {
rowsNumber=1;
}
if(columnsNumber<1) {
columnsNumber=1;
}
// 循环添加水印
for(int j=0;j<rowsNumber;j++){
for(int i=0;i<columnsNumber;i++){
g.drawString(logoText, (int) (i*markWidth + j*markWidth + width*0.1), -i*markWidth + j*markWidth);//画出水印,并设置水印位置
}
}
// 释放资源
g.dispose ();
// 生成图片
ImageIO.write (buffImg, suffix, os);
System.out.println ("添加水印文字成功!");
} catch (Exception e) {
e.printStackTrace ();
}
}
}
以上是关于java给图片加水印的主要内容,如果未能解决你的问题,请参考以下文章