In diesem Artikel wird der Beispielcode zum Hinzufügen von Wasserzeichen zu Java -Bildern vorgestellt. Es ist sehr bequem, Wasserzeichen in Java umzusetzen. Das Wasserzeichen kann Bilder oder Text sein. Der spezifische Inhalt ist wie folgt
Paket Michael.io.image; Import Java.awt.Alphacomposite; Import Java.awt.Graphics2d; import Java.awt.image; importieren java.awt.renderingHINDINTS; Import Java.awt.image.BuffenedImage; Import Java.io.file; import Java.io.FileInputStream; importieren java.io.fileoutputStream; importieren java.io.inputstream; importieren java.io.outputstream; import Javax.imageo.imageo; import Javax.swing.imageicon; import com.sun.image.codec.jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegimagedEcoder; import com.sun.image.codec.jpeg.jpegimageCoder; /** * Image Watermark * @blog http://sjsky.iteye.com * @Author Michael */Public Class ImagemarkLogobyicic {/** * @param args */public static void main (String [] args) {String srcimgpat = "d: /Test/michael String iconpath = "d: /test/michael/blog_logo.png"; String targetPath = "D: /test/michael/img_mark_icon.jpg"; String targetPath2 = "D: /test/michael/img_mark_icon_rotate.jpg"; // Wasserzeichen zum Image ImagemarkLogobyicon.MarkimageByicon (IconPath, SrcimgPath, TargetPath) hinzufügen; // Wasserzeichen zum Bild hinzufügen, Wasserzeichen Rotation -45 ImagemarkLogobyicon } / *** Fügen Sie dem Bild ein Wasserzeichen hinzu* @param iconPath Wasserzeichen Bildweg* @param srcimgPath Quelle Bildpfad* @param Zielpath Zielbilderpfad* / public static void markimageByicon (String iconPath, String srcimgpath, String Zielpath) {markimageByicon (iconpath, srcimgpath, targetPath, actarpHpath, nullpath, null); } / *** Fügen Sie dem Bild ein Wasserzeichen hinzu und setzen Sie den Rotationswinkel des Wasserzeichenbildes* @param iconPath Wassermarkbildpfad* @param srcimgPath Quellbild Pfad* @param targetPhode -Ziel -Bild -Pfad* @param Grad Wassermark -Bilddrehwinkel* / Public Static Void MarkimageByic. try {image srcimg = imageio.read (neue Datei (srcimgPath)); Bufferedimage buffimg = new bufferedImage (srcimg.getwidth (null), srcimg.getheight (null), bufferedimage.type_int_rgb); // das Pinselobjekt abrufen // Graphics g = buffimg.getGraphics (); Graphics2d g = buffimg.createGraphics (); // Setzen Sie die gezackte Kantenverarbeitung des Leitungssegments 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! } // Der Pfad des Wasserzeichenbildes ist im Allgemeinen GIF oder PNG, so dass die Transparenz imageCon imgicon = new ImageIcon (IconPath) festgelegt werden kann; // das Bildobjekt abrufen. Bild img = imgicon.getImage (); float alpha = 0,5f; // Transparenz G.SetComposite (Alphacomposite.getInstance (Alphacomposite.src_atop, Alpha)); // Zeigt die Position des Wasserzeichenbilds G.Drawimage an (IMG, 150, 300, NULL); G.SetComposite (Alphacomposite.getInstance (Alphacomposite.src_over)); g.disponse (); os = new FileOutputStream (TargerPath); // Image Imageio.write (Buffimg, "JPG", OS) erzeugen; System.out.println ("Bild fertiggestelltes Symbol Seal ..."); } catch (Ausnahme e) {e.printstacktrace (); } endlich {try {if (null! = os) os.close (); } catch (Ausnahme e) {e.printstacktrace (); }}}}Lassen Sie mich ein weiteres Beispiel mit Ihnen teilen:
Import Java.awt.Color; Import Java.awt.font; Import Java.awt.graphics; Import Java.awt.image; Import Java.awt.image.BuffeedImage; Import Java.io.file; com.sun.image.codec.jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegimagecoder; public Final Class Imageutils {public imageutils () {}/** * public Final Static String getSimgPath () {return applicationContext * .getRealPath ("/template/data/util/shuiyin.gif"); } */ /** * Print the image on the image* * @param pressImg -- * Watermark file* @param targetImg -- * Target file* @param x * --x coordinates* @param y * --y coordinates*/ public final static void pressImage(String pressImg, String targetImg, int x, int y) { try { //Target file File _file = new File(targetImg); Bild src = imageio.read (_file); int wideth = src.getwidth (null); int height = src.getheight (null); Bufferedimage image = new bufferedImage (wideth, Höhe, bufferedimage.type_int_rgb); Grafik g = Bild.CreateGraphics (); G.Drawimage (SRC, 0, 0, Wideth, Höhe, Null); // Watermark -Datei Datei _FileBiao = New Datei (PressImg); Bild src_biao = imageio.read (_Filebiao); int wideth_biao = src_biao.getwidth (null); int height_biao = src_biao.getheight (null); g.drawimage (src_biao, (wideth - wideth_biao) / 2, (Höhe - Höhe_biao) / 2, wideth_biao, height_biao, null); // Ende der Wasserzeichendatei g.disponse (); FileOutputStream out = new FileOutputStream (targetImg); JpegimageCoder cccoder = jpegcodec.createjpeGenCoder (out); ccoder.encode (Bild); out.close (); } catch (Ausnahme e) {e.printstacktrace (); } } /** * Print text watermark image* * @param pressText * --text* @param targetImg -- * Target image* @param fontName -- * Font name* @param fontStyle -- * Font style* @param color -- * Font color* @param fontSize -- * Font size* @param x -- * Offset* @param y */ public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, int color, int fontsize, int x, int y) {try {file _file = new Datei (targetImg); Bild src = imageio.read (_file); int wideth = src.getwidth (null); int height = src.getheight (null); Bufferedimage image = new bufferedImage (wideth, Höhe, bufferedimage.type_int_rgb); Grafik g = Bild.CreateGraphics (); G.Drawimage (SRC, 0, 0, Wideth, Höhe, Null); G.SetColor (color.red); G.SetFont (neue Schriftart (FontName, fontStyle, fontsize)); g.drawstring (pressText, wideth - fontsize - x, Höhe - fontsize / 2 - y); g.disponse (); FileOutputStream out = new FileOutputStream (targetImg); JpegimageCoder cccoder = jpegcodec.createjpeGenCoder (out); ccoder.encode (Bild); out.close (); } catch (Ausnahme e) {System.out.println (e); }} public static void main (String [] args) {pressimage ("f:/logo.png", "f: /123.jpg", 0, 0); }}Ich hoffe, dass dieser Artikel für alle hilfreich ist, um Java -Sprache zu verwenden, um Bildern Wasserzeichen hinzuzufügen.