Utilizzando Graphics2D di sovrapporre testo su una BufferedImage e restituire un BufferedImage
-
27-09-2019 - |
Domanda
Ho controllato domande nome simile, ma non rispondere a questa caso d'uso.
Fondamentalmente, ero sovrapporre un testo (testo) in un dato coordinate (x, y) ho la sotto funzione in un pacchetto;
protected BufferedImage Process2(BufferedImage image){
Graphics2D gO = image.createGraphics();
gO.setColor(Color.red);
gO.setFont(new Font( "SansSerif", Font.BOLD, 12 ));
gO.drawString(this.text, this.x, this.y);
System.err.println(this.text+this.x+this.y);
return image;
}
mi sento come im manca qualcosa di ovvio; ogni riferimento a Graphics2D posso trovare a che fare sia con i giochi o scrivendo direttamente su un file, ma voglio solo un BufferedImage restituito. con l'overlay 'reso'
Nel codice corrente, l'immagine appare fuori alla fine invariata.
Grazie!
Soluzione
Il metodo usi drawString()
x e y per il carattere più a sinistra linea di base . I numeri in genere non hanno discendenti; se lo stesso vale per text
, una stringa disegnata nella posizione (0,0) sarà reso interamente all'esterno dell'immagine. Vedere questo esempio .
Addendum: Che possono avere problemi con un modello di colore non compatibile nella vostra immagine. Una semplice accorgimento è quello di rendere l'immagine e quindi modificarlo in situ .
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
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;
/**
* @see https://stackoverflow.com/questions/2658663
*/
public class TextOverlay extends JPanel {
private BufferedImage image;
public TextOverlay() {
try {
image = ImageIO.read(new URL(
"http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"));
} catch (IOException e) {
e.printStackTrace();
}
image = process(image);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(), image.getHeight());
}
private BufferedImage process(BufferedImage old) {
int w = old.getWidth() / 3;
int h = old.getHeight() / 3;
BufferedImage img = new BufferedImage(
w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, w, h, this);
g2d.setPaint(Color.red);
g2d.setFont(new Font("Serif", Font.BOLD, 20));
String s = "Hello, world!";
FontMetrics fm = g2d.getFontMetrics();
int x = img.getWidth() - fm.stringWidth(s) - 5;
int y = fm.getHeight();
g2d.drawString(s, x, y);
g2d.dispose();
return img;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
private static void create() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TextOverlay());
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
create();
}
});
}
}