You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1.1 KiB
30 lines
1.1 KiB
1 year ago
|
package UnitTest;
|
||
|
|
||
|
import javax.imageio.ImageIO;
|
||
|
import java.awt.*;
|
||
|
import java.awt.image.BufferedImage;
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
|
||
|
public class WriteCharactorToPng {
|
||
|
public static void main(String[] args) throws IOException {
|
||
|
String pngPath = "D:\\dsWork\\QingLong\\src\\main\\java\\UnitTest\\Image\\Rectangle.png";
|
||
|
BufferedImage image = ImageIO.read(new File(pngPath));
|
||
|
Graphics2D g = image.createGraphics();
|
||
|
g.setColor(Color.BLACK);
|
||
|
g.setFont(new Font("黑体", Font.PLAIN, 28));
|
||
|
|
||
|
String text = "三角形";
|
||
|
FontMetrics fm = g.getFontMetrics();
|
||
|
int x = (image.getWidth() - fm.stringWidth(text)) / 2;
|
||
|
int y = (image.getHeight() - fm.getHeight()) / 2 + fm.getAscent();
|
||
|
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||
|
g.drawString(text, x, y);
|
||
|
g.dispose();
|
||
|
|
||
|
ImageIO.write(image, "PNG", new File("c:\\output.png"));
|
||
|
}
|
||
|
}
|