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);
}
/**
* 设定文字在图片中的位置以及文字的颜色
* @param x
* @param y
* @param color
*/
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"),
"天堂杭州"));
}
}
测试结果:
图片不好上传,效果就是在原来的图片上面,添加了天堂杭州四个字
如果觉得《java图片加文字水印_JAVA实现图片的修改 添加文字水印效果》对你有帮助,请点赞、收藏,并留下你的观点哦!