-
package com.qihui.qxj.utils;
-
import java.awt.Color;
-
import java.awt.Font;
-
import java.awt.Graphics;
-
import java.awt.Graphics2D;
-
import java.awt.Image;
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
-
import javax.imageio.ImageIO;
-
-
import com.sun.image.codec.jpeg.JPEGCodec;
-
import com.sun.image.codec.jpeg.JPEGImageEncoder;
-
public
class Main {
-
public static void main(String[] args) throws IOException {
-
/* String sourceImg="D:\\test.jpg"; //源图片地址
-
String targetImg="D:\\123.jpg"; //新存储的地址
-
addWatermark(sourceImg, targetImg);*/
-
pressImage(
"d:/1557906953.jpg",
"d:/1557814473(1).jpg");
-
}
-
-
/**
-
* 把图片印刷到图片上
-
*
-
* @param pressImg --
-
* 水印文件
-
* @param targetImg --
-
* 目标文件
-
*/
-
public final static void pressImage(String pressImg, String targetImg) {
-
try {
-
//目标文件
-
File _file =
new File(targetImg);
-
Image src = ImageIO.read(_file);
-
int wideth = src.getWidth(
null);
-
int height = src.getHeight(
null);
-
BufferedImage image =
new BufferedImage(wideth, height,
-
BufferedImage.TYPE_INT_RGB);
-
Graphics g = image.createGraphics();
-
g.drawImage(src,
0,
0, wideth, height,
null);
-
-
//水印文件
-
File _filebiao =
new File(pressImg);
-
Image src_biao = ImageIO.read(_filebiao);
-
int wideth_biao = src_biao.getWidth(
null);
-
int height_biao = src_biao.getHeight(
null);
-
g.drawImage(src_biao,
3000,
-
2020, wideth_biao, height_biao,
null);
-
//水印文件结束
-
g.dispose();
-
FileOutputStream out =
new FileOutputStream(targetImg);
-
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
-
encoder.encode(image);
-
out.close();
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
-
-
/** 设置文字水印
-
* @param sourceImg 源图片路径
-
* @param targetImg 保存的图片路径
-
* @throws IOException
-
*/
-
public static void addWatermark(String sourceImg, String targetImg) throws IOException {
-
File srcImgFile =
new File(sourceImg);
-
Image srcImg = ImageIO.read(srcImgFile);
-
int srcImgWidth = srcImg.getWidth(
null);
-
int srcImgHeight = srcImg.getHeight(
null);
-
int fontSize=
75;
-
Font font =
new Font(
"黑体,Arial",
1, fontSize);
-
BufferedImage bufImg =
new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
-
Graphics2D g = bufImg.createGraphics();
-
g.drawImage(srcImg,
0,
0, srcImgWidth, srcImgHeight,
null);
-
g.setColor(Color.BLACK);
-
g.setFont(font);
-
-
//设置水印的坐标
-
g.drawString(
"测试",
3196,
150);
-
g.drawString(
"性别:男",
3196,
150+(
120*
1));
-
g.drawString(
"毕业学校:XXX大学",
3196,
150+(
120*
2));
-
g.setColor(Color.white);
-
font =
new Font(
"黑体,Arial",
1,
120);
-
g.setFont(font);
-
g.drawString(
"广东省深圳(SHENZHEN)",
3000,
2020);
-
g.dispose();
-
// 输出图片
-
FileOutputStream outImgStream =
new FileOutputStream(targetImg);
-
ImageIO.write(bufImg,
"jpg", outImgStream);
-
System.out.println(
"添加水印完成");
-
outImgStream.flush();
-
outImgStream.close();
-
}
-
}
红色选中的部分是添加的水印。
转载自:https://blog.csdn.net/qq_16855077/article/details/90238575
-
package com.qihui.qxj.utils;
-
import java.awt.Color;
-
import java.awt.Font;
-
import java.awt.Graphics;
-
import java.awt.Graphics2D;
-
import java.awt.Image;
-
import java.awt.image.BufferedImage;
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
-
import javax.imageio.ImageIO;
-
-
import com.sun.image.codec.jpeg.JPEGCodec;
-
import com.sun.image.codec.jpeg.JPEGImageEncoder;
-
public
class Main {
-
public static void main(String[] args) throws IOException {
-
/* String sourceImg="D:\\test.jpg"; //源图片地址
-
String targetImg="D:\\123.jpg"; //新存储的地址
-
addWatermark(sourceImg, targetImg);*/
-
pressImage(
"d:/1557906953.jpg",
"d:/1557814473(1).jpg");
-
}
-
-
/**
-
* 把图片印刷到图片上
-
*
-
* @param pressImg --
-
* 水印文件
-
* @param targetImg --
-
* 目标文件
-
*/
-
public final static void pressImage(String pressImg, String targetImg) {
-
try {
-
//目标文件
-
File _file =
new File(targetImg);
-
Image src = ImageIO.read(_file);
-
int wideth = src.getWidth(
null);
-
int height = src.getHeight(
null);
-
BufferedImage image =
new BufferedImage(wideth, height,
-
BufferedImage.TYPE_INT_RGB);
-
Graphics g = image.createGraphics();
-
g.drawImage(src,
0,
0, wideth, height,
null);
-
-
//水印文件
-
File _filebiao =
new File(pressImg);
-
Image src_biao = ImageIO.read(_filebiao);
-
int wideth_biao = src_biao.getWidth(
null);
-
int height_biao = src_biao.getHeight(
null);
-
g.drawImage(src_biao,
3000,
-
2020, wideth_biao, height_biao,
null);
-
//水印文件结束
-
g.dispose();
-
FileOutputStream out =
new FileOutputStream(targetImg);
-
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
-
encoder.encode(image);
-
out.close();
-
}
catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
-
-
/** 设置文字水印
-
* @param sourceImg 源图片路径
-
* @param targetImg 保存的图片路径
-
* @throws IOException
-
*/
-
public static void addWatermark(String sourceImg, String targetImg) throws IOException {
-
File srcImgFile =
new File(sourceImg);
-
Image srcImg = ImageIO.read(srcImgFile);
-
int srcImgWidth = srcImg.getWidth(
null);
-
int srcImgHeight = srcImg.getHeight(
null);
-
int fontSize=
75;
-
Font font =
new Font(
"黑体,Arial",
1, fontSize);
-
BufferedImage bufImg =
new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
-
Graphics2D g = bufImg.createGraphics();
-
g.drawImage(srcImg,
0,
0, srcImgWidth, srcImgHeight,
null);
-
g.setColor(Color.BLACK);
-
g.setFont(font);
-
-
//设置水印的坐标
-
g.drawString(
"测试",
3196,
150);
-
g.drawString(
"性别:男",
3196,
150+(
120*
1));
-
g.drawString(
"毕业学校:XXX大学",
3196,
150+(
120*
2));
-
g.setColor(Color.white);
-
font =
new Font(
"黑体,Arial",
1,
120);
-
g.setFont(font);
-
g.drawString(
"广东省深圳(SHENZHEN)",
3000,
2020);
-
g.dispose();
-
// 输出图片
-
FileOutputStream outImgStream =
new FileOutputStream(targetImg);
-
ImageIO.write(bufImg,
"jpg", outImgStream);
-
System.out.println(
"添加水印完成");
-
outImgStream.flush();
-
outImgStream.close();
-
}
-
}