java开发给cad加水印_java实现给图片添加水印

1 packagemichael.io.image;2

3 importjava.awt.AlphaComposite;4 importjava.awt.Graphics2D;5 importjava.awt.Image;6 importjava.awt.RenderingHints;7 importjava.awt.image.BufferedImage;8 importjava.io.File;9 importjava.io.FileInputStream;10 importjava.io.FileOutputStream;11 importjava.io.InputStream;12 importjava.io.OutputStream;13

14 importjavax.imageio.ImageIO;15 importjavax.swing.ImageIcon;16

17 importcom.sun.image.codec.jpeg.JPEGCodec;18 importcom.sun.image.codec.jpeg.JPEGImageDecoder;19 importcom.sun.image.codec.jpeg.JPEGImageEncoder;20

21 /**

22 * 图片水印23 * @bloghttp://sjsky.iteye.com

24 *@authorMichael25 */

26 public classImageMarkLogoByIcon {27

28 /**

29 *@paramargs30 */

31 public static voidmain(String[] args) {32 String srcImgPath = "d:/test/michael/myblog_01.png";33 String iconPath = "d:/test/michael/blog_logo.png";34 String targerPath = "d:/test/michael/img_mark_icon.jpg";35 String targerPath2 = "d:/test/michael/img_mark_icon_rotate.jpg";36 //给图片添加水印

37 ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath);38 //给图片添加水印,水印旋转-45

39 ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath2,40 -45);41

42 }43

44 /**

45 * 给图片添加水印46 *@paramiconPath 水印图片路径47 *@paramsrcImgPath 源图片路径48 *@paramtargerPath 目标图片路径49 */

50 public static voidmarkImageByIcon(String iconPath, String srcImgPath,51 String targerPath) {52 markImageByIcon(iconPath, srcImgPath, targerPath, null);53 }54

55 /**

56 * 给图片添加水印、可设置水印图片旋转角度57 *@paramiconPath 水印图片路径58 *@paramsrcImgPath 源图片路径59 *@paramtargerPath 目标图片路径60 *@paramdegree 水印图片旋转角度61 */

62 public static voidmarkImageByIcon(String iconPath, String srcImgPath,63 String targerPath, Integer degree) {64 OutputStream os = null;65 try{66 Image srcImg = ImageIO.read(newFile(srcImgPath));67

68 BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),69 srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);70

71 //得到画笔对象72 //Graphics g= buffImg.getGraphics();

73 Graphics2D g =buffImg.createGraphics();74

75 //设置对线段的锯齿状边缘处理

76 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,77 RenderingHints.VALUE_INTERPOLATION_BILINEAR);78

79 g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg80 .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);81

82 if (null !=degree) {83 //设置水印旋转

84 g.rotate(Math.toRadians(degree),85 (double) buffImg.getWidth() / 2, (double) buffImg86 .getHeight() / 2);87 }88

89 //水印图象的路径 水印一般为gif或者png的,这样可设置透明度

90 ImageIcon imgIcon = newImageIcon(iconPath);91

92 //得到Image对象。

93 Image img =imgIcon.getImage();94

95 float alpha = 0.5f; //透明度

96 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,97 alpha));98

99 //表示水印图片的位置

100 g.drawImage(img, 150, 300, null);101

102 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));103

104 g.dispose();105

106 os = newFileOutputStream(targerPath);107

108 //生成图片

109 ImageIO.write(buffImg, "JPG", os);110

111 System.out.println("图片完成添加Icon印章。。。。。。");112 } catch(Exception e) {113 e.printStackTrace();114 } finally{115 try{116 if (null !=os)117 os.close();118 } catch(Exception e) {119 e.printStackTrace();120 }121 }122 }123 }

图片添加水印Java实现需要用到Java图像处理库,常用的有Java Advanced Imaging (JAI)和ImageIO。下面提供一种使用ImageIO的实现方式: ```java import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class Watermark { public static void main(String[] args) throws Exception { String inputImagePath = "input.jpg"; String outputImagePath = "output.jpg"; String watermarkText = "confidential"; File inputFile = new File(inputImagePath); BufferedImage inputImage = ImageIO.read(inputFile); // 创建一个与原图像相同的新图像 BufferedImage outputImage = new BufferedImage( inputImage.getWidth(), inputImage.getHeight(), BufferedImage.TYPE_INT_RGB); // 将原图像复制到新图像中 Graphics2D g2d = outputImage.createGraphics(); g2d.drawImage(inputImage, 0, 0, null); // 添加水印 Font font = new Font("Arial", Font.BOLD, 48); g2d.setFont(font); g2d.setColor(Color.BLACK); AlphaComposite alphaComposite = AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.3f); // 设置水印透明度 g2d.setComposite(alphaComposite); FontMetrics fontMetrics = g2d.getFontMetrics(); int x = inputImage.getWidth() - fontMetrics.stringWidth(watermarkText) - 50; // 水印位置 int y = inputImage.getHeight() - fontMetrics.getHeight() - 50; g2d.drawString(watermarkText, x, y); g2d.dispose(); // 输出图像 File outputFile = new File(outputImagePath); ImageIO.write(outputImage, "jpg", outputFile); } } ``` 上述代码中,首先读取一个JPEG格式的输入图像,然后创建一个与原图像大小相同的新图像,将原图像复制到新图像中,再在新图像上添加水印,最后输出新图像。可以根据需要修改字体、水印文本、水印位置等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值