原始图:
水印原始模板:
添加水印后的效果图
package com.sjaco.hy.api.test;
import java.awt.image.BufferedImage;
import java.io.File;
public class Test {
public static void main(String[] args) {
Data data=new Data();
data.setOriginalFilePath("C:\\Users\\18710\\Desktop\\2.png");//模板地址
data.setNewFilePath("C:\\Users\\18710\\Desktop\\100.png");//输出地址
data.setTitle("110kXX线路光缆巡检项目");
data.setTime("2020.06.01 15:26");
data.setWeather("阴 31");
data.setAddress("红河哈尼族黎族自治州.朝阳苑(朝阳路)");
data.setAltitude("1304.5米");
data.setLongitude("103.3882474");
data.setLatitude("23.35864044");
aADDatermark("C:\\Users\\18710\\Desktop\\1.png","C:\\Users\\18710\\Desktop\\sc.png",data);
}
/**
* 给图片添加水印
*
*
*
* @param srcImgPath
* 源图片路径
* @param targerPath
* 目标图片路径
*/
private static void aADDatermark( String srcImgPath, String targerPath,Data data){
//添加文字后的水印地址
String path= generateWatermark(data);
WaterMarkUtils.markImageByIcon(path, srcImgPath, targerPath);
File file = new File(path);
file.delete();
}
/**
*
* 给图片添加文字
*
* @param data 参数
* @return 添加文字后的图片路径
*/
public static String generateWatermark(Data data) {
FileUtil tt = new FileUtil();
BufferedImage d = tt.loadImageLocal(data.getOriginalFilePath());
tt.modifyImage(d, data.getTitle(), 100, 50);
tt.modifyImage(d, data.getTime(), 100, 100);
tt.modifyImage(d, data.getWeather(), 100, 135);
String address=data.getAddress();
if (address.length()>=15){
tt.modifyImage(d, address.substring(0,15), 100, 170);
tt.modifyImage(d, address.substring(15), 100, 200);
}else {
tt.modifyImage(d, address, 100, 170);
}
tt.modifyImage(d, data.getAltitude(), 100, 245);
tt.modifyImage(d, data.getLongitude(), 100, 280);
tt.modifyImage(d, data.getLatitude(), 100, 320);
tt.writeImageLocal(data.getNewFilePath(), d);
return data.getNewFilePath();
}
}
package com.sjaco.hy.api.test;
public class Data {
/**
* 原始文件模板
*/
private String originalFilePath;
/**
* 添加文字后的文件地址
*/
private String newFilePath;
/**
* 标题
*/
private String title;
/**
* 时间
*/
private String time;
/**
* 天气
*/
private String weather;
/**
* 地址
*/
private String address;
/**
* 海拔
*/
private String altitude;
/**
* 经度
*/
private String longitude;
/**
* 纬度
*/
private String latitude;
public String getOriginalFilePath() {
return originalFilePath;
}
public void setOriginalFilePath(String originalFilePath) {
this.originalFilePath = originalFilePath;
}
public String getNewFilePath() {
return newFilePath;
}
public void setNewFilePath(String newFilePath) {
this.newFilePath = newFilePath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAltitude() {
return altitude;
}
public void setAltitude(String altitude) {
this.altitude = altitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
}
package com.sjaco.hy.api.test;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@Slf4j
public class FileUtil {
private Font font = new Font("黑体", Font.BOLD, 25); // 添加字体的属性设置
private Graphics2D g = null;
/**
* 修改图片,返回修改后的图片缓冲区(只输出一行文本)
*/
public BufferedImage modifyImage(BufferedImage img, Object content, int x, int y) {
try {
int w = img.getWidth();
int h = img.getHeight();
g = img.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(new Color(120, 120, 110));//设置字体颜色
if (this.font != null)
g.setFont(this.font);
if (content != null) {
// createGraphics().translate(w, h );
g.translate(0, 0);
// g.rotate(8 * Math.PI / 180); //旋转字体
g.drawString(content.toString(), x, y);
}
g.dispose();
} catch (Exception e) {
log.info(e.getMessage());
}
return img;
}
/**
* 生成新图片到本地
*/
public void writeImageLocal(String newImage, BufferedImage img) {
if (newImage != null && img != null) {
try {
File outputfile = new File(newImage);
ImageIO.write(img, "png", outputfile);
} catch (IOException e) {
log.info(e.getMessage());
}
}
}
/**
* 导入本地图片到缓冲区
*/
public BufferedImage loadImageLocal(String imgName) {
try {
return ImageIO.read(new File(imgName));
} catch (IOException e) {
log.info(e.getMessage());
}
return null;
}
public static void main(String[] args) {
FileUtil tt = new FileUtil();
BufferedImage d = tt.loadImageLocal("C:\\Users\\18710\\Desktop\\2.png");
tt.modifyImage(d, "110kXX线路光缆巡检项目", 100, 50);
tt.modifyImage(d, "2020.06.01 15:26", 100, 100);
tt.modifyImage(d, "阴 31", 100, 135);
tt.modifyImage(d, "红河哈尼族黎族自治州.朝阳苑(", 100, 170);
tt.modifyImage(d, "朝阳路)", 100, 200);
tt.modifyImage(d, "1304.5米", 100, 245);
tt.modifyImage(d, "103.3882474", 100, 280);
tt.modifyImage(d, "23.35864044", 100, 320);
tt.writeImageLocal("C:\\Users\\18710\\Desktop\\100.png", d);
log.info("success");
}
}
package com.sjaco.hy.api.test;
import lombok.extern.slf4j.Slf4j;
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.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
@Slf4j
public class WaterMarkUtils {
private static int gethigth(String s) throws Exception{
File picture=new File(s);
BufferedImage sourceImg=ImageIO.read(new FileInputStream(picture));
log.info("宽"+sourceImg.getWidth());
return sourceImg.getHeight();
}
/**
* 给图片添加水印
*
* @param iconPath
* 水印图片路径
* @param srcImgPath
* 源图片路径
* @param targerPath
* 目标图片路径
*/
public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath) {
markImageByIcon(iconPath, srcImgPath, targerPath, null);
}
/**
* 给图片添加水印、可设置水印图片旋转角度
*
* @param iconPath
* 水印图片路径
* @param srcImgPath
* 源图片路径
* @param targerPath
* 目标图片路径
* @param degree
* 水印图片旋转角度
*/
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);
// 得到画笔对象
// Graphics g= buffImg.getGraphics();
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);
}
// 水印图象的路径 水印一般为gif或者png的,这样可设置透明度
ImageIcon imgIcon = new ImageIcon(iconPath);
// 得到Image对象。
Image img = imgIcon.getImage();
float alpha = 0.8f; // 透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int yheight=gethigth(srcImgPath);//原图片高度
int sheight=gethigth(iconPath);//水印图片高度
// 表示水印图片的位置
g.drawImage(img, 20, yheight-sheight-5, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(targerPath);
// 生成图片
ImageIO.write(buffImg, "JPG", os);
log.info("图片完成添加水印完成。。。。。。");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
String srcImgPath = "C:\\Users\\18710\\Desktop\\1.png";
String iconPath = "C:\\Users\\18710\\Desktop\\2.png";
String targerPath = "C:\\Users\\18710\\Desktop\\mubiao.png";
// String targerPath2 = "C:/Users/liqiang/Desktop/图片/qlq2.jpeg";
gethigth(iconPath);
// 给图片添加水印
WaterMarkUtils.markImageByIcon(iconPath, srcImgPath, targerPath);
}
}