简体   繁体   中英

append text and picture to an image

I'd like ask a question about image processing in Java.

What I'm looking to achieve is to put a text below an existing image. Not over the existing image, but to append to image bottom. I have googled for that, but all the answers were the suggestions how to do the latter - how to put text on image.

Below is the example picture what result I wish to have. The original picture is the Leena image and the result by image processing is together with below the red dotted lines.

想要的结果

What could be fastest way to achieve this - write some text below image and also append another smaller picture?

What I thought about doing is that I append to my exisitng BufferedImage bottom 30 pixels height of white background and then write to that place a text and add a logo. Could this be most efficient approach? Are there some libs that would do that work for me or is there better approach than i mentioned?

EDIT: For better clarity I want the result as a single picture

I would take some time to have a read through

This is a really basic example. I've not bothered with adding the code to actually render a logo, you can read through the suggested reading list and should be able to figure out how that would be achieved ;)

在此处输入图片说明

public class AppendImage {

    public static void main(String[] args) {
        new AppendImage();
    }

    public AppendImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ImagePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ImagePane extends JPanel {

        private BufferedImage sourceImage;
        private BufferedImage labeledImage;

        public ImagePane() {

            try {
                sourceImage = ImageIO.read(getClass().getResource("/source.jpg"));
                labeledImage = addLabel("Hello", sourceImage);
            } catch (IOException exp) {
                exp.printStackTrace();
            }

        }

        @Override
        public Dimension getPreferredSize() {
            int width = sourceImage.getWidth() + labeledImage.getWidth();
            int height = labeledImage.getHeight();

            return new Dimension(width, height);
        }

        protected BufferedImage addLabel(String label, BufferedImage sourceImage) {

            Font font = UIManager.getFont("Label.font");
            FontMetrics fm = getFontMetrics(font);
            int width = sourceImage.getWidth() + 2;
            int height = sourceImage.getHeight() + 4 + fm.getHeight() + 4;

            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = image.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, width, height);
            g2d.setColor(Color.BLACK);
            g2d.setStroke(new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
            g2d.drawRect(0, 0, width - 1, height - 1);
            g2d.drawImage(sourceImage, 1, 1, this);
            g2d.setColor(Color.RED);
            g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10f, new float[]{10f}, 0f));
            g2d.drawLine(0, sourceImage.getHeight() + 2, image.getWidth(), sourceImage.getHeight() + 2);

            int y = sourceImage.getHeight() + (((image.getHeight() - sourceImage.getHeight()) - fm.getHeight()) / 2) + 2;
            g2d.drawString(label, 2, y + fm.getAscent());

            String logo = "Logo here";
            int x = image.getWidth() - fm.stringWidth(logo) - 12;
            g2d.drawString(logo, x, y + fm.getAscent());

            g2d.dispose();

            return image;

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = 0;
            g.drawImage(sourceImage, x, 0, this);
            x += sourceImage.getWidth();
            g.drawImage(labeledImage, x, 0, this);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM