Java. ImageIo to add image watermark code

  • 2020-04-01 02:05:16
  • OfStack


package com.blogs.image;
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;

public class ImageUtil {
    
    public static void main(String[] args) {
        String srcImgPath = "e:/2.png";
        String iconPath = "e:\logo.jpg";
        String targerPath = "e:/3.jpg";
        //Add a watermark to the image
        ImageUtil.waterMarkImageByIcon(iconPath, srcImgPath, targerPath, 0, 0,
, 0.1f);
        //Add a watermark to the image, Watermark rotation -45
        // ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath,
        // targerPath2, -45);
    }
    
    public static void waterMarkImageByIcon(String iconPath, String srcImgPath,
            String targerPath, Integer degree, Integer width, Integer height,
            float clarity) {
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(new File(srcImgPath));
            System.out.println("width:" + srcImg.getWidth(null));
            System.out.println("height:" + srcImg.getHeight(null));
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            //Get the brush object
            // Graphics g= buffImg.getGraphics();
            Graphics2D g = buffImg.createGraphics();
            //Sets the jagged edge of the line segment
            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 (null != degree) {
                //Set watermark rotation
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getHeight() / 2);
            }
            //Watermark image path watermark is usually GIF or PNG, so you can set the transparency
            ImageIcon imgIcon = new ImageIcon(iconPath);
            //I get the Image object.
            Image img = imgIcon.getImage();
            float alpha = clarity; //transparency
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));
            //Represents the position of the watermark image
            g.drawImage(img, width, height, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            g.dispose();
            os = new FileOutputStream(targerPath);
            //Generate images
            ImageIO.write(buffImg, "JPG", os);
            System.out.println(" Add watermark image to complete !");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void waterMarkByText(String logoText, String srcImgPath,
            String targerPath, Integer degree, Integer width, Integer height,
            Float clarity) {
        //The path to the main image
        InputStream is = null;
        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);
            //Get the brush object
            // Graphics g= buffImg.getGraphics();
            Graphics2D g = buffImg.createGraphics();
            //Sets the jagged edge of the line segment
            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 (null != degree) {
                //Set watermark rotation
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getHeight() / 2);
            }
            //Set the color
            g.setColor(Color.red);
            //Set the Font
            g.setFont(new Font(" Song typeface ", Font.BOLD, 30));
            float alpha = clarity;
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));
            //First parameter -> Set the content, the last two parameters -> The coordinate position of the text on the picture (x,y).
            g.drawString(logoText, width, height);
            g.dispose();
            os = new FileOutputStream(targerPath);
            //Generate images
            ImageIO.write(buffImg, "JPG", os);
            System.out.println(" Add watermark text to complete !");
        } 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();
            }
        }
    }
}

There is also an image zoom code:


    public static void zoomImage(String srcPath, String destPath, int destHeight, int destWidth) {
        try {
            BufferedImage srcBufferedImage = ImageIO.read(new File(srcPath));
            int imgWidth = destWidth;
            int imgHeight = destHeight;
            int srcWidth = srcBufferedImage.getWidth();
            int srcHeight = srcBufferedImage.getHeight();
            if (srcHeight >= srcWidth) {
                imgWidth = (int) Math.round(((destHeight * 1.0 / srcHeight) * srcWidth));
            } else {
                imgHeight = (int) Math.round(((destWidth * 1.0 / srcWidth) * srcHeight));
            }
            BufferedImage destBufferedImage = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = destBufferedImage.createGraphics();
            graphics2D.setBackground(Color.WHITE);
            graphics2D.clearRect(0, 0, destWidth, destHeight);
            graphics2D.drawImage(srcBufferedImage.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH), (destWidth / 2) - (imgWidth / 2), (destHeight / 2) - (imgHeight / 2), null);
            graphics2D.dispose();
            ImageIO.write(destBufferedImage, "JPEG", new File(destPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


Related articles: