package beans;
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.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
* 图片处理工具类 基于https://blog.csdn.net/beinlife/article/details/53665152 改造
*/
public class ImageWatermark {
/**
* 水印之间的横向间隔
*/
private static final int XMOVE = 120;
/**
* 水印之间的纵向间隔
*/
private static final int YMOVE = 75;
public static void createWaterMarkByIcon(File srcImageFile, File logoImageFile,
File outputImageFile, double degree) {
OutputStream os = null;
try {
Image srcImg = ImageIO.read(srcImageFile);
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = buffImg.createGraphics();
buffImg = graphics.getDeviceConfiguration().createCompatibleImage(srcImg.getWidth(null), srcImg.getHeight(null), Transparency.TRANSLUCENT);
graphics = buffImg.createGraphics();
//graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
int imgWidth = srcImg.getWidth(null);
int imgHeight = srcImg.getHeight(null);
graphics.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile));
Image logoImg = logoImgIcon.getImage();
//旋转
if (degree>0) {
graphics.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 2,
(double) buffImg.getHeight() / 2);
}
float alpha = 0.7f; // 透明度
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
//水印 的位置
Image logoImg2 = ImageIO.read(logoImageFile);
int logoWidth = logoImg2.getWidth(null);
int logoHeight = logoImg2.getHeight(null);
// 文字长度相对于图片宽度应该有多少行
int line =buffImg.getWidth()/logoWidth;
int co = buffImg.getHeight()/logoHeight;
// 填充Y轴方向
for (int y = 0; y < co; y++) {
// 填充X轴
for (int x = 0; x < line; x++) {
graphics.drawImage(logoImg, 0+x*(logoWidth+XMOVE), 0+y*(logoHeight+YMOVE),logoWidth,logoHeight, null);
}
}
// graphics.drawImage(logoImg, (buffImg.getWidth()-logoWidth)/2, (buffImg.getHeight()-logoHeight)/2,logoWidth,logoHeight, null);
//graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
graphics.dispose();
os = new FileOutputStream(outputImageFile);
// 生成图片
ImageIO.write(buffImg, "PNG", os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
createWaterMarkByIcon(new File("c:/old/444_out.jpg"),new File("c:/old/markt.png"),
new File("c:/old/1999.png"),0);
}
}
免责声明: | |
1、 | 资源售价只是赞助,不代表代码或者素材本身价格。收取费用仅维持本站的日常运营所需。 |
2、 | 本站资源来自用户上传,仅供用户学习使用,不得用于商业或者非法用途,违反国家法律一切后果用户自负。用于商业用途,请购买正版授权合法使用。 |
3、 | 本站资源不保证其完整性和安全性,下载后自行检测安全,在使用过程中出现的任何问题均与本站无关,本站不承担任何技术及版权问题,不对任何资源负法律责任。 |
4、 | 如有损害你的权益,请联系275551777@qq.com及时删除。 |