1. Overview
1.概述
Sometimes we need to add some text to an image or a set of images. Doing this manually is easy using an image editing tool. But when we want to add the same text in the same way to a significant number of pictures, it would be very useful to do this programmatically.
有时我们需要在一张图片或一组图片上添加一些文字。使用图像编辑工具手动完成这一工作是很容易的。但是当我们想以同样的方式在大量的图片上添加同样的文字时,以编程的方式来做这件事将会非常有用。
In this quick tutorial, we’re going to learn how to add some text to images using Java.
在这个快速教程中,我们将学习如何使用Java向图片添加一些文字。
2. Adding Text to an Image
2.在图像上添加文本
To read an image and add some text, we can use different classes. In the subsequent sections, we’re going to see a couple of options.
为了读取图片并添加一些文字,我们可以使用不同的类。在随后的章节中,我们将看到几个选项。
2.1. ImagePlus and ImageProcessor
2.1.ImagePlus和ImageProcessor
First, let’s see how to use the classes ImagePlus and ImageProcessor that are available in the ImageJ library. To use this library we need to include this dependency in our project:
首先,让我们看看如何使用ImagePlus和ImageProcessor,这些类在ImageJ库中是可用的。为了使用这个库,我们需要在我们的项目中包含这个依赖项。
<dependency>
<groupId>net.imagej</groupId>
<artifactId>ij</artifactId>
<version>1.51h</version>
</dependency>
To read the image we’ll use the openImage static method. The result of this method will be stored in memory using an ImagePlus object:
为了读取图像,我们将使用openImage静态方法。这个方法的结果将使用一个ImagePlus对象存储在内存中。
ImagePlus image = IJ.openImage(path);
Once we have the image loaded into memory, let’s add some text to it using the class ImageProcessor:
一旦我们将图像加载到内存中,让我们使用ImageProcessor类向它添加一些文本。
Font font = new Font("Arial", Font.BOLD, 18);
ImageProcessor ip = image.getProcessor();
ip.setColor(Color.GREEN);
ip.setFont(font);
ip.drawString(text, 0, 20);
With this code, what we’re doing is adding the specified text in green at the top left of the image. Note that we set the position using the second and third arguments of the drawString method which represent the number of pixels from the left and top respectively.
通过这段代码,我们要做的是在图像的左上方添加指定的绿色文本。注意,我们使用drawString方法的第二个和第三个参数来设置位置,这两个参数分别代表了从左边和上面的像素数。
2.2. BufferedImage and Graphics
2.2.BufferedImage和Graphics
Next, we’re going to see how we can achieve the same result using the classes BufferedImage and Graphics. The standard build of Java includes these classes, so there’s no need for additional libraries.
接下来,我们将看到如何使用BufferedImage和Graphics类来实现同样的结果。Java的标准构建包括这些类,所以不需要额外的库。
The same way we used openImage of ImageJ, we’re going to use the read method available in ImageIO:
与我们使用ImageJ的openImage一样,我们要使用ImageIO中的read方法。
BufferedImage image = ImageIO.read(new File(path));
Once we have the image loaded in memory, let’s add some text to it using the class Graphics:
一旦我们在内存中加载了图片,让我们使用Graphics类在上面添加一些文本。
Font font = new Font("Arial", Font.BOLD, 18);
Graphics g = image.getGraphics();
g.setFont(font);
g.setColor(Color.GREEN);
g.drawString(text, 0, 20);
As we can see, both alternatives are very similar in the way are used. In this case, the second and the third arguments of the method drawString are specified in the same way that we’ve done for the ImageProcessor method.
正如我们所看到的,这两个替代方法的使用方式都非常相似。在这种情况下,方法drawString的第二个和第三个参数的指定方式与我们为ImageProcessor方法所做的相同。
2.3. Draw Based on AttributedCharacterIterator
2.3.基于AttributedCharacterIterator的绘制
The method drawString available in Graphics allows us to print the text using an AttributedCharacterIterator. This means that instead of using a plain String, we could use text with some properties associated. Let’s see an example:
Graphics中的方法drawString允许我们使用AttributedCharacterIterator打印文本。这意味着我们可以不使用普通的String,而使用带有一些属性关联的文本。让我们看一个例子。
Font font = new Font("Arial", Font.BOLD, 18);
AttributedString attributedText = new AttributedString(text);
attributedText.addAttribute(TextAttribute.FONT, font);
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
Graphics g = image.getGraphics();
g.drawString(attributedText.getIterator(), 0, 20);
This way of printing the text gives us the chance to associate the format directly with the String, which is cleaner than changing Graphics object properties whenever we want to change the format.
这种打印文本的方式让我们有机会将格式直接与String相关联,这比我们每次想改变格式时改变Graphics对象属性要干净。
3. Text Alignment
3.文本对齐
Now that we’ve learned how to add a simple text in the top left of an image let’s see now how we can add this text in certain positions.
现在我们已经学会了如何在图片的左上方添加一个简单的文本,现在让我们看看如何在某些位置添加这个文本。
3.1. Centered Text
3.1.居中的文本
The first type of alignment that we’re going to tackle is centering the text. To dynamically set the correct position where we want to write the text, we need to figure out some information:
我们要解决的第一种对齐方式是将文本居中。为了动态地设置我们要写的文本的正确位置,我们需要弄清一些信息。
- Image size
- Font size
This information can be obtained very easily. In the case of the image size, this data can be accessed through the methods getWidth and getHeight of the BufferedImage object. On the other hand, to get the data related to the font size we need to use the object FontMetrics.
这些信息可以非常容易地获得。就图像大小而言,可以通过BufferedImage对象的getWidth和getHeight方法访问该数据。另一方面,为了获得与字体大小有关的数据,我们需要使用对象FontMetrics。
Let’s see an example where we calculate the correct position for our text and draw it:
让我们看一个例子,我们为我们的文本计算正确的位置并绘制它。
Graphics g = image.getGraphics();
FontMetrics metrics = g.getFontMetrics(font);
int positionX = (image.getWidth() - metrics.stringWidth(text)) / 2;
int positionY = (image.getHeight() - metrics.getHeight()) / 2 + metrics.getAscent();
g.drawString(attributedText.getIterator(), positionX, positionY);
3.2. Text Aligned in the Bottom Right
3.2.文本在右下方对齐
The next type of alignment that we’re going to see is the bottom right. In this case, we need to dynamically get the correct positions:
我们要看到的下一种对齐方式是右下方。在这种情况下,我们需要动态地获得正确的位置。
int positionX = (image.getWidth() - metrics.stringWidth(text));
int positionY = (image.getHeight() - metrics.getHeight()) + metrics.getAscent();
3.3. Text Located in the Top Left
3.3.位于左上方的文本
Finally, let’s see how to print our text in the top left:
最后,让我们看看如何在左上方打印我们的文本。
int positionX = 0;
int positionY = metrics.getAscent();
The rest of the alignments can be deduced from the three we’ve seen.
其余的排列组合可以从我们看到的三个排列组合中推导出来。
4. Adapting Text Size Based on Image
4.根据图像调整文字大小
When we draw the text in the image, we might find that this text exceeds the size of the image. To solve this, we have to adapt the size of the font that we’re using based on the image size.
当我们在图像中绘制文本时,我们可能会发现这个文本超过了图像的尺寸。为了解决这个问题,我们必须根据图像的大小来调整我们所使用的字体的大小。
First, we need to obtain the expected width and height of the text using the base font. In order to achieve this, we’ll make use of the classes FontMetrics, GlyphVector, and Shape.
首先,我们需要获得使用基本字体的文本的预期宽度和高度。为了实现这一目标,我们将利用FontMetrics、GlyphVector、和Shape这些类。
FontMetrics ruler = graphics.getFontMetrics(baseFont);
GlyphVector vector = baseFont.createGlyphVector(ruler.getFontRenderContext(), text);
Shape outline = vector.getOutline(0, 0);
double expectedWidth = outline.getBounds().getWidth();
double expectedHeight = outline.getBounds().getHeight();
The next step is to check if the resize of the font is necessary. For this purpose, let’s compare the expected size of the text and the size of the image:
下一步是检查是否有必要调整字体的大小。为此,让我们比较一下文本的预期大小和图像的大小。
boolean textFits = image.getWidth() >= expectedWidth && image.getHeight() >= expectedHeight;
Finally, if our text doesn’t fit in the image, we have to reduce the font size. We’ll use the method deriveFont for that:
最后,如果我们的文本不适合在图片中出现,我们必须减小字体大小。我们将使用方法deriveFont来实现。
double widthBasedFontSize = (baseFont.getSize2D()*image.getWidth())/expectedWidth;
double heightBasedFontSize = (baseFont.getSize2D()*image.getHeight())/expectedHeight;
double newFontSize = widthBasedFontSize < heightBasedFontSize ? widthBasedFontSize : heightBasedFontSize;
newFont = baseFont.deriveFont(baseFont.getStyle(), (float)newFontSize);
Note that we need to obtain the new font size based on both width and height and apply the lowest of them.
请注意,我们需要根据宽度和高度来获得新的字体大小,并应用其中最低的字体。
5. Summary
5.总结
In this article, we’ve seen how to write text in an image using different methods.
在这篇文章中,我们已经看到了如何用不同的方法在图片中写文字。
We’ve also learned how to dynamically get the position where we want to print our text based on the image size and the font properties.
我们还学会了如何根据图像大小和字体属性,动态地获得我们要打印的文本的位置。
At last, we’ve seen how to adapt the font size of the text in case it exceeds the size of the image where we are drawing it.
最后,我们已经看到了如何在文本的字体大小超过我们正在绘制的图像的大小时调整它。
As always, the full source code of the article is available over on GitHub.
一如既往,文章的完整源代码可在GitHub上获得over。