Java 生成图片 绘制图片 绘制文字
程序员文章站
2024-03-04 12:43:11
...
项目源码:https://github.com/yicaifenchen8/Java_GraphicsImage.git
核心代码
public class Main {
public static void main(String[] args) {
drawImage();
}
public static void drawImage(){
try {
BufferedImage buffImg = ImageIO.read(new File("in.jpg"));
Graphics2D g = (Graphics2D) buffImg.getGraphics();
//设置抗锯齿,防止图片或文字模糊
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//绘制图片
ImageIcon imgIcon = new ImageIcon("in.jpg");
Image img = imgIcon.getImage();
g.drawImage(img,50,50,null);
//绘制文字
Font f = new Font("宋体",Font.PLAIN,30);
g.setColor(Color.RED);
g.setFont(f);
g.drawString("我是渣渣辉",10,100);
ImageIO.write(buffImg,"png", new File("out.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
效果如下
项目源码:https://github.com/yicaifenchen8/Java_GraphicsImage.git