1

I am trying to write a string into a image using ImageIo. But while writing a large string ,full string is not written into that image.

Here's my code:

File url=new File(imgUrl);

BufferedImage image = ImageIO.read(url);

Graphics g = image.getGraphics();
g.setPaintMode();
g.setFont(g.getFont().deriveFont(30f));
g.drawString(text, 100, 100);
g.dispose();

This code works fine for small strings.but when the width of the string exceeds the width of the image,then full string is not displayed on that image.

Any suggestions?

2
  • 3
    "this code works fine for small strings.but when the width of the string exceeds the width of the image,then full string is not displayed on that image" - that's obvious. What is your question? What's your expected behavior? Commented Feb 4, 2013 at 11:49
  • 2
    Split string and write it on multiple lines. Commented Feb 4, 2013 at 11:50

4 Answers 4

1

i have an old method try it

public BufferedImage stringToImage(String text, Font font, Color bgColor, Color fgColor) {
    BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    FontRenderContext fc = g2d.getFontRenderContext();
    Rectangle2D bounds = font.getStringBounds(text, fc);

    //calculate the size of the text
    int width = (int) bounds.getWidth();
    int height = (int) bounds.getHeight();

    image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    g2d = (Graphics2D) image.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setFont(font);

    g2d.setColor(bgColor);
    g2d.fillRect(0, 0, width, height);
    g2d.setColor(fgColor);
    g2d.drawString(text, 0, (int)-bounds.getY());
    g2d.dispose();

    return image;
}

and use

BufferedImage image = stringToImage(text, font, bgColor, fgColor);
ImageIO.write(image, "jpg", file);
Sign up to request clarification or add additional context in comments.

Comments

1

Not tested, but it could be done as this:

JLabel label = new JLabel("<html><h2>Title</h2><p>large text ...</p>");
int w = image.getWidth();
int h = image.getHeigth();
label.setBounds(0, 0, w, h);
SwingUtilities.paintComponent(g, label, null, 0, 0, w, h);

Comments

0

There are many ways to acheive this.

  • FontRenderContext/GlyphVector as mentioned by pbaris. See this answer for an e.g.
  • FontMetrics as seen in this answer.
  • A JLabel (possibly multi-line) to contain and size the text. As mentioned by Joop E.G. LabelRenderTest

Comments

0

You can use JTextArea to layout text:

JTextArea textArea = new JTextArea(text);
textArea.setFont(g.getFont().deriveFont(30f));
textArea.setOpaque(false);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setBounds(0, 0, image.getWidth(), image.getHeight());
textArea.paint(g);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.