تقدم هذه المقالة رمز المثال لإضافة العلامات المائية إلى صور Java. من المريح للغاية تنفيذ العلامات المائية في جافا. العلامة المائية يمكن أن تكون صورًا أو نصًا. المحتوى المحدد كما يلي
حزمة michael.io.image ؛ استيراد java.awt.alphacomposite ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.image ؛ استيراد java.awt.renderinghints ؛ استيراد java.awt.image.bufferedImage ؛ استيراد java.io.file ؛ استيراد java.io.fileInputStream ؛ استيراد java.io.fileOutputStream ؛ استيراد java.io.inputstream ؛ استيراد java.io.outputStream ؛ استيراد javax.imageio.imageio ؛ استيراد javax.swing.imageicon ؛ استيراد com.sun.image.codec.jpeg.jpegcodec ؛ استيراد com.sun.image.codec.jpeg.jpegimagedecoder ؛ استيراد com.sun.image.codec.jpeg.jpegimageencoder ؛ /** * Image Watermark * @plog http://sjsky.iteye.com * Author Michael */public class ImageMarkLogObyicon {/** * @param args */public static void main (string [] args) {String srcimgpathpath = /test/michael/myblog_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" ؛ // إضافة علامة مائية إلى صورة ImageMarkLogobyicon.MarkImagebyicon (IconPath ، SrcimgPath ، TargetPath) ؛ // إضافة علامة مائية إلى الصورة ، الدوران المائي -45 ImageMarkLogobyicon.MarkImagebyicon (IconPath ، Srcimgpath ، TargetPath2 ، -45) ؛ } / *** أضف علامة مائية إلى الصورة* param iconpath path path path* param srcimgpath path path* param targetpath path* / public static void markimagebyicon (string iconpath ، string srcimgpath ، string targpath) {markimagebyicon (iconpath ، srcimgpath ، targatpath ، null) ؛ } / *** أضف علامة مائية إلى الصورة وتعيين زاوية الدوران لصورة العلامة المائية* param iconpath path path* param srcimgpath path path* @param target path* @param detark target angle* / public static void markimageby (string iconpathpath ، string srcimgath ، حاول {image srcimg = imageio.read (ملف جديد (srcimgpath)) ؛ BufferedImage buffimg = جديد bufferedImage (srcimg.getwidth (null) ، srcimg.getheight (null) ، bufferedImage.type_int_rgb) ؛ // احصل على كائن الفرشاة // الرسومات g = buffimg.getGraphics () ؛ graphics2d g = buffimg.creategraphics () ؛ // قم بتعيين معالجة الحافة المسننة لقطاع الخط 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! = درجة) {// اضبط العلامة المائية G.Rotate (Math.Toradians (درجة) ، (مزدوج) buffimg.getWidth () / 2 ، (مزدوج) buffimg .getheight () / 2) ؛ } // مسار صورة العلامة المائية عمومًا GIF أو PNG ، بحيث يمكن ضبط الشفافية Imgicon = new ImageIcon (IconPath) ؛ // احصل على كائن الصورة. صورة IMG = IMGICON.GETIMAGE () ؛ تعويم ألفا = 0.5F ؛ // الشفافية G.SetComposite (Alphacomposite.getInstance (Alphacomposite.src_atop ، alpha)) ؛ // يشير إلى موقع صورة العلامة المائية G.DrawImage (IMG ، 150 ، 300 ، NULL) ؛ G.SetComposite (Alphacomposite.getInstance (Alphacomposite.src_over)) ؛ G.Dispose () ؛ OS = FileOutputStream جديد (TargerPath) ؛ // إنشاء Imageio.write (Buffimg ، "JPG" ، OS) ؛ System.out.println ("الصورة المنتهية إضافة رمز الختم ...") ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ } أخيرًا {try {if (null! = os) os.close () ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }}}}دعني أشارككم مثالًا آخر:
استيراد java.awt.color ؛ استيراد java.awt.font ؛ استيراد java.awt.graphics ؛ استيراد java.awt.image ؛ استيراد java.awt.image.bufferedimage ؛ استيراد java.io.file ؛ استيراد java.io.fileOutputStream ؛ استيراد javax.imageio.imageio ؛ com.sun.image.codec.jpeg.jpegcodec ؛ import com.sun.image.codec.jpeg.jpegimageencoder ؛ الفئة النهائية العامة ImageUtils {public imageUtils () {}/** * سلسلة ثابتة عامة getPressImgPath () {return applicationContext * .getRealPath ("/data/util/shuiyin.gif") ؛ } *///** * اطبع الصورة على الصورة * * param pressimg - * ملف watermark * param targetimg - * ملف الهدف * param x * -إحداثيات * @param y * -إحداثيات * // public Final static pressImage (string pressimg ، string int x ، int y) صورة src = imageio.read (_file) ؛ int wideth = src.getWidth (null) ؛ ارتفاع int = src.getheight (null) ؛ صورة bufferedImage = جديد bufferedImage (wideth ، الارتفاع ، bufferedImage.type_int_rgb) ؛ الرسومات g = image.creategraphics () ؛ G.DrawImage (SRC ، 0 ، 0 ، wideth ، الارتفاع ، null) ؛ // file file watermark _filebiao = ملف جديد (pressImg) ؛ صورة 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 ، (height - height_biao) / 2 ، wideth_biao ، height_biao ، null) ؛ // End of Watermark File G.Dispose () ؛ FileOutputStream Out = fileOutputStream جديد (TargetImg) ؛ jpegimageencoder encoder = jpegcodec.createjpegencoder (out) ؛ Encoder.encode (Image) ؛ out.close () ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ } ستر TargetImg ، String fontname ، int fontstyle ، int color ، int fontsize ، int x ، int y) {try {file _file = new file (targetimg) ؛ صورة src = imageio.read (_file) ؛ int wideth = src.getWidth (null) ؛ ارتفاع int = src.getheight (null) ؛ صورة bufferedImage = جديد bufferedImage (wideth ، الارتفاع ، bufferedImage.type_int_rgb) ؛ الرسومات g = image.creategraphics () ؛ G.DrawImage (SRC ، 0 ، 0 ، wideth ، الارتفاع ، null) ؛ G.SetColor (color.red) ؛ G.SetFont (خط جديد (fontname ، fontstyle ، fontsize)) ؛ G.DrawString (pressText ، wideth - fontsize - x ، height - fontsize / 2 - y) ؛ G.Dispose () ؛ FileOutputStream Out = fileOutputStream جديد (TargetImg) ؛ jpegimageencoder encoder = jpegcodec.createjpegencoder (out) ؛ Encoder.encode (Image) ؛ out.close () ؛ } catch (استثناء e) {system.out.println (e) ؛ }} public static void main (string [] args) {pressImage ("f: /logo.png" ، "f: /123.jpg" ، 0 ، 0) ؛ }}آمل أن يكون هذا المقال مفيدًا للجميع لاستخدام لغة جافا لإضافة علامات مائية إلى الصور.