67,511
社区成员
发帖
与我相关
我的任务
分享
/**
* 把字符印到图片上
* @param imgPath 图片路径(物理路径)
* @param text 准备的字符
* @param font 字体样式
* @param color 字体颜色
* @param left 图片的x坐标 (印的位置)
* @param top 图片的y坐标 (印的位置)
*/
public void fontImg(String imgPath, String text, Font font, Color color, int left, int top) {
try {
BufferedImage src = ImageIO.read(new File(imgPath));
int width = src.getWidth();
int height = src.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics graph = image.createGraphics();
graph.drawImage(src, 0, 0, width, height, null);
graph.setColor(color);
graph.setFont(font);
graph.drawString(text, left, top);
graph.dispose();
FileOutputStream out = new FileOutputStream(imgPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
image.flush();
src.flush();
} catch (Exception e) {
e.printStackTrace();
}
}