Add a watermark to the Java picture and set the transparency. Description very detailed

Source: Internet
Author: User
Tags add watermark to picture

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.outputstream;import Javax.imageio.imageio;import Javax.swing.ImageIcon; /** * Picture watermark, setting transparency * Http://blog.csdn.net/hfmbook * @author Gary * Date Created: December 16, 2014 22:45:17 */public class Imagemarkl Ogobyicon {/** * @param args */public static void main (string[] args) {String srcimg           Path = "C:/1111.png";           String IconPath = "C:/0439.jpg";          String Targerpath = "C:/c.png";      Add a watermark to the image Imagemarklogobyicon.markimagebyicon (IconPath, Srcimgpath, Targerpath,-45); /** * Add watermark to Picture * @param iconpath watermark Picture Path * @param srcimgpath source Picture path * @param targerpath destination Picture Road           Path */public static void Markimagebyicon (String iconpath, String Srcimgpath,    String Targerpath) {Markimagebyicon (IconPath, Srcimgpath, Targerpath, NULL); }/** * Add watermark to picture, can set watermark picture rotation angle * @param iconpath watermark Picture Path * @param srcimgpath source Picture path * @param targ Erpath Target Picture path * @param degree watermark picture rotation angle */public static void Markimagebyicon (String iconpath, String srcimg           Path, 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), Bufferedima Ge.             TYPE_INT_RGB);               Get the Brush object//Graphics g= buffimg.getgraphics ();                 Graphics2D g = buffimg.creategraphics (); Sets the jagged edge processing for a segment G.setrenderinghint (Renderinghints.key_interpolation, RENDERINGHINTS.V                 Alue_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 generally GIF or PNG, so you can set the transparency ImageIcon Imgicon = new ImageIcon (IconPath);               Gets the image object.               Image img = imgicon.getimage (); float alpha = 0.2f;               Transparency G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));               Indicates the location of the watermark image G.drawimage (IMG, map, and null);               G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_over));               G.dispose ();               OS = new FileOutputStream (Targerpath); Generate Picture Imageio.wriTe (buffimg, "JPG", OS);           } catch (Exception e) {e.printstacktrace ();               } finally {try {if (null! = OS) os.close ();               } catch (Exception e) {e.printstacktrace ();  }           }       }   }


Add a watermark to the Java picture and set the transparency. Description very detailed

Related Article

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.