在Java中将多行文本作为水印添加到图像上。

huangapple go评论97阅读模式
英文:

Multiple Text Lines as Watermark on image in java

问题

// 初始化水印图片
BufferedImage watermarked = new BufferedImage(imageWidth, imageHeight, imageType);

// 初始化绘图对象
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));

// 这里是您希望的方法,它接受 WatermarkFormat 对象或其他替代方法
WatermarkFormat watermarkFormat = new WatermarkFormat(); // 假设您已经创建了 WatermarkFormat 对象
String formattedText = watermarkFormat.toString(); // 将对象转换为字符串
w.drawString(formattedText, 100, 70);

ImageIO.write(watermarked, type, destination);
w.dispose();

在上述代码中,您需要创建一个 WatermarkFormat 类的实例,并且该类需要覆写 toString() 方法,以便将您想要的特定格式转换为字符串。然后您可以将转换后的字符串传递给 drawString() 方法来绘制水印。这种方法允许您将对象的内容转换为可在图像上绘制的格式。

英文:

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 ?

答案1

得分: 2

如果您所说的是多行水印,您可能想尝试使用以下方法替换您当前的w.drawString()方法:

// 如果文本是一个包含换行字符的单一字符串
for (String line : text.split("\n")) {
w.drawString(line, x, y += w.getFontMetrics().getHeight());
}

// 如果文本是一个名为text[]的字符串数组
for (String line : text) {
w.drawString(line, x, y += w.getFontMetrics().getHeight());
}

英文:

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());
}

huangapple
  • 本文由 发表于 2020年4月6日 18:43:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61057977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定