Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Java g.drawString() high resolution?

I have background templates where java program must write some dynamic texts,

BufferedImage image = ImageIO.read(new File("background.jpg"));
Graphics g = image.createGraphics();

g.setFont(new Font("DejaVu Sans",Font.PLAIN,18));
g.drawString("Hello,World!",10,10);

When writing in such manner, I have some resolution problems around text that Java wrote.

How to write high resolution text on image by Java?

UPDATE: Here example with anti-aliasing. enter image description here

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
like image 732
Rinat Tainov Avatar asked Feb 01 '26 02:02

Rinat Tainov


1 Answers

The problem is not the drawing of the text, but the subsequent saving of the file: If you are using JPEG-compression, you will get compression artifacts, specially around sharp corners like your text.

There is no real way around this, apart from not using JPG.

like image 142
Paŭlo Ebermann Avatar answered Feb 02 '26 17:02

Paŭlo Ebermann