Expand my Community achievements bar.

SOLVED

Adding Text to the Image using Java OSGi Service

Avatar

Employee

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!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Community Advisor

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

Avatar

Employee

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.

Avatar

Correct answer by
Community Advisor

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