บทความนี้แนะนำรหัสตัวอย่างสำหรับการเพิ่มลายน้ำลงในรูปภาพ 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; /** * ภาพลายน้ำ * @blog http://sjsky.iteye.com * @author Michael */คลาสสาธารณะ imageMarklogobyicon {/** * @param args */โมฆะสาธารณะคง string iconpath = "d: /test/michael/blog_logo.png"; สตริง targetPath = "d: /test/michael/img_mark_icon.jpg"; String targetPath2 = "d: /test/michael/img_mark_icon_rotate.jpg"; // เพิ่มลายน้ำลงใน Image ImageMarklogobyicon.markimageByicon (IconPath, SrcimgPath, TargetPath); // เพิ่มลายน้ำลงในภาพลายน้ำหมุน -45 Imagemarklogobyicon.markimageByicon (IconPath, SrcimgPath, TargetPath2, -45); } / *** เพิ่มลายน้ำลงในภาพ* @param ไอคอนพา ธ เส้นทางภาพลายน้ำ* @param srcimgpath เส้นทางภาพเส้นทางภาพ* @param targetpath เส้นทางเป้าหมายเส้นทางภาพ* / โมฆะสาธารณะคงที่ markimageByicon (ไอคอนสตริง, สตริง srcimgpath, สตริงเป้าหมาย) } / *** เพิ่มลายน้ำลงในภาพและตั้งมุมการหมุนของภาพลายน้ำ* @param ไอคอนพา ธ เส้นทางภาพลายน้ำ* @param srcimgpath เส้นทางที่มาของภาพ* @param เป้าหมายพา ธ เส้นทางเป้าหมาย* @param ระดับการหมุนภาพ ลอง {image srcimg = imageio.read (ไฟล์ใหม่ (srcimgpath)); bufferedImage buffimg = new bufferedImage (srcimg.getWidth (null), srcimg.getheight (null), bufferedimage.type_int_rgb); // รับวัตถุแปรง // กราฟิก g = buffimg.getGraphics (); graphics2d g = buffimg.creategraphics (); // ตั้งค่าการประมวลผลขอบ jagged ของส่วนบรรทัด 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 เพื่อให้ความโปร่งใสสามารถตั้งค่า imageicon imgicon = imageicon ใหม่ (iconpath); // รับวัตถุภาพ Image 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); // สร้าง image imageio.write (buffimg, "jpg", OS); System.out.println ("รูปภาพเพิ่มไอคอนซีลเสร็จแล้ว ... "); } catch (exception e) {e.printstacktrace (); } ในที่สุด {ลอง {ถ้า (null! = os) os.close (); } catch (exception e) {e.printstacktrace (); -ให้ฉันแบ่งปันตัวอย่างอื่นกับคุณ:
นำเข้า java.awt.color; นำเข้า java.awt.font; นำเข้า java.awt.graphics; นำเข้า java.awt.image; นำเข้า java.awt.image.bufferedimage; นำเข้า Java.io.file; com.sun.image.codec.jpeg.jpegcodec; นำเข้า com.sun.image.codec.jpeg.jpegimageencoder; ImageUtils ชั้นเรียนสุดท้ายสาธารณะ {Public ImageUtils () {}/** * สตริงคงสุดท้ายสาธารณะสุดท้าย getPressimgPath () {return ApplicationContext * .getRealAlpath ("/template/data/util/shuiyin.gif"); } *//** * พิมพ์ภาพบนภาพ * * @param pressimg - * ไฟล์ลายน้ำ * @param targetImg - * ไฟล์เป้าหมาย * @param x * --x พิกัด * @param y * --y พิกัด */ไฟล์ void filemg {try {try {try {try {try {try {try {try {try {try {try {try {try {try {try {try {try {try {try {try {try {try {try {treatile Image Src = imageio.read (_file); int wideth = src.getWidth (null); ความสูง int = src.getheight (null); BufferedImage Image = New BufferedImage (กว้าง, ความสูง, bufferedImage.type_int_rgb); กราฟิก g = image.createGraphics (); G.DrawImage (src, 0, 0, กว้าง, ความสูง, null); // ไฟล์ watermark ไฟล์ _filebiao = ไฟล์ใหม่ (pressimg); Image 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, (ความสูง - ความสูง _biao) / 2, wideth_biao, height_biao, null); // สิ้นสุดไฟล์ลายน้ำ G.Dispose (); fileOutputStream out = new fileOutputStream (targetImg); jpeGimageEncoder encoder = jpegCodec.createJPegenCoder (out); encoder.encode (รูปภาพ); out.close (); } catch (exception e) {e.printstacktrace (); }} / ** * พิมพ์ข้อความลายน้ำข้อความ * * @param presstext * -text * @param targetImg - * ภาพเป้าหมาย * @param fontname - * ชื่อตัวอักษร * @param fontstyle - * รูปแบบตัวอักษร * @param สี @param, @param, @parame fontname, int fontstyle, int สี, int fontsize, int x, int y) {ลอง {ไฟล์ _file = ไฟล์ใหม่ (targetImg); Image Src = imageio.read (_file); int wideth = src.getWidth (null); ความสูง int = src.getheight (null); BufferedImage Image = New BufferedImage (กว้าง, ความสูง, bufferedImage.type_int_rgb); กราฟิก g = image.createGraphics (); G.DrawImage (src, 0, 0, กว้าง, ความสูง, null); G.SetColor (color.Red); G.SetFont (ตัวอักษรใหม่ (FontName, Fontstyle, FontSize)); G.DrawString (PressText, Wideth - FontSize - X, ความสูง - FontSize / 2 - Y); G.Dispose (); fileOutputStream out = new fileOutputStream (targetImg); jpeGimageEncoder encoder = jpegCodec.createJPegenCoder (out); encoder.encode (รูปภาพ); out.close (); } catch (exception e) {system.out.println (e); }} โมฆะคงที่สาธารณะหลัก (สตริง [] args) {pressimage ("f: /logo.png", "f: /123.jpg", 0, 0); -ฉันหวังว่าบทความนี้จะเป็นประโยชน์สำหรับทุกคนในการใช้ภาษา Java เพื่อเพิ่มลายน้ำให้กับภาพ