この2日間、私は自分のニーズに応じて画像をアップロードしてきました。正直に言うと、Javaがこのように動作できることはわかりません。要件があるので、私は情報を見つけに行き、それを研究しました。私は今それを共有し、困っている兄弟を助けることを望んでいます。
通常の写真に透かしを追加することは、アニメーションに透かしを追加することとは異なります。通常の写真に透かしを追加することは、Java独自の方法で書かれています。 GIF4Jフレームワークは、アニメーションに使用されます。 CSDNでダウンロードできる多くのフレームワークがあります。元のJARパッケージには使用できない透かしがあるため、ひびの入ったバージョンをダウンロードすることをお勧めします。
java.awt。*; java.awt.image.bufferedimageをインポートします。 java.io.*をインポートします。 javax.imageio.imageioをインポートします。 javax.swing.imageiconをインポートします。 //これは、gif4jフレームワークのインポートcom.gif4j.gifdecoderのクラスです。 com.gif4j.gifencoderをインポートします。 com.gif4j.gifimageをインポートします。 com.gif4j.giftransformerをインポートします。 com.gif4j.textpainterをインポートします。 com.gif4j.watermarkをインポートします。 /*** 2018/1/18にZXDによって作成されました。 */ public class imageRemarkutil {//透明な透明なプライベートフロートアルファ= 0.5F; //ウォーターマーク水平位置private int positionwidth = 150; //透かし垂直位置private int positionheight = 300; //透かし幅private int width = 80; //透かし式ハイプライベートINT高さ= 80; //ウォーターマークテキストフォントプライベートフォントフォント= new Font( "宋体"、font.bold、72); //透かしテキストカラープライベートカラー= color.red; / *************************透かし付き通常の画像***********// *****@Param Alpha*透けて透明性*@param PositionWidth*透かし式水平位置*ウォーターマーク垂直*@param font*ウォーターマークテキスト*/パブリックボイドカラー*/パブリックボイドカラー* positionWidth、int positionheight、int width、int height、font font、color Color){if(alpha!= 0.0f)this.alpha = alpha; if(positionwidth!= 0)this.positionWidth = positionWidth; if(positionheight!= 0)this.positionheight = positionheight; if(height!= 0)this.height = height; if(width!= 0)this.width = width; if(font!= null)this.font = font; if(color!= null)this.color = color; } / ** *透かし画像を画像に追加 * * * @param Iconpath *透かし画像パス * @param Srcimgpath *ソース画像パス * @paramターゲットパス *ターゲット画像パス * / public void markimagebyicon(string srcimgpath、string target path){markimagebyicon(srcimgath、srcimgath、srcimgath、srcimgpath、srcimgpath、srcimgpath、srcimgpath、 } / ***透かし画像を画像に追加し、透かしの画像の回転角を設定します** @param Iconpath*透かし画像パス* @param Srcimgpath*ソース画像パス* @paramターゲットパス*ターゲット画像パス*透かし画像ローテーション角* /パブリックボイドマルチマージビーコン(String srcimgpath、integer、srcimgpath、srcimgpath、srcimgpath、ヌル; try {image srcimg = imageio.read(new file(srcimgpath)); bufferedimage buffimg = new bufferedimage(srcimg.getWidth(null)、srcimg.getheight(null)、bufferedimage.type_int_rgb); // 1。ブラシオブジェクトを取得グラフィックス2dg = 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。透かし回転を設定するif(null!= degree){g.rotate(math.toradians(degree)、(double)buffimg.getWidth() / 2、(double)buffimg.getheigh() / 2); } // 4。透かし画像のパス透明イメージは一般にGIFまたはPNGであるため、透明度をImageIcon imgicon = new ImageIcon(IconPath)に設定できます。 // 5。画像オブジェクトを取得します。画像img = imgicon.getimage(); g.setComposite(alphacomposite.getInstance(alphacomposite.src_atop、alpha)); integer x = srcimg.getWidth(null); integer y = srcimg.getheight(null); //6。透かし画像の位置G.drawimage(IMG、X-(positionWidth+width)、y-(positionheight+height)、width、height、null); g.setComposite(alphacomposite.getInstance(alphacomposite.src_over)); //7。リソースG.Dispose()をリリースします。 // 8。画像を生成os = new fileoutputStream(targerPath); Imageio.write(buffimg、 "jpg"、os); system.out.println( "画像完了ウォーターマークイメージを完了します"); } catch(Exception e){e.printstacktrace(); }最後に{try {if(null!= os)os.close(); } catch(Exception e){e.printstacktrace(); }}}} / ** *透かしテキストを画像に追加 * * * @param logotext *透かし * @param srcimgpath *ソース画像パス * @paramターゲットパス *ターゲットイメージパス * /パブリックボイドMarkimageBytext(String logoText、String Srcimgpath、String Target Path) } / ** *透かしテキストを画像に追加し、透かしテキストの回転角を設定します * * @param logotext * @param logotext * @param srcimgpath * @paramターゲットパス * / public void markimagebytext(string logotext、string srcimgpath、string target -degree、integer is = null; outputStream os = null; try {// 1。ソース画像srcimg = imageio.read(new file(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。透かし回転を設定するif(null!= degree){g.rotate(math.toradians(degree)、(double)buffimg.getWidth() / 2、(double)buffimg.getheigh() / 2); } //5。透かしテキストの色G.SetColor(色)を設定します。 //6。透かしテキストフォントg.setfont(font); //7。透かし透明度G.SetComposite(alphacomposite.getInstance(alphacomposite.src_atop、alpha))を設定します。 //8。最初のパラメーター - >設定コンテンツ、次の2つのパラメーター - >画像上のテキストの位置を調整する(x、y)g.drawstring(logotext、positionwidth、positionheight); //9。リソースG.Dispose()をリリースします。 // 10。画像を生成os = new fileoutputStream(targerPath); Imageio.write(buffimg、 "jpg"、os); system.out.println( "画像完了透かしテキスト"); } catch(Exception e){e.printstacktrace(); }最後に{try {if(null!= is)is.close(); } catch(Exception e){e.printstacktrace(); } try {if(null!= os)os.close(); } catch(Exception e){e.printstacktrace(); }} / *********************アニメーションイメージは透かし式**********************GIFイメージをスケーリングし、ファイルファイルを直接渡す、幅と高さを直接渡すと、[ファイルSRC、ファイルデスティ、幅、幅、int width、int height)をスローすることができます。 gifdecoder.decode(src); // gifimageオブジェクトを作成します。 gifimage resizeimg = giftransformer.resize(gifimage、width、height、true); gifencoder.encode(resizeimg、dest); } // GIFイメージをスケーリングし、ファイルパスを直接渡し、幅と高さのパブリックボイドMakeGif(String Src、String Dest、int width、int height)を設定します。 makegif(new File(SRC)、new File(dest)、gifimage.getScreenWidth() / 2、gifimage.getScreenHeigh() / 2); } // gif画像のスケーリング、ファイルファイル、幅、高さを渡すことはできませんpublic void makegif(file src、file dest)throws ioexception {gifimage gifimage = gifdecoder.decode(src); // gifimageオブジェクトを作成します。 makegif(src、dest、gifimage.getscreenwidth() / 2、gifimage.getscreenheight() / 2); } // gifイメージをスケーリングし、ファイルパスを通過し、幅と高さを設定しないpublic void makegif(string src、string dest)throws ioexception {makegif(new file(src)、new file(dest)); } /***アニメーションにテキスト透かしを追加* /public void addtextwatermarktogif(file src、string watermarktext、file dest)throws ioexception {//ウォーターマークの初期化と設定(フォント、スタイル、サイズ、色)textpainter textpainter = new textpainter(new font( "bold、12); textPainter.setOutlinePaint(color.white); bufferedimage renderedwatermarktext = textPainter.RenderString(waterMarkText、true); //画像オブジェクトgifimage gf = gifdecoder.decode(src); //画像サイズを取得int iw = gf.getScreenWidth(); int ih = gf.getscreenheight(); //ウォーターマークサイズを取得int tw = renderedwatermarktext.getWidth(); int th = renderedwatermarktext.getheight(); //ウォーターマークポイントP = new Point(); px = iw -tw -5; py = ih -th -4; //ウォーターマークウォーターマークウォーターマーク=新しい透かし(renderedwatermarktext、p); gf = watermark.apply(gifdecoder.decode(src)、true); //出力gifencoder.encode(gf、dest); } / ***アニメーションに画像の透かしを追加* / public void addimagewatermarktogif(file 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 = renderedwatermarktext.getWidth(); int th = renderedwatermarktext.getheight(); //ウォーターマーク位置ポイントP = new Point(); PX = IW-TW-20; py = ih-th-20; //ウォーターマークウォーターマークウォーターマーク=新しいウォーターマーク(renderedwatermarktext、p); //ウォーターマーク透明性Watermark.setTransparency(1); gf = watermark.apply(gifdecoder.decode(src)、false); //出力gifencoder.encode(gf、dest); } catch(ioexception e){e.printstacktrace(); }} public static void main(string [] args){//画像を透かし式にする必要がある場所string srcimgpath = "d:/1.jpg";文字列logotext = "copy invalid"; //画像のパス透かし文字列iconpath = "d:/2.jpg"; //ウォーターマークファイルの出力パス文字列ターゲットテキストパス= "d:/qie_text.jpg"; string targetTextPath2 = "d:/qie_text_rotate.jpg"; string tageticOnpath = "d:/qie_icon.jpg"; string tageticonpath2 = "d:/qie_icon_rotate.jpg"; System.out.println( "画像に透かしテキストの追加を開始する..."); //画像ウォーターマークテキストMarkimageByText(logoText、srcimgpath、ターゲットテキストパス); //透かしテキストを画像に追加する、透かしテキストの回転-45 MarkimageByText(logoText、srcimgpath、targetTextPath2、-45); System.out.println( "透かしテキストを画像に追加、終了..."); system.out.println( "透かしを画像に追加して、開始..."); setimagemarkoptions(0.3f、1、1、null、null); //透かし画像を画像に追加すると、透かし画像回転-45 MarkimageByicon(Iconpath、Srcimgpath、TargeticOnpath2、-45); System.out.println( "画像終了に透かしを追加..."); //アニメーション画像に透かしを追加します(透かし式アニメーションファイルを追加し、透かしを追加し、出力ファイルを追加)addTextWaterMarkTogif( "d://10.gif")、 "copy invalid"、new file( "d://11.gif")); addimagewatermarktogif(new file( "d://gif//10.gif")、 "d://gif//3.png"、new file( "d://gif//4.gif")); }}通常の写真に透かしを追加し、アニメーションに透かしを追加する方法は次のとおりです。通常の写真に透かしを追加する方法が、画像が送信された場合に正常に追加できる場合、画像は静的になります。アニメーションに透かしを追加する方法が送信されると、エラーが直接報告されます。
私はそれらをやっていたときにこれらを試しましたので、私は今ここでメモを取って録音します。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。