简体   繁体   English

将文本和图片附加到图像

[英]append text and picture to an image

I'd like ask a question about image processing in Java.我想问一个关于 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.原图为Leena 图像,图像处理后的结果与下方红色虚线一起显示。

想要的结果

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.我想做的是将白色背景的 BufferedImage 底部 30 像素高度附加到我的现有 BufferedImage 上,然后在该位置写入文本并添加徽标。 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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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