java 中图像上的多个文本行作为水印

Multiple Text Lines as Watermark on image in java

我想在 java 中的解码图像上打印特定格式作为水印,例如图像上的时间戳、纬度、经度。我为它创建了一个 watermarkformat pojo class。 现在我想在 decoded/rendered 图像上为该特定格式加水印,但 Graphics2D drawString() 方法采用 String 和 x,y 坐标。我该如何将我的对象转换为字符串以传递给 drawString()

看下面代码-

BufferedImage watermarked = new BufferedImage(imageWidth, imageHeight, imageType);

    // initializes necessary graphic properties
    Graphics2D w = (Graphics2D) watermarked.getGraphics();
    w.drawImage(image, 0, 0, null);
    AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
    w.setComposite(alphaChannel);
    w.setColor(Color.RED);
    w.setFont(new Font("Verdana", Font.BOLD, 12));
    w.drawString(text,100,70); // here i want alternative method which takes watermarkformat object or any alternative way 
    ImageIO.write(watermarked, type, destination);
    w.dispose();

请帮忙在图像上打印特定格式的替代方法是什么?

如果你说的是多行水印,那么你可能想尝试用这个来替换你当前的 w.drawString() 方法:

// If the text is in a single string with newline characters in it
for (String line : text.split("\n")) {
    w.drawString(line, x, y += w.getFontMetrics().getHeight());
}

// If the text is in a String Array named: text[]
for (String line : text) {
    w.drawString(line, x, y += w.getFontMetrics().getHeight());
}