java 图片加水印实例代码
时间:2021-01-10 11:03:12|栏目:JAVA代码|点击: 次
复制代码 代码如下:
try {
String targetImg = "D:/Blue hills.jpg";
// String pressImg = "D:/20130311220300.jpg";
String pressImg = "D:/html_original.jpg";
File f1 = new File(targetImg);
Image src = ImageIO.read(f1);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, width, height, null);
File f2 = new File(pressImg);
Image src_pao = ImageIO.read(f2);
int width_pao = src_pao.getWidth(null);
int height_pao = src_pao.getHeight(null);
g.drawImage(src_pao, (width-+width_pao)/2, (height-height_pao)/2,width_pao,height_pao, null);
// g.drawImage(src_pao, 100, 100,width,height, null);
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
System.out.println("水印添加成功!!");
} catch (Exception e) {
// TODO: handle exception
System.out.println("水印添加失败!!");
e.printStackTrace();
}
上一篇:Spring实战之ResourceLoader接口资源加载用法示例
栏 目:JAVA代码
下一篇:SpringBoot集成gRPC微服务工程搭建实践的方法
本文标题:java 图片加水印实例代码
本文地址:http://www.codeinn.net/misctech/43050.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机