I want to print a specific format as watermark on decoded image in java such as timestamp,latitude,longitude on image. I have created a watermarkformat pojo class for it. Now i want to watermark that particular format on decoded/rendered image, but Graphics2D drawString() method takes String and x,y coordinates. How shall i convert my object in to string to pass to drawString()
Look at the following code -
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();
please help what could be the alternative way to print specific format on image?
If what you are talking about is a multi-line watermark then you may want to try this to replace your current w.drawString() method:
// 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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.