如何在图片上添加水印文字

package D6;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class A2 {
    public static void main(String[] args) throws Exception {
        int width = 500;
        int height = 500;
        BufferedImage img = ImageIO.read(new File("E:\\永劫无间图\\abc.png"));
        Graphics2D g = img.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        String text = "版权所有 电话:123456";
        int size = 40;
        Font a = Font.createFont(Font.TRUETYPE_FONT,new File("E:\\北游作业和练习\\java练习\\wa2\\src\\D6\\zh164hfyh.ttf"));
        Font font = a.deriveFont(Font.BOLD,size);
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics();
        int fw = fm.stringWidth(text);
        int fh = fm.getAscent() - (fm.getHeight() - fm.getAscent());

        g.setColor(Color.RED);
        int sp = 10;
        int x = sp;
        int y = fh + sp;
        x = (width - fw) / 2;
        y = (height - fh) / 2 + fh;
        x = width - fw - sp;
        y = height - fh + sp;
        g.drawString(text, x, y);


        //g.setColor(Color.YELLOW);
        //g.drawLine(250, 0, 250, 500);
        //g.drawLine(0, 250, 500, 250);


        g.dispose();
        ImageIO.write(img, "png", new File("abc.png"));

        Runtime.getRuntime().exec("cmd /k start abc.png");

    }
}

你可能感兴趣的:(java,java,jvm,开发语言)