Java- Write Text onto Image, then Write to Output File

前端 未结 3 1440
萌比男神i
萌比男神i 2021-01-13 01:33

I have an image on top of which I would like to write text that has multiple lines, is center-aligned, and dynamic (variable width). I\'ve tried using the drawString

相关标签:
3条回答
  • Use a JLabel to display the image and text:

    JLabel picture = new JLabel( new ImageIcon(...) );
    picture.setText("<html><center>Text<br>over<br>Image<center></html>");
    picture.setHorizontalTextPosition(JLabel.CENTER);
    picture.setVerticalTextPosition(JLabel.CENTER);
    

    Then you can use the Screen Image class to create an image of any component.

    Edit:

    Missed your update that you don't want the text centered on the image. For your new requirement you can add other components to the label to do the formatting for you. The code below shows the original suggestion as will as some examples of using components:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class LabelImageText extends JPanel
    {
        public LabelImageText()
        {
            JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) );
            label1.setText( "Easy Way" );
            label1.setHorizontalTextPosition(JLabel.CENTER);
            label1.setVerticalTextPosition(JLabel.CENTER);
            add( label1 );
    
            //
    
            JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) );
            label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) );
            add( label2 );
    
            JLabel text = new JLabel( "More Control" );
            text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            label2.add( Box.createVerticalGlue() );
            label2.add( text );
            label2.add( Box.createVerticalStrut(10) );
    
            //
    
            JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) );
            add( label3 );
    
            JLabel text3 = new JLabel();
            text3.setText("<html><center>Text<br>over<br>Image<center></html>");
            text3.setLocation(20, 20);
            text3.setSize(text3.getPreferredSize());
            label3.add( text3 );
    
            //
    
            JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) );
            add( label4 );
    
            JTextPane textPane = new JTextPane();
            textPane.setText("Add some text that will wrap at your preferred width");
            textPane.setEditable( false );
            textPane.setOpaque(false);
            SimpleAttributeSet center = new SimpleAttributeSet();
            StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
            StyledDocument doc = textPane.getStyledDocument();
            doc.setParagraphAttributes(0, doc.getLength(), center, false);
            textPane.setBounds(20, 20, 75, 100);
            label4.add( textPane );
        }
    
        public static class ColorIcon implements Icon
        {
            private Color color;
            private int width;
            private int height;
    
            public ColorIcon(Color color, int width, int height)
            {
                this.color = color;
                this.width = width;
                this.height = height;
            }
    
            public int getIconWidth()
            {
                return width;
            }
    
            public int getIconHeight()
            {
                return height;
            }
    
            public void paintIcon(Component c, Graphics g, int x, int y)
            {
                g.setColor(color);
                g.fillRect(x, y, width, height);
            }
        }
    
        private static void createAndShowUI()
        {
            JFrame frame = new JFrame("LabelImageText");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add( new LabelImageText() );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2021-01-13 02:06

    You should use a drawString().

    To properly center a text you need to calculate the width for a given font:

    Font font = getFont();
    FontMetrics metrics = getFontMetrics( font );
    int width = metrics.stringWidth( theString );
    

    And remember to add -Djava.awt.headless=true if you want to call in on server (without GUI).

    0 讨论(0)
  • 2021-01-13 02:11

    You don't really need swing at all if you just want to generate image files.

    You can do something like this:

    import java.awt.image.BufferedImage;
    import java.awt.Graphics2D;
    import java.io.File;
    import javax.imageio.ImageIO;
    import java.io.IOException;
    
    BufferedImage img = ImageIO.read(new File("dog.jpg")); // try/catch IOException
    int width = img.getWidth();
    int height = img.getHeight();
    
    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bufferedImage.createGraphics();
    
    // draw graphics
    g2d.drawImage(img, 0, 0, null);
    g2d.drawString(text, x, y);
    
    g2d.dispose();
    
    try {
    
    // Save as PNG
    File file = new File("newimage.png");
    ImageIO.write(bufferedImage, "png", file);
    
    // Save as JPEG
    file = new File("newimage.jpg");
    ImageIO.write(bufferedImage, "jpg", file);
    
    } catch (IOException e) { }
    

    For more info, see:

    http://www.exampledepot.com/egs/javax.imageio/Graphic2File.html

    The text alignment and centering can be done using the FontMetrics class.

    0 讨论(0)
提交回复
热议问题