Skip to content

Commit 24137c1

Browse files
authored
[BAEL-4240] Adding Text to an Image in Java (eugenp#9642)
* [BAEL-4240] Code Upload * [BAEL-4240] Remove double space * [BAEL-4240] Adapting Text to the image * [BAEL-4240] Package rename
1 parent 5a097e9 commit 24137c1

1 file changed

Lines changed: 220 additions & 0 deletions

File tree

  • image-processing/src/main/java/com/baeldung/imageprocessing/addingtext
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
package com.baeldung.imageprocessing.addingtext;
2+
3+
import ij.IJ;
4+
import ij.ImagePlus;
5+
import ij.process.ImageProcessor;
6+
7+
import java.awt.*;
8+
import java.awt.font.GlyphVector;
9+
import java.awt.font.TextAttribute;
10+
import java.awt.image.BufferedImage;
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.text.AttributedString;
14+
15+
import javax.imageio.ImageIO;
16+
17+
public class AddText {
18+
public static void main(String[] args) throws IOException {
19+
String imagePath = AddText.class.getClassLoader().getResource("lena.jpg").getPath();
20+
21+
ImagePlus resultPlus= signImageImageProcessor("www.baeldung.com", imagePath);
22+
resultPlus.show();
23+
24+
ImagePlus resultGraphics = new ImagePlus("", signImageGraphics("www.baeldung.com", imagePath));
25+
resultGraphics.show();
26+
27+
ImagePlus resultGraphicsWithIterator = new ImagePlus("", signImageGraphicsWithIterator("www.baeldung.com", imagePath));
28+
resultGraphicsWithIterator.show();
29+
30+
ImagePlus resultGraphicsCentered = new ImagePlus("", signImageCenter("www.baeldung.com", imagePath));
31+
resultGraphicsCentered.show();
32+
33+
ImagePlus resultGraphicsBottomRight = new ImagePlus("", signImageBottomRight("www.baeldung.com", imagePath));
34+
resultGraphicsBottomRight.show();
35+
36+
ImagePlus resultGraphicsTopLeft= new ImagePlus("", signImageTopLeft("www.baeldung.com", imagePath));
37+
resultGraphicsTopLeft.show();
38+
39+
ImagePlus resultGraphicsAdaptBasedOnImage= new ImagePlus("", signImageAdaptBasedOnImage("www.baeldung.com", imagePath));
40+
resultGraphicsAdaptBasedOnImage.show();
41+
}
42+
43+
private static ImagePlus signImageImageProcessor(String text, String path) {
44+
ImagePlus image = IJ.openImage(path);
45+
Font font = new Font("Arial", Font.BOLD, 18);
46+
47+
ImageProcessor ip = image.getProcessor();
48+
ip.setColor(Color.GREEN);
49+
ip.setFont(font);
50+
ip.drawString(text, 0, 20);
51+
52+
return image;
53+
}
54+
55+
private static BufferedImage signImageGraphics(String text, String path) throws IOException {
56+
BufferedImage image = ImageIO.read(new File(path));
57+
Font font = new Font("Arial", Font.BOLD, 18);
58+
59+
Graphics g = image.getGraphics();
60+
g.setFont(font);
61+
g.setColor(Color.GREEN);
62+
g.drawString(text, 0, 20);
63+
64+
return image;
65+
}
66+
67+
68+
private static BufferedImage signImageGraphicsWithIterator(String text, String path) throws IOException {
69+
BufferedImage image = ImageIO.read(new File(path));
70+
Font font = new Font("Arial", Font.BOLD, 18);
71+
72+
AttributedString attributedText = new AttributedString(text);
73+
attributedText.addAttribute(TextAttribute.FONT, font);
74+
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
75+
76+
Graphics g = image.getGraphics();
77+
g.drawString(attributedText.getIterator(), 0, 20);
78+
79+
return image;
80+
}
81+
82+
/**
83+
* Draw a String centered in the middle of a Rectangle.
84+
*
85+
* @param g The Graphics instance.
86+
* @param text The String to draw.
87+
* @param rect The Rectangle to center the text in.
88+
* @throws IOException
89+
*/
90+
public static BufferedImage signImageCenter(String text, String path) throws IOException {
91+
92+
BufferedImage image = ImageIO.read(new File(path));
93+
Font font = new Font("Arial", Font.BOLD, 18);
94+
95+
AttributedString attributedText = new AttributedString(text);
96+
attributedText.addAttribute(TextAttribute.FONT, font);
97+
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
98+
99+
Graphics g = image.getGraphics();
100+
101+
FontMetrics metrics = g.getFontMetrics(font);
102+
int positionX = (image.getWidth() - metrics.stringWidth(text)) / 2;
103+
int positionY = (image.getHeight() - metrics.getHeight()) / 2 + metrics.getAscent();
104+
105+
g.drawString(attributedText.getIterator(), positionX, positionY);
106+
107+
return image;
108+
}
109+
110+
/**
111+
* Draw a String centered in the middle of a Rectangle.
112+
*
113+
* @param g The Graphics instance.
114+
* @param text The String to draw.
115+
* @param rect The Rectangle to center the text in.
116+
* @throws IOException
117+
*/
118+
public static BufferedImage signImageBottomRight(String text, String path) throws IOException {
119+
120+
BufferedImage image = ImageIO.read(new File(path));
121+
122+
Font font = new Font("Arial", Font.BOLD, 18);
123+
124+
AttributedString attributedText = new AttributedString(text);
125+
attributedText.addAttribute(TextAttribute.FONT, font);
126+
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
127+
128+
Graphics g = image.getGraphics();
129+
130+
FontMetrics metrics = g.getFontMetrics(font);
131+
int positionX = (image.getWidth() - metrics.stringWidth(text));
132+
int positionY = (image.getHeight() - metrics.getHeight()) + metrics.getAscent();
133+
134+
g.drawString(attributedText.getIterator(), positionX, positionY);
135+
136+
return image;
137+
}
138+
139+
/**
140+
* Draw a String centered in the middle of a Rectangle.
141+
*
142+
* @param g The Graphics instance.
143+
* @param text The String to draw.
144+
* @param rect The Rectangle to center the text in.
145+
* @throws IOException
146+
*/
147+
public static BufferedImage signImageTopLeft(String text, String path) throws IOException {
148+
149+
BufferedImage image = ImageIO.read(new File(path));
150+
151+
Font font = new Font("Arial", Font.BOLD, 18);
152+
153+
AttributedString attributedText = new AttributedString(text);
154+
attributedText.addAttribute(TextAttribute.FONT, font);
155+
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
156+
157+
Graphics g = image.getGraphics();
158+
159+
FontMetrics metrics = g.getFontMetrics(font);
160+
int positionX = 0;
161+
int positionY = metrics.getAscent();
162+
163+
g.drawString(attributedText.getIterator(), positionX, positionY);
164+
165+
return image;
166+
}
167+
168+
/**
169+
* Draw a String centered in the middle of a Rectangle.
170+
*
171+
* @param g The Graphics instance.
172+
* @param text The String to draw.
173+
* @param rect The Rectangle to center the text in.
174+
* @throws IOException
175+
*/
176+
public static BufferedImage signImageAdaptBasedOnImage(String text, String path) throws IOException {
177+
178+
BufferedImage image = ImageIO.read(new File(path));
179+
180+
Font font = createFontToFit(new Font("Arial", Font.BOLD, 80), text, image);
181+
182+
AttributedString attributedText = new AttributedString(text);
183+
attributedText.addAttribute(TextAttribute.FONT, font);
184+
attributedText.addAttribute(TextAttribute.FOREGROUND, Color.GREEN);
185+
186+
Graphics g = image.getGraphics();
187+
188+
FontMetrics metrics = g.getFontMetrics(font);
189+
int positionX = (image.getWidth() - metrics.stringWidth(text));
190+
int positionY = (image.getHeight() - metrics.getHeight()) + metrics.getAscent();
191+
192+
g.drawString(attributedText.getIterator(), positionX, positionY);
193+
194+
return image;
195+
}
196+
197+
public static Font createFontToFit(Font baseFont, String text, BufferedImage image) throws IOException
198+
{
199+
Font newFont = baseFont;
200+
201+
FontMetrics ruler = image.getGraphics().getFontMetrics(baseFont);
202+
GlyphVector vector = baseFont.createGlyphVector(ruler.getFontRenderContext(), text);
203+
204+
Shape outline = vector.getOutline(0, 0);
205+
206+
double expectedWidth = outline.getBounds().getWidth();
207+
double expectedHeight = outline.getBounds().getHeight();
208+
209+
boolean textFits = image.getWidth() >= expectedWidth && image.getHeight() >= expectedHeight;
210+
211+
if(!textFits) {
212+
double widthBasedFontSize = (baseFont.getSize2D()*image.getWidth())/expectedWidth;
213+
double heightBasedFontSize = (baseFont.getSize2D()*image.getHeight())/expectedHeight;
214+
215+
double newFontSize = widthBasedFontSize < heightBasedFontSize ? widthBasedFontSize : heightBasedFontSize;
216+
newFont = baseFont.deriveFont(baseFont.getStyle(), (float)newFontSize);
217+
}
218+
return newFont;
219+
}
220+
}

0 commit comments

Comments
 (0)