`
firecool
  • 浏览: 81548 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

图片加水印JAVA实现

    博客分类:
  • java
阅读更多
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. }   
分享到:
评论
4 楼 lihua2008love 2011-09-26  
为什么报
javax.imageio.IIOException: Can't read input file!
	at javax.imageio.ImageIO.read(ImageIO.java:1275)
	at com.szair.test.ImageUtils.pressImage(ImageUtils.java:43)
	at com.szair.test.ImageUtils.main(ImageUtils.java:108)

不懂、图片路径是对的
3 楼 javay 2008-12-15  
谢谢。
2 楼 hailanc1108 2008-10-10  
不错,谢谢啦.
1 楼 elf8848 2008-10-02  
我看了, 很好, 但水印不是半透明的

相关推荐

Global site tag (gtag.js) - Google Analytics