使用Java添加图片水印和文字水印

来源:互联网 发布:破坏电力设备罪 知乎 编辑:程序博客网 时间:2024/06/06 08:31
 

方法一:

 


  1. import java.awt.*;  
  2. import java.awt.image.*;  
  3. import java.io.*;  
  4. import javax.swing.*;  
  5. import com.sun.image.codec.jpeg.*;  
  6. public class WaterSet {  
  7.     /** *//** 
  8.      * 给图片添加水印 
  9.      *  
  10.      * @param filePath 
  11.      *            需要添加水印的图片的路径 
  12.      * @param markContent 
  13.      *            水印的文字 
  14.      * @param markContentColor 
  15.      *            水印文字的颜色 
  16.      * @param qualNum 
  17.      *            图片质量 
  18.      * @return 
  19.      */  
  20.     public boolean createMark(String filePath, String markContent,  
  21.             Color markContentColor, float qualNum) {  
  22.         ImageIcon imgIcon = new ImageIcon(filePath);  
  23.         Image theImg = imgIcon.getImage();  
  24.         int width = theImg.getWidth(null);  
  25.         int height = theImg.getHeight(null);  
  26.         BufferedImage bimage = new BufferedImage(width, height,  
  27.                 BufferedImage.TYPE_INT_RGB);  
  28.         Graphics2D g = bimage.createGraphics();  
  29.         g.setColor(markContentColor);  
  30.         g.setBackground(Color.white);  
  31.         g.drawImage(theImg, 00null);  
  32.         g.drawString(markContent, width / 5, height / 5); // 添加水印的文字和设置水印文字出现的内容  
  33.         g.dispose();  
  34.         try {  
  35.             FileOutputStream out = new FileOutputStream(filePath);  
  36.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  37.             JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);  
  38.             param.setQuality(qualNum, true);  
  39.             encoder.encode(bimage, param);  
  40.             out.close();  
  41.         } catch (Exception e) {  
  42.             return false;  
  43.         }  
  44.         return true;  
  45.     }  
  46. }  

方法二:添加图片水印和文字水印两种,水印图片可以是GIF,PNG透明的文件,我一般采用的是PNG的,因为它的质量和GIF相比要高一些。

view plaincopy to clipboardprint?
  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. import javax.imageio.ImageIO;  
  9. import com.sun.image.codec.jpeg.JPEGCodec;  
  10. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  11. public final class ImageUtils {  
  12.     public ImageUtils() {  
  13.     }  
  14.     /**//* 
  15.      * public final static String getPressImgPath() { return ApplicationContext 
  16.      * .getRealPath("/template/data/util/shuiyin.gif"); } 
  17.      */  
  18.     /** *//** 
  19.      * 把图片印刷到图片上 
  20.      *  
  21.      * @param pressImg -- 
  22.      *            水印文件 
  23.      * @param targetImg -- 
  24.      *            目标文件 
  25.      * @param x 
  26.      *            --x坐标 
  27.      * @param y 
  28.      *            --y坐标 
  29.      */  
  30.     public final static void pressImage(String pressImg, String targetImg,  
  31.             int x, int y) {  
  32.         try {  
  33.             //目标文件  
  34.             File _file = new File(targetImg);  
  35.             Image src = ImageIO.read(_file);  
  36.             int wideth = src.getWidth(null);  
  37.             int height = src.getHeight(null);  
  38.             BufferedImage image = new BufferedImage(wideth, height,  
  39.                     BufferedImage.TYPE_INT_RGB);  
  40.             Graphics g = image.createGraphics();  
  41.             g.drawImage(src, 00, wideth, height, null);  
  42.             //水印文件  
  43.             File _filebiao = new File(pressImg);  
  44.             Image src_biao = ImageIO.read(_filebiao);  
  45.             int wideth_biao = src_biao.getWidth(null);  
  46.             int height_biao = src_biao.getHeight(null);  
  47.             g.drawImage(src_biao, (wideth - wideth_biao) / 2,  
  48.                     (height - height_biao) / 2, wideth_biao, height_biao, null);  
  49.             //水印文件结束  
  50.             g.dispose();  
  51.             FileOutputStream out = new FileOutputStream(targetImg);  
  52.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  53.             encoder.encode(image);  
  54.             out.close();  
  55.         } catch (Exception e) {  
  56.             e.printStackTrace();  
  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.     public static void pressText(String pressText, String targetImg,  
  79.             String fontName, int fontStyle, int color, int fontSize, int x,  
  80.             int y) {  
  81.         try {  
  82.             File _file = new File(targetImg);  
  83.             Image src = ImageIO.read(_file);  
  84.             int wideth = src.getWidth(null);  
  85.             int height = src.getHeight(null);  
  86.             BufferedImage image = new BufferedImage(wideth, height,  
  87.                     BufferedImage.TYPE_INT_RGB);  
  88.             Graphics g = image.createGraphics();  
  89.             g.drawImage(src, 00, wideth, height, null);  
  90.             // String s="www.qhd.com.cn";  
  91.             g.setColor(Color.RED);  
  92.             g.setFont(new Font(fontName, fontStyle, fontSize));  
  93.             g.drawString(pressText, wideth - fontSize - x, height - fontSize  
  94.                     / 2 - y);  
  95.             g.dispose();  
  96.             FileOutputStream out = new FileOutputStream(targetImg);  
  97.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  98.             encoder.encode(image);  
  99.             out.close();  
  100.         } catch (Exception e) {  
  101.             System.out.println(e);  
  102.         }  
  103.     }  
  104.     public static void main(String[] args) {  
  105.         pressImage("F:/logo.png",          "F:/123.jpg"00);  
  106.     }  
  107. }  

转自Java进行时【http://www.blogjava.net/davma/archive/2007/11/29/163969.html】

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小米5s手机黑屏打不开怎么办 小米5s黑屏只能开关机怎么办 手机拨号键没了怎么办 华为手机桌面拨号图标不见了怎么办 华为手机拨号图标不见了怎么办 小米4s屏幕显示黑屏怎么办 平板拨号键没了怎么办 华为手机拨号图标没了怎么办 华为荣耀手机进水了怎么办 华为畅享7黑屏怎么办 华为畅玩7x黑屏怎么办 华为手机打电话时黑屏怎么办 三星a8手机黑屏打不开怎么办 华为手机恢复出厂后黑屏怎么办 华为荣耀4c白屏怎么办 华为荣耀6手机信号不好怎么办 华为荣耀8手机音量小怎么办 无法激活触控id怎么办 魅蓝2卡顿怎么办 小米max2玩王者荣耀卡怎么办 小米4玩王者荣耀卡怎么办 华为荣耀7i卡顿怎么办 华为荣耀7卡的怎么办 荣耀8手机有孤独怎么办 红米note4玩游戏卡怎么办 红米note4x玩游戏卡怎么办 华为4c死屏怎么办 华为4c充电很慢怎么办? 华为4c突然死机了怎么办 华为畅玩4c内存不足怎么办 荣耀4c一直亮屏怎么办 华为手机返回键失灵怎么办 荣耀6p死机了怎么办 荣耀6主板烧坏了怎么办 虚拟运营商倒闭了号怎么办 买到二次放号怎么办 新运动鞋鞋穿着有点紧怎么办 一件代发被买家退货后怎么办? 洗了翻毛的鞋子怎么办 猫眼竹芋泡根了怎么办 双线花叶子卷了怎么办