I want to render a string using java in a a bufferedImage. the problem is when i use a small font (for example: 8pt) i have bad quality images and the image resolution is 72dpi. i want to have the best quality for images and if possible change the images resolution to 360dpi. Noted that i want to generate a database of a language words with differents fonts, font sizes and font styles. and i used the standard java API (Graphics2D, Font, RenderingHints, FontMetrics,...) to generate images. here is my code:
public static void main(String[] args) {
String text = "ⴰⴱⴱⵓⵥⵥⵍ";
String [] polices=polices();
for(int i=0;i<polices.length;i++){
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = img.createGraphics();
Font font = new Font(polices[i], Font.PLAIN, 8);
g2d.setFont(font);
FontMetrics fm = g2d.getFontMetrics();
int width = fm.stringWidth(text);
int height = fm.getHeight();
g2d.dispose();
img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
g2d = img.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setFont(font);
g2d.setBackground(Color.WHITE);
g2d.clearRect(0, 0, width, height);
fm = g2d.getFontMetrics();
g2d.setColor(Color.BLACK);
g2d.drawString(text, 0, fm.getAscent());
g2d.dispose();
try {
ImageIO.write(img, "png", new File("Text "+i+".png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I searched a lot for a solution vainly, if someone can help me i will be thankfull. A solution that uses any externe libraries is welcome too.