import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
* 给图片添加水印
* @author mahualong
* @param filename 上传文件名
* @param markImg 水印的文件名
* @param transParent 类型,居中、右下角、左上角、矩形
* @param markAngle 水印旋转角度,应在正负45度之间
* @throws IOException
public static void markImage(String filename, String markImg, String transParent, double markAngle,Long siteId) throws IOException {
int margin_x=1;
int margin_y=1;
String[] filenames = filename.split(";");
String originImgPath = "";
String markImgPath = ConfigUtil.getPublishPath("upload", siteId, 1, markImg);
for(int k=0;k<filenames.length;k++) {
originImgPath = ConfigUtil.getPublishPath("usertemp", filenames[k]);
float opacity = Float.parseFloat("1");
BufferedImage originImg = ImageIO.read(new File(originImgPath));
BufferedImage markImage = ImageIO.read(new File(markImgPath));
Graphics2D graphics = (Graphics2D) originImg.getGraphics();
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, opacity));
int canvasHeight = originImg.getHeight();
int canvasWidth = originImg.getWidth();
int markHeight = markImage.getHeight();
int markWidth = markImage.getWidth();
int interval = markWidth + markHeight;
switch (transParent){
case "1"://左上角
graphics.rotate(0);
graphics.drawImage(markImage, 10, 10, markImage.getWidth(), markImage.getHeight(), null);
break;
case "2"://居中
graphics.rotate(0);
graphics.drawImage(markImage, (canvasWidth-markWidth)/2, (canvasHeight-markHeight)/2, markImage.getWidth(), markImage.getHeight(), null);
break;
case "3"://右下角
graphics.rotate(0);
graphics.drawImage(markImage, (canvasWidth-markWidth)-10, (canvasHeight-markHeight)-10, markImage.getWidth(), markImage.getHeight(), null);
break;
default://矩形
graphics.rotate(markAngle);
for (int i = -canvasHeight; i < canvasWidth + canvasHeight; i = i + interval + margin_x) {
for (int j = -canvasWidth; j < canvasHeight + canvasWidth; j = j + interval + margin_y) {
graphics.drawImage(markImage, i, j, markImage.getWidth(), markImage.getHeight(), null);
break;
graphics.dispose();
ImageIO.write(originImg, "png", new File(originImgPath));
但实际使用中会遇到水印消失的情况。
一旦加了下面代码。
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, opacity));
不论透明系数调整为多少都会导致水印消失。
需将代码调整为
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
组合规则决定图形之间颜色的相互影响。
例如图片或图形的不透明度属于整个分类。以下方法可以得到组合规则:
g2.setComposite(java.awt.Composite composite);
Composite composite = g2.getComposite();
Java 2D允许分配透明(alpha)值,以便底层的图形可以显示出来...
public static void addWaterMark(InputStream input, File file) throws Exception {
Color color = new Color(255, 200, 0, 118);
Font font = new Font("微软雅黑", Font.ITALIC, 15);
java使用BufferedImage和Graphics2D制作背景透明的水印
做小程序的同事让给做个水印图片的接口,以前没做过,百度了下,还挺多,不过做出来的要么图片不透明,要么水印透明度没法控制。改了半天终于做好了,下面上代码:
public String makewatermark(String mark){
Font font=new Font("微软雅黑", Font.PLAIN, 22);
Integer width=300;
Intege
要设置水印的深浅,可以使用 Graphics2D 对象的 setComposite 方法,该方法可以设置绘制时的 Alpha 值,从而改变图形的深浅程度。以下是一个示例代码,演示如何设置水印深浅:
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
public class WatermarkExample {
public static void main(String[] args) throws IOException {
// 读入原始图片
BufferedImage originalImage = ImageIO.read(new File("original.png"));
// 创建 Graphics2D 对象,用于绘制水印
Graphics2D g2d = originalImage.createGraphics();
// 设置 Alpha 值,控制水印深浅程度
float alpha = 0.5f; // 水印透明度为 50%
AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(alphaComposite);
// 绘制水印
Font font = new Font("Arial", Font.BOLD, 48);
g2d.setFont(font);
g2d.setColor(Color.WHITE);
String watermark = "Watermark";
FontMetrics fm = g2d.getFontMetrics();
Rectangle2D rect = fm.getStringBounds(watermark, g2d);
int x = (originalImage.getWidth() - (int) rect.getWidth()) / 2;
int y = (originalImage.getHeight() - (int) rect.getHeight()) / 2;
g2d.drawString(watermark, x, y);
// 保存修改后的图片
ImageIO.write(originalImage, "png", new File("watermarked.png"));
在该示例中,我们首先读入了一张原始图片,然后创建了一个 Graphics2D 对象,用于绘制水印。接着,我们使用 AlphaComposite 对象设置了 Alpha 值,控制了水印的深浅程度。最后,我们使用 Graphics2D 对象绘制了水印,并保存了修改后的图片。