天天看点

java图片加文字水印_JAVA实现图片的修改,添加文字水印效果

JAVA实现图片的修改,添加文字水印效果,根据文字内容生成图片,下面是具体的代码,以及测试方法:

实现类:

package JavaMoImage;

import javax.p_w_picpathio.ImageIO;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.p_w_picpath.BufferedImage;

import java.io.File;

import java.io.IOException;

public class ModifyPicClass {

private Font font = new Font("", Font.PLAIN, 20);// 添加字体的属性设置

private Graphics2D g = null;

private int fontsize;

private int x;

private int y;

private String imgSrcPath;

private String imgDesPath;

private String content;

private String fontStyle;

private Color color;

public BufferedImage loadImageLocal(String imgSrcPath) {

try {

System.out.println(imgSrcPath);

this.imgSrcPath = imgSrcPath;

return ImageIO.read(new File(this.imgSrcPath));

} catch (IOException e) {

System.out.println(e.getMessage());

}

return null;

}

public void writeImageLocal(String imgDesPath, BufferedImage img) {

if (imgDesPath != null && img != null) {

try {

System.out.println(imgDesPath);

this.imgDesPath = imgDesPath;

File outputfile = new File(this.imgDesPath);

System.out.println("创建对象成功,准备写文件");

ImageIO.write(img, "jpg", outputfile);

System.out.println("文件写成功");

} catch (IOException e) {

System.out.println(e.getMessage());

}

}

}

public void setFont(String fontStyle, int fontSize) {

this.fontsize = fontSize;

this.fontStyle = fontStyle;

this.font = new Font(this.fontStyle, Font.PLAIN, this.fontsize);

}

public void setLocalColor(int x,int y,Color color){

this.x = x;

this.y = y;

this.color=color;

}

public BufferedImage modifyImage(BufferedImage img, String content) {

try {

int w = img.getWidth();

int h = img.getHeight();

g = img.createGraphics();

//g.setBackground(Color.WHITE);

g.setColor(this.color);

if (this.font != null)

g.setFont(this.font);

// 验证输出位置的纵坐标和横坐标

if (x >= h || y >= w) {

this.x = h - this.fontsize + 2;

this.y = w;

}

this.content=content;

if (content != null) {

g.drawString(this.content, this.x, this.y);

}

g.dispose();

} catch (Exception e) {

System.out.println(e.getMessage());

}

return img;

}

public ModifyPicClass(String imgSrcPath, String imgDesPath, String content,

String fontStyle, int fontsize, int x, int y,Color color) {

super();

this.fontsize = fontsize;

this.x = x;

this.y = y;

this.imgSrcPath = imgSrcPath;

this.imgDesPath = imgDesPath;

this.content = content;

this.fontStyle = fontStyle;

this.color=color;

setFont(fontStyle, fontsize);

writeImageLocal(imgDesPath, modifyImage(loadImageLocal(imgSrcPath),

content));

}

public ModifyPicClass(String fontStyle, int fontsize, int x, int y,Color color) {

super();

this.fontsize = fontsize;

this.x = x;

this.y = y;

this.fontStyle = fontStyle;

this.color=color;

setFont(fontStyle, fontsize);

setLocalColor(x,y,color);

}

}

测试类:

package JavaMoImage;

import java.awt.Color;

public class ModifyPicTest {

public static void main(String[] args) {

ModifyPicClass mp1 = new ModifyPicClass("e:\\me2.jpg","e:\\new14.jpg","我的地球","STYLE_ITALIC",40,300,300,Color.RED);

ModifyPicClass mp2 = new ModifyPicClass("STYLE_ITALIC",40,300,300,Color.RED);

mp2.writeImageLocal("e:\\new15.jpg", mp2.modifyImage(mp2.loadImageLocal("e:\\me2.jpg"),

"天堂杭州"));

}

}

测试结果:

图片不好上传,效果就是在原来的图片上面,添加了天堂杭州四个字