0

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.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328

1 Answers1

0

An 8pt font at 72dpi is 8/72*360 = size 40 font at 360dpi - so set the font size to 40.

As to how to set the DPI for an image, see the question How to set DPI information in an image?

Community
  • 1
  • 1
Joni
  • 108,737
  • 14
  • 143
  • 193
  • A point is defined as exactly 1/72 of an inch, not as a pixel. 8pt font should render the same physical size whether the image is 72dpi or 360dpi. – RealSkeptic May 15 '16 at 12:46
  • @RealSkeptic yes, an 8pt font should be 8 points - unfortunately Java's `BufferedImage` doesn't support setting the dpi directly, and the easiest solution is to pretend that font sizes are set in pixels – Joni May 15 '16 at 12:51
  • I think you should make that clearer in the answer, perhaps mention that the result of your calculation is pixels and then you use it as points and why. – RealSkeptic May 15 '16 at 12:57
  • thank u for replies, i can use images with 72dpi (it's not a problem) but in this resolution and with a small font (8pt) i have a very bad text rendering and the text is unreadable while in MS WORD the text is clear and readable with the same size 8pt. – Nabil Aharrane May 15 '16 at 15:35
  • The Java standard library does not use the same algorithms to render fonts as Microsoft Word, it just is not good at rendering small fonts in low resolutions. Increase the resolution. – Joni May 15 '16 at 23:04
  • what do you mean by increase the resolution? do i need to change the image dpi or what? – Nabil Aharrane May 16 '16 at 23:00
  • Yes, create a bigger image and set the DPI when you write it to a file – Joni May 22 '16 at 14:38