Artikel ini memperkenalkan kode contoh untuk menambahkan tanda air ke gambar Java. Sangat nyaman untuk mengimplementasikan tanda air di Java. Tanda air bisa berupa gambar atau teks. Konten spesifiknya adalah sebagai berikut
paket michael.io.image; impor java.awt.alphacomposite; impor java.awt.graphics2d; impor java.awt.image; impor java.awt.renderinghints; impor java.awt.image.bufferedimage; impor java.io.file; impor java.io.fileInputStream; impor java.io.fileoutputStream; impor java.io.inputstream; impor java.io.outputStream; impor javax.imageio.imageio; impor javax.swing.imageicon; impor com.sun.image.codec.jpeg.jpegcodec; impor com.sun.image.codec.jpeg.jpegimagedecoder; impor com.sun.image.codec.jpeg.jpegimageencoder; /** * Gambar watermark * @blog http://sjsky.iteye.com * @author michael */kelas publik Imagemarklogobyicon {/** * @param args */public static void main (string [] args) {string srcimgpath = "d: /test/ichael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/mpael/Pympat 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"; // Tambahkan Watermark ke gambar ImageMarkLogobyicon.markimagebyicon (IconPath, Srcimgpath, TargetPath); // Tambahkan tanda air ke gambar, rotasi watermark -45 ImageMarklogobyicon.markimagebyicon (IconPath, Srcimgpath, TargetPath2, -45); } /** * Add a watermark to the 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); } /** * Add a watermark to the image and set the rotation angle of the watermark image* @param iconPath Watermark image path* @param srcImgPath Source image path* @param targetPath Target image path* @param degree Watermark image rotation angle*/ public static void markImageByIcon(String iconPath, String srcImgPath, String targetPath, Integer degree) { OutputStream OS = null; coba {gambar srcimg = imageIo.read (file baru (srcimgpath)); BufferedImage buffimg = baru buferedImage (srcimg.getWidth (null), srcimg.getheight (null), bufferedimage.type_int_rgb); // Dapatkan Objek Kuas // Grafik G = BuffImg.GetGraphics (); Graphics2d G = BuffImg.Creategraphics (); // Atur pemrosesan tepi bergerigi dari segmen garis 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! = derajat) {// atur rotasi watermark g.rotate (math.toradians (derajat), (ganda) buffimg.getWidth () / 2, (ganda) buffimg .getheight () / 2); } // Jalur gambar tanda air umumnya adalah GIF atau PNG, sehingga transparansi dapat diatur Imageicon imgicon = Imageicon baru (IconPath); // Dapatkan objek gambar. Gambar img = imgicon.getImage (); float alpha = 0,5F; // transparansi g.setComposite (alphacomposite.getInstance (alphacomposite.src_atop, alpha)); // menunjukkan lokasi gambar tanda air G.Drawimage (IMG, 150, 300, null); g.setComposite (alphacomposite.getInstance (alphacomposite.src_over)); g.dispose (); OS = FileOutputStream baru (TargerPath); // menghasilkan gambar gambaro.write (buffimg, "jpg", os); System.out.println ("Gambar Selesai Menambahkan Segel Ikon ..."); } catch (Exception e) {E.PrintStackTrace (); } akhirnya {coba {if (null! = os) os.close (); } catch (Exception e) {E.PrintStackTrace (); }}}}Izinkan saya berbagi dengan Anda contoh lain:
impor java.awt.color; impor java.awt.font; impor java.awt.graphics; impor java.awt.image; impor java.awt.image.bufferedImage; impor java.io.file; import java.io.fileOutputputstream; impor javax.imageio; com.sun.image.codec.jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegimageencoder; Public Final Class ImageUtils {public imageUtils () {}/** * Publik final string statis getPressimgPath () {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); Image src = ImageIo.read (_file); int wideth = src.getWidth (null); int tinggi = src.getHeight (null); BufferedImage Image = BufferedImage baru (wideth, height, bufferedImage.type_int_rgb); Grafik g = image.creategraphics (); G.DrawImage (Src, 0, 0, Wideth, Height, Null); // file watermark _filebiao = file baru (pressimg); Gambar 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); // akhir file watermark g.dispose (); FileOutputStream out = FileOutputStream baru (targetIMG); Jpegimageencoder encoder = jpegcodec.createJpegencoder (out); encoder.encode (gambar); out.close (); } catch (Exception e) {E.PrintStackTrace (); }} / ** * Cetak gambar watermark teks * * @param pressText * --text * @param targetImg - * gambar target * @param fontname - * nama font * @param fontStyle - * font gaya * @parex - * font color * @param fontSize - * font ukuran * @param x -@parext * font @param @param @param @param - * font ukuran * @param x -@parexx * targetImg, string fontname, int fontstyle, int warna, int fontsize, int x, int y) {coba {file _file = file baru (targetImg); Image src = ImageIo.read (_file); int wideth = src.getWidth (null); int tinggi = src.getHeight (null); BufferedImage Image = BufferedImage baru (wideth, height, bufferedImage.type_int_rgb); Grafik g = image.creategraphics (); G.DrawImage (Src, 0, 0, Wideth, Height, Null); G.SetColor (Color.Red); g.setfont (font baru (fontname, fontstyle, fontsize)); g.drawstring (pressText, wideth - fontsize - x, height - fontsize / 2 - y); g.dispose (); FileOutputStream out = FileOutputStream baru (targetIMG); Jpegimageencoder encoder = jpegcodec.createJpegencoder (out); encoder.encode (gambar); out.close (); } catch (Exception e) {System.out.println (e); }} public static void main (string [] args) {pressimage ("f: /logo.png", "f: /123.jpg", 0, 0); }}Saya harap artikel ini akan membantu semua orang menggunakan bahasa Java untuk menambahkan tanda air ke gambar.