Java share - to add a picture watermark images (picture rotation)

This article describes how to add Java implementation to the picture image watermark, image watermark, involving java reading a picture, add watermark settings and other related operational skills, a friend in need can refer to.

 The pictures on the previous add a text watermark to implementation is the same, do not say directly on the code.

package com.myFirstSpring.test; 

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;

/** 
 * @author tqf
 * @version 创建时间:2020-4-3 上午10:49:02 
 * 类说明:图片添加水印   文字&图片水印 
 */
public class ImageRemarkUtil {

    // 水印透明度
    private static float alpha = 0.5f;
    // 水印横向位置
    private static int positionWidth = 150;
    // 水印纵向位置
    private static int positionHeight = 300;
    // 水印文字字体
    private static Font font = new Font("宋体", Font.BOLD, 35);
    // 水印文字颜色
    private static Color color = Color.red;

    

    /**
     * 给图片添加水印图片、可设置水印图片旋转角度
     * 
     * @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();
            }
        }
    }

    

    

    public static void main(String[] args) {
    	//原始图片路径
        String srcImgPath = "d:/demo.png";  //只需要这一张图片 然后生成4张图片
        //添加的icon图片
        String iconPath = "d:/logo.png";
        String targerIconPath = "d:/demo_logo.png"; //添加图片水印之后的图片存放路径
        String targerIconPath2 = "d:/demo_logo2.png"; //添加图片水印之后的图片存放路径 水印图片旋转-45

        System.out.println("给图片添加水印图片开始...");
        setImageMarkOptions(0.3f, 1, 1, null, null);
        // 给图片添加水印图片
        markImageByIcon(iconPath, srcImgPath, targerIconPath,null);
        // 给图片添加水印图片,水印图片旋转-45
        markImageByIcon(iconPath, srcImgPath, targerIconPath2, -45);
        System.out.println("给图片添加水印图片结束...");

    }

}
 

The following are two methods for generating the above image after the addition of the watermark, there is no rotation of the first image, the second image is rotated by -45 degrees 

Well methods on picture to add a picture watermark aspects written above, I hope that the contents of this paper to learn or work can bring some help, if useful to you point Like Comment Favorite Artist! ^ _ ^  

Published 105 original articles · won praise 175 · views 50000 +

Guess you like

Origin blog.csdn.net/tanqingfu1/article/details/105292492