Writing String into a image in java

别说谁变了你拦得住时间么 提交于 2019-12-11 08:43:54

问题


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?


回答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);



回答2:


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);



回答3:


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




回答4:


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);


来源:https://stackoverflow.com/questions/14686099/writing-string-into-a-image-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!