添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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 {
    //矩形使用,-45可以反过来
//        if (markAngle > 45 || markAngle < -45) {
//            throw new RuntimeException("旋转角度必须在正负45度之间。");
//        }
        //@param margin_x      水印之间的水平间距
        int margin_x=1;
        //@param margin_y      水印之间的垂直间距
        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;
            //(x,y)用来指定图像image显示的位置
            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 对象绘制了水印,并保存了修改后的图片。