我想在Java的解码图像上打印特定格式的水印,例如图像上的时间戳,纬度,经度。我为它创建了一个watermarkformat pojo类。现在,我想在解码/渲染的图像上给该特定格式加水印,但是Graphics2D drawString()方法采用String和x,y坐标。我应如何将对象转换为字符串以传递给drawString()
请看下面的代码-
带水印的BufferedImage =新的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());
}