이틀 동안, 나는 내 필요에 따라 이미지를 업로드하고 있습니다. 솔직히 말해서, 나는 Java가 이런 식으로 작동 할 수 있다는 것을 모른다. 요구 사항이 있기 때문에 정보를 찾아서 연구했습니다. 도움이 필요한 형제들을 돕기를 바라면서 지금 공유하겠습니다.
일반 사진에 워터 마크를 추가하는 것은 워터 마크 추가와 애니메이션에 다릅니다. 일반 사진에 워터 마크를 추가하는 것은 Java의 자체 방법으로 작성됩니다. GIF4J 프레임 워크는 애니메이션에 사용됩니다. CSDN에서 다운로드 할 수있는 많은 프레임 워크가 있습니다. 원래 JAR 패키지에는 사용할 수없는 워터 마크가 있기 때문에 Cracked 버전을 다운로드하는 것이 좋습니다.
java.awt.*; java.awt.image.bufferedimage import; java.io.*; import javax.imageio.imageio; import javax.swing.imageicon; // 이것은 gif4j 프레임 워크의 클래스 import com.gif4j.gifdecoder; com.gif4j.gifencoder 가져 오기; import com.gif4j.gifimage; import com.gif4j.giftransformer; import com.gif4j.textPainter; com.gif4j.watermark import; /*** ZXD가 2018/1/18에 작성했습니다. */ public class imageRemarkutil {// Watermark Transparency private float alpha = 0.5f; // 워터 마크 수평 위치 private int positionWidth = 150; // Watermark 수직 위치 private int positionHeight = 300; // 워터 마크 너비 비공개 int 너비 = 80; // Watermark 높은 개인 int 높이 = 80; // 워터 마크 텍스트 글꼴 개인 글꼴 글꼴 = 새 글꼴 ( "宋体", font.bold, 72); // 워터 마크 텍스트 색상 비공개 색상 = 색상 .red; / ********************* Watermark와 함께 일반 이미지 ********** ** ** ***@param alpha*watermark transparency*@param positionwidth*watermark 수평 위치*@param positionheight*수직 위치*@param font*watermark 텍스트*@param 텍스트 색상*/ public void setimagemarkmarkmarkmarkmarkome PositionHeight, int 너비, int 높이, 글꼴, 색상) {if (alpha! = 0.0f) this.alpha = alpha; if (positionWidth! = 0) this.PositionWidth = positionWidth; if (positionHeight! = 0) this.PositionHeight = positionHeight; if (높이! = 0) this.height = 높이; if (너비! = 0) this.width = 너비; if (font! = null) this.font = font; if (color! = null) this.color = color; } / ** * 이미지에 워터 마크 이미지 추가 * * @param iConpath * 워터 마크 이미지 경로 * @param srcimgpath * 소스 이미지 경로 * @param targetpath * target image path * / public void markimagebyicon (문자열 아이콘 경로, 문자열 srcimgpath) {markImageByicon (iconagepath, srcimg path, srcimg path, srcimg path, srcimg path); } / *** 이미지에 워터 마크 이미지를 추가하고 워터 마크 이미지의 회전 각도를 설정** @param iconpath* 워터 마크 이미지 경로* @param srcimgpath* 소스 이미지 경로* @param targetpath* 대상 이미지 경로* @param degreat* watermark rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation rotation markimagebyicon (string iconpath, string targettram, string targertrestrect). = null; try {image srcimg = imageio.read (새 파일 (srcimgpath)); bufferedImage buffimg = new bufferedImage (srcimg.getWidth (null), srcimg.getheight (null), bufferedImage.type_int_rgb); // 1. 브러시 개체를 가져옵니다. graphics2d g = buffimg.creategraphics (); // 2. 라인 세그먼트 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); // 3. (null! = dec } // 4. 워터 마크 이미지의 경로는 워터 마크 이미지의 경로는 일반적으로 GIF 또는 PNG이므로 투명도가 ImageICon Imgicon = New ImageIcon (iconpath)을 설정할 수 있습니다. // 5. 이미지 객체를 가져옵니다. 이미지 img = imgicon.getimage (); g.setcomposite (alphacomposite.getinstance (alphacomposite.src_atop, alpha)); 정수 x = srcimg.getWidth (null); 정수 y = srcimg.getheight (null); // 6. 워터 마크 이미지의 위치 G.DrawImage (img, x- (positionWidth+너비), y- (위치 하이이트+높이), 너비, 높이, 널); g.setcomposite (alphacomposite.getinstance (alphacomposite.src_over)); // 7. 리소스 g.dispose (); // 8. 이미지 생성 os = 새 파일 아웃 PutStream (TargerPath); imageio.write (buffimg, "jpg", os); System.out.println ( "이미지 완료 완전한 WaterMark 이미지 추가"); } catch (예외 e) {e.printstacktrace (); } 마침내 {try {if (null! = os) os.close (); } catch (예외 e) {e.printstacktrace (); }}}} / ** * 이미지에 워터 마크 텍스트 추가 * * @param logotext * 워터 마크 텍스트 * @param srcimgpath * 소스 이미지 경로 * @param targetpath * / public void markimageBytext (String srcimgpath, String targetpath) {markImageBytext, srcimgPath, srcimgPath); } / ** * 이미지에 워터 마크 텍스트를 추가하고 워터 마크 텍스트의 회전 각도를 설정하십시오 * @param logotext * @param logotext * @param srcimgpath * @param targetpath * / public void markimagebytext (String logotext, string srcimgpath, string strage, intger decture) {inputstream is = null; OutputStream os = null; {// 1. 소스 이미지 이미지 srcimg = imageio.read (새 파일 (srcimgpath)); bufferedImage buffimg = new bufferedImage (srcimg.getWidth (null), srcimg.getheight (null), bufferedImage.type_int_rgb); // 2. 브러시 객체 graphics2d g = buffimg.creategraphics ()를 가져옵니다. // 3. 라인 세그먼트 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); // 4. (null! = dec } // 5. 워터 마크 텍스트 색상 G.SetColor (색); // 6. 워터 마크 텍스트 글꼴 G.SetFont (FONT); // 7. 워터 마크 텍스트 투명성 G.SetComposite (alphacomposite.getInstance (alphacomposite.src_atop, alpha)); // 8. 첫 번째 매개 변수 -> 컨텐츠를 설정합니다. 다음 두 매개 변수 -> 그림에서 텍스트의 좌표 위치 (x, y) G.DrawString (logotext, positionWidth, positionHeight); // 9. 자원 g.dispose (); // 10. 이미지 생성 os = new FileOutputStream (TargerPath); imageio.write (buffimg, "jpg", os); System.out.println ( "이미지 완료 완전한 WaterMark 텍스트 추가"); } catch (예외 e) {e.printstacktrace (); } 마침내 {try {if (null! = is) is.close (); } catch (예외 e) {e.printstacktrace (); } try {if (null! = os) os.close (); } catch (예외 e) {e.printstacktrace (); }}}} / *********************** 애니메이션 이미지는 물 마크로 표시되어 ****************** / / ***GIF 이미지를 직접 전달하고 파일 파일을 직접 전달하면 파일 및 높이* / public void makeGif (파일 SRC, 파일 DEST, INT 높이, int) {gifimage gifimage =를 설정할 수 있습니다. gifdecoder.decode (src); // gifimage 객체를 만듭니다. gifimage resizeimg = giftransformer.resize (gifimage, 너비, 높이, 참); gifencoder.encode (resizeimg, dest); } // gif 이미지를 스케일하고 파일 경로를 직접 전달하고 너비와 높이 공개 void makegif (String Src, String Dest, int width, int height)를 ioexception {gifimage gifimage = gifdecoder.decode (새 파일 (src)); // gifimage 객체를 만듭니다. makeGif (새 파일 (SRC), 새 파일 (dest), gifimage.getScreenWidth () / 2, gifimage.getScreenHeight () / 2); } // gif 이미지 스케일링, 파일 파일 전달 파일, 너비 및 높이를 설정할 수 없습니다. public void makegif (파일 SRC, 파일 dest)는 ioexception {gifimage gifimage = gifdecoder.decode (src); // gifimage 객체를 만듭니다. makegif (src, dest, gifimage.getScreenWidth () / 2, gifimage.getScreenHeight () / 2); } // gif 이미지 스케일링, 파일 경로 전달, 너비와 높이 공개 void makegif (문자열 src, string dest)를 설정하지 않습니다. ioexception {makegif (src), 새 파일 (dest)); } /*** 애니메이션에 텍스트 watermark 추가* /public void addTextWaterMarkTogif (파일 SRC, String WaterMarkText, File Dest)는 ioException {// WaterMark 초기화 및 설정 (글꼴, 스타일, 크기, 색상) TextPainter = New TextPainter ( "Bold", font.10); TextPainter.setoutLlinePaint (Color.white); BufferedImage RenderWaterMarkText = TextPainter.Renderstring (WaterMarkText, True); // 이미지 객체 gifimage gf = gifdecoder.decode (src); // 이미지 크기를 가져옵니다 int iw = gf.getScreenWidth (); int ih = gf.getscreenheight (); // 워터 마크 크기를 가져옵니다 int tw = RenderWaterMarkText.getWidth (); int th = renderedWaterMarkText.getheight (); // WaterMark Point P = New Point (); px = iw -tw -5; py = ih -th -4; // WaterMark WaterMark WaterMark = New WaterMark (RenderWaterMarkText, P); gf = watermark.apply (gifdecoder.decode (src), true); // output gifencoder.encode (gf, dest); } / *** 애니메이션에서 이미지 워터 마크 추가* / public void addimageWaterMarkTogif (파일 SRC, String WaterMarkPath, File Dest) {try {bufferedImage renderedWaterMarkText = imageIO.Read (새 파일 (WaterMarkPath)); // 이미지 객체 gifimage gf = gifdecoder.decode (src); // 이미지 크기를 가져옵니다 int iw = gf.getScreenWidth (); int ih = gf.getscreenheight (); // 워터 마크 크기를 가져옵니다 int tw = RenderWaterMarkText.getWidth (); int th = renderedWaterMarkText.getheight (); // 워터 마크 위치 포인트 p = new Point (); px = IW-TW-20; py = IH-TH-20; // WaterMark WaterMark WaterMark = New WaterMark (RenderWaterMarkText, P); // Watermark Transparency Watermark.setTransparency (1); gf = watermark.apply (gifdecoder.decode (src), false); // output gifencoder.encode (gf, dest); } catch (ioexception e) {e.printstacktrace (); }} public static void main (string [] args) {// 이미지가 워터 마크로 필요한 경로 srcimgpath = "d : /1.jpg"; 문자열 logotext = "copy invalid"; // 이미지의 경로 Watermark String iconpath = "d : /2.jpg"; // 워터 마크 파일의 출력 경로 문자열 targettextPath = "d :/Qie_Text.jpg"; 문자열 targettextpath2 = "d : /qie_text_rotate.jpg"; String targeticonpath = "d : /qie_icon.jpg"; 문자열 tartomiconpath2 = "d : /qie_icon_rotate.jpg"; System.out.println ( "이미지에 워터 마크 텍스트를 추가하기 시작합니다 ..."); // 이미지 워터 마크 텍스트 MarkImageBytext (logotext, srcimgpath, targettextPath); // 이미지에 워터 마크 텍스트 추가, 워터 마크 텍스트 회전 -45 MarkImageBytext (logotext, srcimgpath, targettextPath2, -45); System.out.println ( "이미지에 워터 마크 텍스트 추가, 끝 ..."); System.out.println ( "이미지에 워터 마크 이미지 추가, 시작 ..."); setimagemarkoptions (0.3f, 1, 1, null, null); // 이미지에 WaterMark 이미지 추가, WaterMark 이미지 회전 -45 MarkimageByicon (IconPath, SRCIMGPATH, TORMECICONPATH2, -45); System.out.println ( "이미지 끝에 워터 마크 추가 ..."); // 애니메이션 이미지에서 WaterMark 추가 (워터 마크 애니메이션 파일 추가, 워터 마크 추가, 출력 파일 추가) AddTextWaterMarkTogif (새 파일 ( "d : //10.gif"), "copy invalid", 새 파일 ( "d : //11.gif"); addImageWaterMarkTogif (새 파일 ( "d : //gif//10.gif"), "d : //gif//3.png", 새 파일 ( "d : //gif//4.gif"); }}다음은 일반 사진에 워터 마크를 추가하고 애니메이션에 워터 마크를 추가하는 방법입니다. 이미지가 전송되면 일반 그림에 워터 마크를 추가하는 방법을 성공적으로 추가 할 수 있다면 이미지가 정적이됩니다. 애니메이션에 워터 마크를 추가하는 방법이 전송되면 오류가 직접보고됩니다.
나는 그들을 할 때 이것을 시도 했으므로 지금 메모를하고 지금 여기에 기록 할 것이며, 그들이 도움이 필요한 형제들을 도울 수 있기를 바랍니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.