图片加水印

package com.founder.cms.watermark.util;

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.InputStream;

import java.io.OutputStream;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

/*******************************************************************************

 * Description: 图片水印工具类

 * Copyright: Copyright (c) 2011

 * Company: Founder

 * Project: CMS

 *

 * @Author taoxg

 * @version 1.0

 * @See

 * HISTORY 2011-9-27 taoxg create

 ******************************************************************************/

public class ImageMarkLogoUtil {

    

    // 水印透明度

    private static float alpha = 0.5f;

    // 水印横向位置

    private static int positionWidth = 150;

    // 水印纵向位置

    private static int positionHeight = 300;

    // 水印文字字体

    private static Font font = new Font("宋体", Font.BOLD, 30);

    // 水印文字颜色

    private static Color color = Color.red;

    /**

     *

     * @param alpha

     *          水印透明度

     * @param positionWidth

     *          水印横向位置

     * @param positionHeight

     *          水印纵向位置

     * @param font

     *          水印文字字体

     * @param color

     *          水印文字颜色

     */

    public static void setImageMarkOptions(float alpha , int positionWidth , int positionHeight ,Font font,Color color){

        if(alpha!=0.0f)ImageMarkLogoUtil.alpha = alpha;

        if(positionWidth!=0)ImageMarkLogoUtil.positionWidth = positionWidth;

        if(positionHeight!=0)ImageMarkLogoUtil.positionHeight = positionHeight;

        if(font!=null)ImageMarkLogoUtil.font = font;

        if(color!=null)ImageMarkLogoUtil.color = color;

    }

    

    /**

     * 给图片添加水印图片

     *

     * @param iconPath

     *            水印图片路径

     * @param srcImgPath

     *            源图片路径

     * @param targerPath

     *            目标图片路径

     */

    public static void markImageByIcon(String iconPath, String srcImgPath,

            String targerPath) {

        markImageByIcon(iconPath, srcImgPath, targerPath, null);

    }

    /**

     * 给图片添加水印图片、可设置水印图片旋转角度

     *

     * @param iconPath

     *            水印图片路径

     * @param srcImgPath

     *            源图片路径

     * @param targerPath

     *            目标图片路径

     * @param degree

     *            水印图片旋转角度

     */

    public static void markImageByIcon(String iconPath, String srcImgPath,

            String targerPath, Integer degree) {

        OutputStream os = null;

        try {

            

            Image srcImg = ImageIO.read(new File(srcImgPath));

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),

                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 1、得到画笔对象

            Graphics2D g = buffImg.createGraphics();

            // 2、设置对线段的锯齿状边缘处理

            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);

            // 3、设置水印旋转

            if (null != degree) {

                g.rotate(Math.toRadians(degree),(double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);

            }

            // 4、水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度

            ImageIcon imgIcon = new ImageIcon(iconPath);

            // 5、得到Image对象。

            Image img = imgIcon.getImage();

            

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));

            // 6、水印图片的位置

            g.drawImage(img, positionWidth, positionHeight, null);

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

            // 7、释放资源

            g.dispose();

            

            // 8、生成图片

            os = new FileOutputStream(targerPath);

            ImageIO.write(buffImg, "JPG", os);

            System.out.println("图片完成添加水印图片");

            

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (null != os)

                    os.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

    /**

     * 给图片添加水印文字

     *

     * @param logoText

     *          水印文字

     * @param srcImgPath

     *          源图片路径

     * @param targerPath

     *          目标图片路径

     */

    public static void markImageByText(String logoText, String srcImgPath,

            String targerPath) {

        markImageByText(logoText, srcImgPath, targerPath, null);

    }

    /**

     * 给图片添加水印文字、可设置水印文字的旋转角度

     *

     * @param logoText

     * @param srcImgPath

     * @param targerPath

     * @param degree

     */

    public static void markImageByText(String logoText, String srcImgPath,

            String targerPath, Integer degree) {

        

        InputStream is = null;

        OutputStream os = null;

        try {

            // 1、源图片

            Image srcImg = ImageIO.read(new File(srcImgPath));

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 2、得到画笔对象

            Graphics2D g = buffImg.createGraphics();

            // 3、设置对线段的锯齿状边缘处理

            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);

            // 4、设置水印旋转

            if (null != degree) {

                g.rotate(Math.toRadians(degree),(double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);

            }

            // 5、设置水印文字颜色

            g.setColor(color);

            // 6、设置水印文字Font

            g.setFont(font);

            // 7、设置水印文字透明度

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));

            // 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)

            g.drawString(logoText, positionWidth, positionHeight);

            // 9、释放资源

            g.dispose();

            // 10、生成图片

            os = new FileOutputStream(targerPath);

            ImageIO.write(buffImg, "JPG", os);

            System.out.println("图片完成添加水印文字");

            

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                if (null != is)

                    is.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

            try {

                if (null != os)

                    os.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

    

    public static void main(String [] args){

        String srcImgPath = "d:/qie.jpg"

        String logoText = "[ I love Qie]";

        String iconPath = "d:/shuimu.jpg"

        

        String targerTextPath = "d:/qie_text.jpg"

        String targerTextPath2 = "d:/qie_text_rotate.jpg";

        

        String targerIconPath = "d:/qie_icon.jpg"

        String targerIconPath2 = "d:/qie_icon_rotate.jpg";

        

        System.out.println("给图片添加水印文字开始...");

        // 给图片添加水印文字 

        markImageByText(logoText, srcImgPath, targerTextPath); 

        // 给图片添加水印文字,水印文字旋转-45 

        markImageByText(logoText, srcImgPath, targerTextPath2, -45); 

        System.out.println("给图片添加水印文字结束...");

        

        System.out.println("给图片添加水印图片开始...");

        setImageMarkOptions(0.3f,1,1,null,null);

        // 给图片添加水印图片 

        markImageByIcon(iconPath, srcImgPath, targerIconPath); 

        // 给图片添加水印图片,水印图片旋转-45 

        markImageByIcon(iconPath, srcImgPath, targerIconPath2, -45);

        System.out.println("给图片添加水印图片结束...");

    }

}

时间: 2024-10-18 06:11:02

图片加水印的相关文章

thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印

今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载地址:https://github.com/Nintendov/Ueditor-thinkphp 下载下来,看着配置就可以了. 下面就是给上传图片加水印: (在做这步前,请确保ueditor已经正常工作) 我的工程目录如下: fonts里面的fz.fft为水印字体 images里面的logo.png为水印图片

java 图片加水印,设置透明度。说明很详细

package com.yidao.common; 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.Out

文件上传,图片加水印

文件上传: 所用控件:FileUpload 使用时的思路: 1.判断用户是否选中了文件 FileUpload.FileName获取选中的文件名,判断长度,如果长度大于零就代表已经选择了文件 JS端:通过ID获取控件,然后控件的value获取选中的文件名 2.将文件保存到服务器上 FileUpload.SaveAs("绝对路径"); 3.获得绝对路径 先编写相对路径:比如"UpLoads/abc.txt" 再把路径映射成绝对路径:Server.MapPath(&quo

利用PHP为图片加水印

1.为图片加风格字体 <?php header('Content-type:image/jpeg');//告诉服务器用JPEG图像格式输出 $img=imagecreatefromjpeg('images/girl.jpg');//建立背景图像 $color=imagecolorallocate($img,100,100,100);//分配给字体的颜色 $width=imagesx($img);//输出背景图像的总宽度 $height=imagesy($img);//输出背景图像的总长度 $po

iOS图片加水印效果的实现并保存至相冊

图片加水印效果的实现并保存至相冊 实现效果如图: project下载:githubproject下载链接 代码: - (void)viewDidLoad { [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@"pushu.jpg"]; UIImage *waterImage = [self waterMarkImage:image withText:@"朴树水印測试"]; UIImageWriteT

PHP给图片加水印具体实现

给图片加水印实现方法如下: 1 class Mark 2 { 3 public function __construct() 4 { 5 6 } 7 8 /** 9 * 加水印 10 * @param file $srcImg 要加水印的图片 11 * @param file $waterImg 水印图片 12 * @param integer $position 水印图片放置位置 1:左上,2:右上,3:居中,4:左下,5:右下 13 * @param integer $alpha 水印图片透

java 图片加水印,设置透明度。说明非常具体

package com.yidao.common; 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.Out

图片加水印 图片验证码

图片加水印 1   获取这张图片 System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent); 2 给图片加水印 Graphics g = Graphics.FromImage(img); string s = "WWW.ITNBA.COM";         s 给图片加的水印文字 Font f = new Font("微软雅黑", 30); Brush b

给图片加水印

public void DoAddMark(object ParameterInfo) { #region 拆分参数 List<object> listParameter = (List<object>)ParameterInfo; ConnParameter connpar = (ConnParameter)listParameter[0]; //连接数据库参数 string[] strImgPath = (string[])listParameter[1]; //图片源路径 s

php 给图片加水印

昨天下午同事问我一个php的问题,就是给图片加水印,php我也一知半解,网上资料找了一通,自己就写了一个加水印的php类. 具体代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63