1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File;
public class ImageDrawer {
public static void main(String[] argvs) throws Exception { BufferedImage bg = ImageIO.read(ImageDrawer.class.getClassLoader().getResourceAsStream("bg.jpg"));
Graphics2D g = bg.createGraphics();
Font font = new Font("微软雅黑", Font.PLAIN, 50); g.setFont(font); g.setColor(Color.BLACK);
g.drawString("文字", 200, 150);
BufferedImage img = ImageIO.read(ImageDrawer.class.getClassLoader().getResourceAsStream("icon.jpg")); g.drawImage(img, 200, 200, 100, 100, null);
BufferedImage subImg = img.getSubimage(70, 70, 120, 120); g.drawImage(subImg, 20, 20, 60, 60, null);
ImageIO.write(bg, "jpeg", new File("target/newimg.jpg")); } }
|