图片加水印JAVA实现

  1. import java.awt.Color;   
  2. import java.awt.Font;   
  3. import java.awt.Graphics;   
  4. import java.awt.Image;   
  5. import java.awt.image.BufferedImage;   
  6. import java.io.File;   
  7. import java.io.FileOutputStream;   
  8.   
  9. import javax.imageio.ImageIO;   
  10.   
  11. import com.sun.image.codec.jpeg.JPEGCodec;   
  12. import com.sun.image.codec.jpeg.JPEGImageEncoder;   
  13.   
  14. public final class ImageUtils {   
  15.     public ImageUtils() {   
  16.   
  17.     }   
  18.   
  19.     /**  
  20.      * 把图片印刷到图片上  
  21.      *   
  22.      * @param pressImg --  
  23.      *            水印文件  
  24.      * @param targetImg --  
  25.      *            目标文件  
  26.      * @param x  
  27.      * @param y  
  28.      */  
  29.     public final static void pressImage(String pressImg, String targetImg,   
  30.             int x, int y) {   
  31.         try {   
  32.             File _file = new File(targetImg);   
  33.             Image src = ImageIO.read(_file);   
  34.             int wideth = src.getWidth(null);   
  35.             int height = src.getHeight(null);   
  36.             BufferedImage image = new BufferedImage(wideth, height,   
  37.                     BufferedImage.TYPE_INT_RGB);   
  38.             Graphics g = image.createGraphics();   
  39.             g.drawImage(src, 00, wideth, height, null);   
  40.   
  41.             // 水印文件   
  42.             File _filebiao = new File(pressImg);   
  43.             Image src_biao = ImageIO.read(_filebiao);   
  44.             int wideth_biao = src_biao.getWidth(null);   
  45.             int height_biao = src_biao.getHeight(null);   
  46.             g.drawImage(src_biao, wideth - wideth_biao - x, height   
  47.                     - height_biao - y, wideth_biao, height_biao, null);   
  48.             // /   
  49.             g.dispose();   
  50.             FileOutputStream out = new FileOutputStream(targetImg);   
  51.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
  52.             encoder.encode(image);   
  53.             out.close();   
  54.         } catch (Exception e) {   
  55.             e.printStackTrace();   
  56.         }   
  57.     }   
  58.   
  59.     /**  
  60.      * 打印文字水印图片  
  61.      *   
  62.      * @param pressText  
  63.      *            --文字  
  64.      * @param targetImg --  
  65.      *            目标图片  
  66.      * @param fontName --  
  67.      *            字体名  
  68.      * @param fontStyle --  
  69.      *            字体样式  
  70.      * @param color --  
  71.      *            字体颜色  
  72.      * @param fontSize --  
  73.      *            字体大小  
  74.      * @param x --  
  75.      *            偏移量  
  76.      * @param y  
  77.      */  
  78.   
  79.     public static void pressText(String pressText, String targetImg,   
  80.             String fontName, int fontStyle, int color, int fontSize, int x,   
  81.             int y) {   
  82.         try {   
  83.             File _file = new File(targetImg);   
  84.             Image src = ImageIO.read(_file);   
  85.             int wideth = src.getWidth(null);   
  86.             int height = src.getHeight(null);   
  87.             BufferedImage image = new BufferedImage(wideth, height,   
  88.                     BufferedImage.TYPE_INT_RGB);   
  89.             Graphics g = image.createGraphics();   
  90.             g.drawImage(src, 00, wideth, height, null);   
  91.             // String s="www.qhd.com.cn";   
  92.             g.setColor(Color.RED);   
  93.             g.setFont(new Font(fontName, fontStyle, fontSize));   
  94.   
  95.             g.drawString(pressText, wideth - fontSize - x, height - fontSize   
  96.                     / 2 - y);   
  97.             g.dispose();   
  98.             FileOutputStream out = new FileOutputStream(targetImg);   
  99.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
  100.             encoder.encode(image);   
  101.             out.close();   
  102.         } catch (Exception e) {   
  103.             System.out.println(e);   
  104.         }   
  105.     }   
  106.   
  107.     public static void main(String[] args) {   
  108.         pressImage("d:/test/22.jpg""d:/test/11.jpg"2020);   
  109.     }   
  110. }   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴回答你的问题!以下是Java实现图片剪裁和水印的代码示例: 图片剪裁: ```java import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageCropExample { public static void main(String[] args) throws Exception { File input = new File("input.jpg"); BufferedImage image = ImageIO.read(input); // 定义剪裁区域 Rectangle rect = new Rectangle(10, 10, 100, 100); BufferedImage cropped = image.getSubimage(rect.x, rect.y, rect.width, rect.height); File output = new File("output.jpg"); ImageIO.write(cropped, "jpg", output); } } ``` 图片水印: ```java import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageWatermarkExample { public static void main(String[] args) throws Exception { File input = new File("input.jpg"); BufferedImage image = ImageIO.read(input); // 创建Graphics2D对象 Graphics2D g2d = image.createGraphics(); // 设置水印文字字体、颜色和透明度 g2d.setFont(new Font("Arial", Font.BOLD, 36)); g2d.setColor(Color.RED); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); // 创建AffineTransform对象,设置水印文字旋转角度 AffineTransform at = new AffineTransform(); at.rotate(Math.toRadians(30), image.getWidth() / 2, image.getHeight() / 2); g2d.setTransform(at); // 绘制水印文字 g2d.drawString("Watermark", image.getWidth() / 2, image.getHeight() / 2); // 释放Graphics2D对象 g2d.dispose(); File output = new File("output.jpg"); ImageIO.write(image, "jpg", output); } } ``` 希望能帮到你,如果还有其他问题,请随时提出!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值