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

24 篇文章 0 订阅

方法一:

[java]  view plain copy
  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相比要高一些。

[java]  view plain copy
  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. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值