Cet article présente l'exemple de code pour l'ajout de filigranes aux images Java. Il est très pratique de mettre en œuvre des filigranes en Java. Le filigrane peut être des images ou du texte. Le contenu spécifique est le suivant
Package Michael.io.image; import java.awt.alphacomposite; import java.awt.graphics2d; import java.awt.image; Importer java.awt.RenderingHints; Importer java.awt.image.bufferedImage; Importer java.io.file; import java.io.fileInputStream; Importer java.io.fileOutputStream; import java.io.inputStream; import java.io.outputStream; import javax.imageio.imageio; import javax.swing.imageicon; import com.sun.image.codec.jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegImagedEcoder; import com.sun.image.codec.jpeg.jpegImageEncoder; / ** * Image Watermark * @blog http://sjsky.iteye.com * @author michael * / public class ImageMarkLogobyIcon {/ ** * @param args * / public static void main (string [] args) {String srcimgpath = "d: /test/michael/myblig_01.png"; 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"; // Ajouter un filigrane à l'image ImageMarkLogobyIcon.MarkeMageByicon (iconPath, SrcimgPath, TargetPath); // ajouter un filigrane à l'image, rotation de filigrane-45 ImageMarklogobyicon.markemagebyicon (iconPath, srcimgpath, ciblePath2, -45); } / ** * Ajouter un filigrane à l'image * @Param IconPath WaterMark Image Path * @param srcimgpath Source Image Path * @param TargetPath Target Image Path * / public static void markimagebyicon (String iconPath, String SrcimgPath, String TargetPath) {markimagebyicon (iconpath, srcimgpath, targetpath, null); } / ** * Ajoutez un filigrane à l'image et définissez l'angle de rotation de l'image de filigrane * @Param iconPath Watermark Image Chemin d'image * @param degré Watermark Angle Rotation * / Public Static Void MarkImageByicon (String IconPath, String srcimgpath nul; try {image srcimg = imageo.read (nouveau fichier (srcimgpath)); BufferedImage BuffImg = new BufferedImage (srcimg.getWidth (null), srcimg.getheight (null), bufferedImage.type_int_rgb); // Obtenez l'objet Brush // Graphics G = BuffImg.getGraphics (); Graphics2d g = buffimg.creategraphics (); // Définissez le traitement de bord de déchaînement du segment de ligne g.setRenderingHint (RenderingHint.key_interpolation, RenderingHint.value_interpolation_bilinear); G.DrawImage (srcimg.getScaledInstance (srcimg.getWidth (null), srcimg .getheight (null), image.scale_smooth), 0, 0, null); if (null! = degré) {// Réglez la rotation du filigrane g.rotate (math.toradians (degré), (double) buffimg.getWidth () / 2, (double) buffimg .getheight () / 2); } // Le chemin de l'image de filigrane est généralement GIF ou PNG, afin que la transparence puisse être définie ImageIcon IMGICON = new ImageIcon (iconPath); // Obtenez l'objet d'image. Image img = imgicon.getImage (); float alpha = 0,5f; // Transparence G.SetComposite (alphacomposite.getInstance (alphacomposite.src_atop, alpha)); // indique l'emplacement de l'image de filigrane G.Drawimage (IMG, 150, 300, null); g.setComposite (alphacomposite.getInstance (alphacomposite.src_over)); g.dispose (); OS = new FileOutputStream (TargerPath); // générer image imageo.write (buffimg, "jpg", os); System.out.println ("Image terminée ajout d'icônes Sceau ..."); } catch (exception e) {e.printStackTrace (); } enfin {try {if (null! = os) os.close (); } catch (exception e) {e.printStackTrace (); }}}}Permettez-moi de partager avec vous un autre exemple:
Importer java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image; import java.awt.image.bufferedImage; import java.io.file; import java.io.fileoutStream; import javax.imageio.imageio; import com.sun.image.codec.jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegImageEncoder; Public Final class ImageUtils {public ImageUtils () {} / ** * Public Final Static String getPressImgPath () {return applicationContext * .getRealPath ("/ template / data / util / shuiyin.gif"); } * / / ** * Imprimez l'image sur l'image * * @param pressimg - * Fichier de watermark * @param cibleMg - * Fichier cible * @param x * --x Coordonnées * @param y * - y coordonnées * / public final void pressimage (String PressIMg, String TargetImg, int x, int y) {essai {// file cible fichier _file = new file (new fichier); Image src = imageo.read (_file); int wideth = src.getWidth (null); int height = src.GetHeight (null); BufferedImage Image = new BufferedImage (Wideth, Height, BufferedImage.Type_int_rgb); Graphiques g = image.creategraphics (); G.DrawImage (Src, 0, 0, Wideth, hauteur, null); // File de fichier de watermark _FileBiao = nouveau fichier (presimg); Image src_biao = imageo.read (_fileBiao); int wideth_biao = src_biao.getWidth (null); int height_biao = src_biao.getheight (null); G.DrawImage (src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null); // Fin du fichier de filigrane g.dispose (); FileoutputStream out = new FileOutputStream (TargetImg); JpegImageEncoder Encoder = jPegCodec.CreateJPegenCoder (Out); Encoder.encode (image); out.close (); } catch (exception e) {e.printStackTrace (); }} / ** * Imprimer l'image de filigrane du texte * * @param prestext * --text * @param cibleMg - * cible image * @param fontname - * nom de police * @param Fontstyle - * Font Style * @param couleur - * Font Color * @param FonTSize - * FONT SIZETTATED * @Param X - * offSet * @param y * / public static Void Presstex FontName, int FontStyle, int Color, int Fontize, int x, int y) {try {file _file = new File (TargetImg); Image src = imageo.read (_file); int wideth = src.getWidth (null); int height = src.GetHeight (null); BufferedImage Image = new BufferedImage (Wideth, Height, BufferedImage.Type_int_rgb); Graphiques g = image.creategraphics (); G.DrawImage (Src, 0, 0, Wideth, hauteur, null); g.setColor (Color.Red); g.setfont (nouvelle police (Fontname, Fontstyle, FontSize)); G.Drawstring (PressText, Wideth - FonTSize - X, Height - FonTSize / 2 - Y); g.dispose (); FileoutputStream out = new FileOutputStream (TargetImg); JpegImageEncoder Encoder = jPegCodec.CreateJPegenCoder (Out); Encoder.encode (image); out.close (); } catch (exception e) {System.out.println (e); }} public static void main (string [] args) {pressimage ("f: /logo.png", "f: /123.jpg", 0, 0); }}J'espère que cet article sera utile pour que tout le monde utilise le langage Java pour ajouter des filigranes aux images.