Image

   InputStream in =null;
try {
in = new FileInputStream("D:\\My Documents\\My Pictures\\logo.jpg");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//
BufferedImage bi =null;
try {
bi = ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Graphics g = bi.createGraphics();
g.setColor(Color.blue);
g.drawString("我爱你", 10, 100);
g.dispose();

File file = new File("C:\\1.JPG");
try {
ImageIO.write(bi, "JPEG", file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//JPEG或JPG
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



 
/**
* 文字水印
* @param pressText 水印文字
* @param targetImg 目标图片
* @param fontName 字体名称
* @param fontStyle 字体样式
* @param color 字体颜色
* @param fontSize 字体大小
* @param x 修正值
* @param y 修正值
* @param alpha 透明度
*/
public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {
try {
File img = new File(targetImg);
Image src = ImageIO.read(img);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
g.setColor(color);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);
g.dispose();
ImageIO.write((BufferedImage) image, "jpg", img);
} catch (Exception e) {
e.printStackTrace();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值