简体   繁体   English

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

[英]Multiple Text Lines as Watermark on image in java

I want to print a specific format as watermark on decoded image in java such as timestamp,latitude,longitude on image.我想在 java 中的解码图像上打印特定格式作为水印,例如图像上的时间戳、纬度、经度。 I have created a watermarkformat pojo class for it.我为它创建了一个 watermarkformat pojo class。 Now i want to watermark that particular format on decoded/rendered image, but Graphics2D drawString() method takes String and x,y coordinates.现在我想在解码/渲染图像上为该特定格式添加水印,但 Graphics2D drawString() 方法采用字符串和 x,y 坐标。 How shall i convert my object in to string to pass to drawString()我应该如何将我的 object 转换为字符串以传递给 drawString()

Look at the following code -看下面的代码——

BufferedImage watermarked = new BufferedImage(imageWidth, imageHeight, imageType); 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:如果您正在谈论的是多行水印,那么您可能想尝试这个来替换您当前的 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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM