Java实现 图片水印或者文字水印

import java.awt.AlphaComposite;  
import java.awt.Color;  
import java.awt.Font;  
import java.awt.Graphics2D;  
import java.awt.Image;  
import java.awt.RenderingHints;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.OutputStream;  
import javax.imageio.ImageIO;  
  
/** 
 * 生成文字水印 
 * @author Ricky Fung 
 * 
 */  
public class WaterMarkTextTest {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
          
        File srcImgFile = new File("D:/test/myblog.png");  
        String logoText = "[ 水印文字 电话邦   ]";  
          
        File outputImageFile = new File("D:/test/myblog_water_text.jpg");  
          
        File outputRotateImageFile = new File("D:/test/myblog_water_text_rotate.jpg");  
  
        createWaterMarkByText(srcImgFile, logoText, outputImageFile);  
  
        createWaterMarkByText(srcImgFile, logoText, outputRotateImageFile, 45);  
    }  
  
      
    public static void createWaterMarkByText(File srcImgFile, String logoText,  
            File outputImageFile) {  
        createWaterMarkByText(srcImgFile, logoText, outputImageFile, 0);  
    }  
      
    public static void createWaterMarkByText(File srcImgFile, String logoText,  
            File outputImageFile, double degree) {  
          
        OutputStream os = null;  
        try {  
            Image srcImg = ImageIO.read(srcImgFile);  
  
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),  
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);  
  
            Graphics2D g = buffImg.createGraphics();  
  
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,  
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
  
            g.drawImage(  
                    srcImg.getScaledInstance(srcImg.getWidth(null),  
                            srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,  
                    null);  
  
            if (degree>0) {  
                g.rotate(Math.toRadians(degree),  
                        (double) buffImg.getWidth() / 2,  
                        (double) buffImg.getHeight() / 2);  
            }  
  
            g.setColor(Color.RED);  
  
            g.setFont(new Font("宋体", Font.BOLD, 36));  
  
            float alpha = 0.8f;  
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,  
                    alpha));  
  
            g.drawString(logoText, buffImg.getWidth()/3, buffImg.getHeight()/2);  
  
            g.dispose();  
  
            os = new FileOutputStream(outputImageFile);  
  
            // 生成图片  
            ImageIO.write(buffImg, "JPG", os);  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (null != os)  
                    os.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  
  
}  import java.awt.AlphaComposite;  
import java.awt.Graphics2D;  
import java.awt.Image;  
import java.awt.RenderingHints;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.OutputStream;  
import javax.imageio.ImageIO;  
import javax.swing.ImageIcon;  
  
/** 
 * 生成图片水印 
 * @author Ricky Fung 
 * 
 */  
public class WaterMarkIconTest {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
          
        File srcImageFile = new File("D:/test/myblog.png");  
        File logoImageFile = new File("D:/test/logo.png");  
          
        File outputImageFile = new File("D:/test/myblog_water_icon.jpg");  
        File outputRoateImageFile = new File("D:/test/myblog_water_icon_rotate.jpg");  
          
        createWaterMarkByIcon(srcImageFile, logoImageFile, outputImageFile);  
          
        createWaterMarkByIcon(srcImageFile, logoImageFile, outputRoateImageFile, 45);  
    }  
  
    public static void createWaterMarkByIcon(File srcImageFile, File logoImageFile,  
            File outputImageFile) {  
        createWaterMarkByIcon(srcImageFile, logoImageFile, outputImageFile, 0);  
    }  
  
    public static void createWaterMarkByIcon(File srcImageFile, File logoImageFile,  
            File outputImageFile, double degree) {  
          
        OutputStream os = null;  
        try {  
            Image srcImg = ImageIO.read(srcImageFile);  
  
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),  
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);  
  
            Graphics2D g = buffImg.createGraphics();  
  
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,  
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
  
            g.drawImage(  
                    srcImg.getScaledInstance(srcImg.getWidth(null),  
                            srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,  
                    null);  
  
            ImageIcon logoImgIcon = new ImageIcon(ImageIO.read(logoImageFile));  
  
            Image logoImg = logoImgIcon.getImage();  
  
            //旋转  
            if (degree>0) {  
                g.rotate(Math.toRadians(degree),  
                        (double) buffImg.getWidth() / 2,  
                        (double) buffImg.getWidth() / 2);  
            }  
              
            float alpha = 0.8f; // 透明度  
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,  
                    alpha));  
  
            //水印 的位置  
            g.drawImage(logoImg, buffImg.getWidth()/3, buffImg.getHeight()/2, null);  
  
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));  
  
            g.dispose();  
  
            os = new FileOutputStream(outputImageFile);  
  
            // 生成图片  
            ImageIO.write(buffImg, "JPG", os);  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (null != os)  
                    os.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  
  
}  

Java实现 图片水印或者文字水印


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值