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