parent
f062e76b99
commit
ebfaf1f9ae
After Width: | Height: | Size: 2.3 KiB |
@ -0,0 +1,29 @@
|
||||
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"));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue