Picture Add watermark (Java implementation)

Source: Internet
Author: User

Some scenarios require a watermark to protect against misappropriation, such as Weibo user images. The implementation of adding watermarks in Java requires the use of BufferedImage, graphics2d, and ImageIO classes. 1. Add text watermark

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;  /** * Add text watermark * * @author Ricky Fung/public class Textmarkprocessor {/** * @param args/Public
    static void Main (string[] args) {new Textmarkprocessor (). Testtextmark ();
        public void Testtextmark () {File Srcimgfile = new file ("D:/test/desktop.png");

        String logotext = "[Angel's Wings]";

        File Outputrotateimagefile = new file ("D:/test/desktop_text_mark.jpg");
    Createwatermarkbytext (Srcimgfile, Logotext, Outputrotateimagefile, 0); public void Createwatermarkbytext (File srcimgfile, String Logotext, Fi Le 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 graphics = Buffimg.creategraphics (); Graphics.setrenderinghint (Renderinghints.key_interpolation, Renderinghints.value_interpolation_bilinea
            R); Graphics.DrawImage (srcimg.getscaledinstance (Srcimg.getwidth (null), srcimg.getheight (null), Ima Ge.

            Scale_smooth), 0, 0, NULL); if (degree>0) {graphics.rotate (Math.toradians (degree), (double) buffimg.getwid
            Th ()/2, (Double) buffimg.getheight ()/2);
            } graphics.setcolor (color.red);

            Graphics.setfont (New Font ("Song Body", Font.Bold, 36));
            float alpha = 0.8f; Graphics.setcomposite (Alphacomposite.getInstance (Alphacomposite.src_atop, Alpha));
            Graphics.DrawString (Logotext, Buffimg.getwidth ()/3, Buffimg.getheight ()/2);

            Graphics.dispose ();
            OS = new FileOutputStream (outputimagefile);
        Generate Picture Imageio.write (buffimg, "JPG", OS);
        catch (Exception e) {e.printstacktrace ();
            Finally {try {if (null!= OS) os.close ();
            catch (Exception e) {e.printstacktrace (); }
        }
    }
}
2. Add a picture watermark
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; /** * Add Picture watermark * * @author Ricky Fung/public class Picturemarkprocessor {/** * @param args/Pub
    Lic static void Main (string[] args) {new Picturemarkprocessor (). Testpicturemark ();
        public void Testpicturemark () {File Srcimagefile = new file ("D:/test/desktop.png");

        File Logoimagefile = new file ("D:/test/tools.png");

        File Outputroateimagefile = new file ("D:/test/desktop_pic_mark.jpg");
    Createwatermarkbyicon (Srcimagefile, Logoimagefile, Outputroateimagefile, 0);
                                             } public void Createwatermarkbyicon (file srcimagefile, file Logoimagefile, File Outputimagefile, doubledegree) {outputstream OS = null;

            try {Image srcimg = Imageio.read (srcimagefile); BufferedImage buffimg = new BufferedImage (Srcimg.getwidth (null), srcimg.getheight (null), BufferedImage .

            TYPE_INT_RGB);
            Graphics2D graphics = Buffimg.creategraphics ();
            Graphics.setrenderinghint (Renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); Graphics.DrawImage (srcimg.getscaledinstance (Srcimg.getwidth (null), srcimg.getheight (null), Ima Ge.

            Scale_smooth), 0, 0, NULL);
            ImageIcon Logoimgicon = new ImageIcon (Imageio.read (logoimagefile));

            Image logoimg = Logoimgicon.getimage (); Rotate if (degree>0) {graphics.rotate (Math.toradians (degree), DOUBL
            e) Buffimg.getwidth ()/2, (Double) buffimg.getwidth ()/2); float Alpha = 0.8f;

            Transparency Graphics.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));
            Watermark Position Graphics.DrawImage (logoimg, Buffimg.getwidth ()/3, Buffimg.getheight ()/2, NULL);
            Graphics.setcomposite (Alphacomposite.getinstance (alphacomposite.src_over));

            Graphics.dispose ();
            OS = new FileOutputStream (outputimagefile);
        Generate Picture Imageio.write (buffimg, "JPG", OS);
        catch (Exception e) {e.printstacktrace ();
            Finally {try {if (null!= OS) os.close ();
            catch (Exception e) {e.printstacktrace ();
 }
        }
    }
}

The implementation effect is as follows:

Original:

Add text watermark:

Add Image watermark:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.