1

PNG画像の下にタイトル文字列を追加しようとしています。問題は、テキスト用のスペースがなく、空白を追加する必要があることです。これを行う方法がわかりません。これは私がこれまでに行ったことです:

        BufferedImage image = ImageIO.read(new File("output.png"));
        Graphics g = image.getGraphics();
        g.setFont(g.getFont().deriveFont(30f));
        g.setColor(Color.BLACK);
        g.drawString("Hello World!", 100, 350);
        g.dispose();
        ImageIO.write(image, "png", new File("test.png"));
4

2 に答える 2

4

BufferedImage新しいs の高さに必要な間隔を単純に追加してみませんか?

これが私が作った例です:

ここに画像の説明を入力

本当の魔法はここで起こります:

private BufferedImage drawTextOnImage(String text, BufferedImage image, int space) {
    BufferedImage bi = new BufferedImage(image.getWidth(), image.getHeight() + space, BufferedImage.TRANSLUCENT);
    Graphics2D g2d = (Graphics2D) bi.createGraphics();
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));

    g2d.drawImage(image, 0, 0, null);

    g2d.setColor(Color.BLACK);
    g2d.setFont(new Font("Calibri", Font.BOLD, 20));
    FontMetrics fm = g2d.getFontMetrics();
    int textWidth = fm.stringWidth(text);

    //center text at bottom of image in the new space
    g2d.drawString(text, (bi.getWidth() / 2) - textWidth / 2, bi.getHeight());

    g2d.dispose();
    return bi;
}

上記のメソッドを使用すると、テキスト、画像への参照、および追加する間隔の量を渡すことができます。次に、指定されたスペース内の画像に文字列を描画します。drawTextOnImage(..)画像は、次の方法で上記のメソッド ( )に渡す前にサイズ変更されます。

public static BufferedImage resize(BufferedImage image, int width, int height) {
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
    Graphics2D g2d = (Graphics2D) bi.createGraphics();
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    g2d.drawImage(image, 0, 0, width, height, null);
    g2d.dispose();
    return bi;
}

テスト.java:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public Test() {
        createAndShowGui();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        BufferedImage bi = null;
        try {
            bi = ImageIO.read(new URL("http://cs.anu.edu.au/student/comp6700/icons/DukeWithHelmet.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        BufferedImage resizedImage = resize(bi, 200, 200);

        final BufferedImage textRenderedImage = drawTextOnImage("Hello", resizedImage, 15);

        JPanel p = new JPanel() {
            @Override
            protected void paintComponent(Graphics grphcs) {
                super.paintComponent(grphcs);
                Graphics2D g2d = (Graphics2D) grphcs;
                g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
                g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
                g2d.drawImage(textRenderedImage, 0, 0, null);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(textRenderedImage.getWidth(), textRenderedImage.getHeight());
            }
        };

        frame.add(p);
        frame.pack();
        frame.setVisible(true);
    }

    private BufferedImage drawTextOnImage(String text, BufferedImage image, int space) {
        BufferedImage bi = new BufferedImage(image.getWidth(), image.getHeight() + space, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));

        g2d.drawImage(image, 0, 0, null);

        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("Calibri", Font.BOLD, 20));
        FontMetrics fm = g2d.getFontMetrics();
        int textWidth = fm.stringWidth(text);

        //center text at bottom of image in the new space
        g2d.drawString(text, (bi.getWidth() / 2) - textWidth / 2, bi.getHeight());

        g2d.dispose();
        return bi;
    }

    public static BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(image, 0, 0, width, height, null);
        g2d.dispose();
        return bi;
    }
}
于 2013-01-14T17:22:11.907 に答える
2

BufferedImage の下部に空白を追加するには、新しい BufferedImage を作成して画像をコピーする必要があります。

3 行目の 30 は 30 ピクセルの余白です。必要な空白の量を取得するために、任意の数値に置き換えます。

        BufferedImage image = ImageIO.read(new File("output.png"));
        BufferedImage newImage = new BufferedImage(image.getWidth(), 
            image.getHeight() + 30, BufferedImage.TYPE_INT_ARGB);
        Graphics g1 = image.getGraphics();
        Graphics g2 = newImage.getGraphics();
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());
        g2.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
于 2013-01-14T17:11:58.760 に答える