package suiyin;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Wimg {
public static void main(String[] args) {
File file = new File("C:\\Users\\w1663\\Pictures\\Camera Roll\\img.png");
String file1 = "C:\\Users\\w1663\\Pictures\\Camera Roll\\img1.png";
String st = "你好中国";
var color = new Color(112, 211, 11, 180);
wimg(file, file1, st, color);
}
public static void wimg(File file, String file1, String st, Color color) {
try {
Image t = ImageIO.read(file);
int w = t.getWidth(null);
int h = t.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.drawImage(t, 0, 0, w, h, null);
Font font = new Font("宋体", Font.PLAIN, 40);
graphics2D.setColor(color);
graphics2D.setFont(font);
var fm = graphics2D.getFontMetrics(font.deriveFont(40f));
int x = w - fm.stringWidth(st) - 10;
int y = h - 20;
graphics2D.drawString(st, x, y);
graphics2D.dispose();
FileOutputStream out = new FileOutputStream(file1);
ImageIO.write(bufferedImage, "png", out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}