Adding Text to the Image using Java OSGi Service | Community
Skip to main content
ayushmishra07
Adobe Employee
Adobe Employee
September 27, 2023
Solved

Adding Text to the Image using Java OSGi Service

  • September 27, 2023
  • 1 reply
  • 1171 views

I'm generating images at runtime using the zxing library and need to append text at the bottom of each image within an OSGi service environment. I attempted to leverage the java.awt packages, but encountered a Null Exception with the drawString() method.

Has anyone successfully integrated text onto images using Java in an OSGi service? Any insights or guidance would be greatly appreciated!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by B_Sravan

Unless any API provides these fonts, we can't directly refer them.
You might need to create the font files(ttf or otf) with the source in your repo.

File customFontFile = new File("custom-font.ttf path in your repo"); Font customFont = Font.createFont(Font.TRUETYPE_FONT, customFontFile);

 and then you can derive it may be something like this:

g2d.setFont(customFont.deriveFont(Font.PLAIN, 24)); // Adjust font style and size

1 reply

B_Sravan
Community Advisor
Community Advisor
September 27, 2023

Hi @ayushmishra07 ,

Maybe you can try something like this.
Interface:

package com.example.image.service; import java.awt.image.BufferedImage; public interface ImageTextService { BufferedImage addTextToImage(BufferedImage image, String text); }

 

Implementation:

package com.example.image.service.impl; import com.example.image.service.ImageTextService; import javax.media.jai.JAI; import javax.media.jai.RenderedOp; import java.awt.*; import java.awt.image.BufferedImage; public class ImageTextServiceImpl implements ImageTextService { @Override public BufferedImage addTextToImage(BufferedImage image, String text) { RenderedOp renderedImage = JAI.create("awtimage", image); Graphics2D g2d = renderedImage.createGraphics(); g2d.setColor(Color.RED); g2d.setFont(new Font("Arial", Font.BOLD, 36)); g2d.drawString(text, 50, 50); // Adjust coordinates as needed g2d.dispose(); return renderedImage.getAsBufferedImage(); } }

you can register and consume this service in a component or servlet as per your requirement.
you may need this dependency:

<dependencies> <dependency> <groupId>javax.media</groupId> <artifactId>jai_core</artifactId> <version>1.1.3</version> <!-- Use the latest version --> </dependency> </dependencies>


I hope this helps.

Thanks,

Sravan

ayushmishra07
Adobe Employee
Adobe Employee
September 27, 2023

Hi @b_sravan , Thanks for sharing the solution,

Can you please also share how this font can be added on the server side. The issue in our case is 

g2d.setFont(new Font("Arial", Font.BOLD, 36))

Graphics2d is not able to find the font.

B_Sravan
Community Advisor
B_SravanCommunity AdvisorAccepted solution
Community Advisor
September 27, 2023

Unless any API provides these fonts, we can't directly refer them.
You might need to create the font files(ttf or otf) with the source in your repo.

File customFontFile = new File("custom-font.ttf path in your repo"); Font customFont = Font.createFont(Font.TRUETYPE_FONT, customFontFile);

 and then you can derive it may be something like this:

g2d.setFont(customFont.deriveFont(Font.PLAIN, 24)); // Adjust font style and size