자바 그림 에 워 터 마크 (그림 또는 텍스트)

10144 단어
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
 * @author jasonLu
 * @date 2017/5/11 12:30
 * @Description:                  
 */
public class WaterMaskImgUtils
{

    /**
     *       
     *
     * @param targetImg       , :C://myPictrue//1.jpg
     * @param waterImg       , :C://myPictrue//logo.png
     * @param x                 ,  x<0,      
     * @param y                 ,  y<0,      
     * @param alpha    (0.0 -- 1.0, 0.0     ,1.0      )
     * @param degree         
     */
    public static void pressImage(String targetImg, String waterImg,  int x, int y, float alpha, Integer degree,String suffix)
    {
         pressImage(targetImg,waterImg,null,x,y,alpha,null,suffix);
    }

    /**
     *       
     * @param targetImg       , :C://myPictrue//1.jpg
     * @param waterImg       , :C://myPictrue//logo.png
     * @param outImg       ,    ,      
     * @param x                 ,  x<0,      
     * @param y                 ,  y<0,      
     * @param alpha    (0.0 -- 1.0, 0.0     ,1.0      )
     * @param degree         
     */
    public static void pressImage(String targetImg, String waterImg, String outImg, int x, int y, float alpha, Integer degree,String suffix)
    {
        try
        {
            File file = new File(targetImg);
            //            ,         
            File outFile;
            if(StringUtils.isEmpty(outImg))
            {
                outFile = file;
            }
            else
            {
                outFile = new File(outImg);
            }

            Image image = null;
            try
            {
                image = ImageIO.read(file);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);

            if (null != degree)
            {
                //       
                g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }

            Image waterImage = ImageIO.read(new File(waterImg));    //     
            int width_1 = waterImage.getWidth(null);
            int height_1 = waterImage.getHeight(null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

            int widthDiff = width - width_1;
            int heightDiff = height - height_1;
            if (x < 0)
            {
                x = widthDiff / 2;
            }
            else if (x > widthDiff)
            {
                x = widthDiff;
            }
            if (y < 0)
            {
                y = heightDiff / 2;
            }
            else if (y > heightDiff)
            {
                y = heightDiff;
            }
            g.drawImage(waterImage, x, y, width_1, height_1, null); //       
            g.dispose();
            //           .
            ImageIO.write(bufferedImage, suffix.substring(1), outFile);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    /**
     *       
     *
     * @param targetImg       , :C://myPictrue//1.jpg
     * @param pressText     ,  :   
     * @param fontName     ,     :  
     * @param fontStyle     , :     (Font.BOLD|Font.ITALIC)
     * @param fontSize     ,     
     * @param color     
     * @param x                 ,  x<0,      
     * @param y                 ,  y<0,      
     * @param alpha    (0.0 -- 1.0, 0.0     ,1.0      )
     */
    public static void pressText(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, int x, int y, float alpha,String suffix)
    {
        pressText(targetImg,pressText,null,fontName,fontStyle,fontSize,color,x,y,alpha,null,suffix);
    }

    /**
     *       
     * @Description   (0,0)       ,(  ,  )     ,(0,xxx)   ,(x,0)    ,           
     * @param targetImg       , :C://myPictrue//1.jpg
     * @param pressText     ,  :   
     * @param outImg       ,    ,      
     * @param fontName     ,     :  
     * @param fontStyle     , :     (Font.BOLD|Font.ITALIC)
     * @param fontSize     ,     
     * @param color     
     * @param positionX                 ,  x<0,      
     * @param positionY                 ,  y<0,      
     * @param alpha    (0.0 -- 1.0, 0.0     ,1.0      )
     * @param degree         
     */
    public static void pressText(String targetImg, String pressText, String outImg, String fontName, int fontStyle, int fontSize, Color color, int positionX, int positionY, float alpha, Integer degree,String suffix)
    {
        try
        {
            File file = new File(targetImg);
            //             ,         
            File outFile;
            if (StringUtils.isEmpty(outImg))
            {
                outFile = file;
            }
            else
            {
                outFile = new File(outImg);
            }
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Font font = new Font(fontName, fontStyle, fontSize);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            g.setFont(font);
            g.setColor(color);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            if (null != degree)
            {
                //       
                g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }
            //         
            FontRenderContext context = g.getFontRenderContext();
            Rectangle2D stringBounds = font.getStringBounds(pressText,context);

            int textWidth = (int) stringBounds.getWidth() ;
            int textHeight = (int) stringBounds.getHeight();

            int widthDiff = width - textWidth;
            int heightDiff = height - textHeight;
            if (positionX < 0)
            {
                positionX = widthDiff / 2;
            }
            else if (positionX > widthDiff)
            {
                positionX = widthDiff;
            }
            if (positionY < 0)
            {
                positionY = heightDiff / 2;
            }
            else if (positionY > heightDiff)
            {
                positionY = heightDiff;
            }

            g.drawString(pressText, positionX, positionY + textHeight);
            g.dispose();
            ImageIO.write(bufferedImage, suffix, outFile);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    /**
     *       
     * @param targetImg       , :C://myPictrue//1.jpg
     * @param pressText     ,  :   
     * @param fontName     ,     :  
     * @param fontStyle     , :     (Font.BOLD|Font.ITALIC)
     * @param fontSize     ,     
     * @param alpha    (0.0 -- 1.0, 0.0     ,1.0      )
     */
    public static void pressTextToRightBottom(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, float alpha,String suffix)
    {

        pressTextToRightBottom(targetImg,pressText,null,fontName,fontStyle,fontSize,color,alpha,null,suffix);
    }
    /**
     *            
     *
     * @param targetImg       , :C://myPictrue//1.jpg
     * @param pressText     ,  :   
     * @param outImg       ,    ,      
     * @param fontName    ,  :  
     * @param fontStyle     , :     (Font.BOLD|Font.ITALIC)
     * @param fontSize     ,     
     * @param alpha    (0.0 -- 1.0, 0.0     ,1.0      )
     * @param degree         
     */
    public static void pressTextToRightBottom(String targetImg, String pressText, String outImg, String fontName, int fontStyle, int fontSize, Color color, float alpha, Integer degree,String suffix)
    {
        try
        {
            File file = new File(targetImg);
            File outFile;
            if (StringUtils.isEmpty(outImg))
            {
                outFile = file;
            }
            else
            {
                outFile = new File(outImg);
            }
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Font font = new Font(fontName, fontStyle, fontSize);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            g.setFont(font);
            g.setColor(color);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            if (null != degree)
            {
                //       
                g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
            }

            //         
            FontRenderContext context = g.getFontRenderContext();
            Rectangle2D stringBounds = font.getStringBounds(pressText,context);

            int textWidth = (int) stringBounds.getWidth() ;
            int textHight = (int) stringBounds.getHeight();

            g.drawString(pressText, width - textWidth, height - textHight);
            g.dispose();
            ImageIO.write(bufferedImage, suffix.substring(1), outFile);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

좋은 웹페이지 즐겨찾기