이 기사는 Java 사진에 워터 마크를 추가하기위한 예제 코드를 소개합니다. 자바에서 워터 마크를 구현하는 것이 매우 편리합니다. 워터 마크는 그림이나 텍스트 일 수 있습니다. 특정 내용은 다음과 같습니다
패키지 michael.io.image; java.awt.alphacomposite import; import java.awt.graphics2d; import java.awt.image; Java.awt.RenderingHints 가져 오기; java.awt.image.bufferedimage import; import java.io.file; import java.io.fileInputStream; import 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; /** * 이미지 워터 마크 * @Blog http://sjsky.itey.com * @Author Michael */public class imageMarkLogoByicon {/** * @param args */public static void main (String [] args) {String srcimgpath = "d : /test/michael/myblog_01.png"; 문자열 iconpath = "d : /test/michael/blog_logo.png"; 문자열 targetpath = "d : /test/michael/img_mark_icon.jpg"; 문자열 targetpath2 = "d : /test/michael/img_mark_icon_rotate.jpg"; // 이미지에 WaterMark 추가 imageMarkLogoByicon.MarkImageByicon (IconPath, SrcImgPath, TargetPath); // 이미지에 WaterMark 추가, WaterMark Rotation -45 ImageMarkLogobyicon.MarkImageByicon (IconPath, SrcimgPath, TargetPath2, -45); } / *** 이미지에 워터 마크 추가* @param iConpath Watermark 이미지 경로* @param srcimgpath 소스 이미지 경로* @param targetpath 이미지 이미지 경로* / public static void MarkimageByicon (문자열 ICONPATH, String SRCIMGPATH, String TargetPath) {MarkImageByicon (iconpath, srcimgpath, target, null); } / *** 이미지에 워터 마크를 추가하고 워터 마크 이미지의 회전 각도를 설정* @param iConpath 워터 마크 이미지 경로* @param strcimgpath 이미지 경로* @param targetpath target image path* @param degree watermark 이미지 회전 각도* / public static void markimagebyicon (문자열 iconpath, strcimg path, string target as. try {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 (); // 라인 세그먼트 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! = dec } // 워터 마크 이미지의 경로는 일반적으로 GIF 또는 PNG이므로 투명성을 설정할 수 있습니다. ImageIcon 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); // image imageio.write 생성 (buffimg, "jpg", os); System.out.println ( "이미지 완성 아이콘 씰 추가 ..."); } catch (예외 e) {e.printstacktrace (); } 마침내 {try {if (null! = os) os.close (); } catch (예외 e) {e.printstacktrace (); }}}}또 다른 예를 여러분과 공유하겠습니다.
import 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.fileoutputstream; 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"); . image src = imageio.read (_file); int wideth = src.getWidth (null); int height = src.getheight (null); BufferedImage Image = New BufferedImage (wideth, height, bufferedimage.type_int_rgb); 그래픽 g = image.creategraphics (); G.DrawImage (SRC, 0, 0, wideth, 높이, NULL); // 워터 마크 파일 파일 _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, (Height -Height_biao) / 2, wideth_biao, height_biao, null); // 워터 마크 파일의 끝 g.dispose (); fileoutputStream out = 새 FileOutputStream (TargetImg); jpegimageEncoder encoder = jpegcodec.createjpegencoder (out); encoder.encode (이미지); out.close (); } catch (예외 e) {e.printstacktrace (); }} / ** * 인쇄 텍스트 워터 마크 이미지 * * @param presstext * --text * @param targetimg - * target image * @param fontname - * font name * @param fontstyle - * font style * @param color - * font color * @poram fontsize- * font size * @param x - * public void void void presstex. TargetImg, String fontName, int fontStyle, int color, int fontsize, int x, int y) {try {file _file = new File (targetImg); image src = imageio.read (_file); int wideth = src.getWidth (null); int height = src.getheight (null); BufferedImage Image = New BufferedImage (wideth, height, bufferedimage.type_int_rgb); 그래픽 g = image.creategraphics (); G.DrawImage (SRC, 0, 0, wideth, 높이, NULL); g.setcolor (color.red); G. 세트 폰트 (New Font (FontName, FontStyle, fontsize)); G.DrawString (pressText, wideth -fontsize -x, 높이 -Fontsize / 2 -y); g.dispose (); fileoutputStream out = 새 FileOutputStream (TargetImg); jpegimageEncoder encoder = jpegcodec.createjpegencoder (out); encoder.encode (이미지); out.close (); } catch (예외 e) {System.out.println (e); }} public static void main (String [] args) {pressImage ( "f : /logo.png", "f : /123.jpg", 0, 0); }}이 기사가 모든 사람이 Java 언어를 사용하여 이미지에 워터 마크를 추가하는 데 도움이되기를 바랍니다.