今日、私はアップロードされた画像を透かしで埋める必要があることに遭遇しました。私は長い間オンラインで検索し、指定された場所に透かしを設定しました。次のコードは、オンラインで見つけたコードによって変換されました。最初にエフェクト画像を見てみましょう
実装方法については、以下で説明します。
ステップ1:PSまたはMeituソフトウェアを使用して、次のような透かしの写真を設計します。
ステップ2:次のコードをクラスに入れます。
Package org.platform.framework.commons.util; Import java.awt.alphacomposite; Import java.awt.color; Import java.awt.font; Import java.awt.graphics2d; Import java.awt.image; import java.awt.renderinghints; java.io.file; Import java.io.fileoutputStream; Import java.io.inputStream; Import java.outputStream; Import javax.imageio.imageio; import javax.swing.imageicon; Import com.founder.cms.watermark.util.utilate { / **透明透明性* / private static float alpha = 0.5f; / **透かし画像回転角*/プライベート静的二重度= 0f; private static int interval = 0; / ** *透かしパラメーターを設定し、設定せずにデフォルト値を使用 * * * @param alpha *透明透明 * @param学位 *ウォーターマーク画像回転角 * * @paramインターバル *ウォーターマーク画像インターバル */ public static void setimagemarkoptions(float alpha、int、int interval){if(alpha!= 0.0f){0.0f){0.0f) } if(degree!= 0f){imagemarkutil.degree = degute; } if(interval!= 0f){imagemarkutil.interval = interval; }} / ***透かし画像を画像に追加*** @param waterimgpath*ウォーターマーク画像パス* @param srcimgpath* @paramターゲットパス*ターゲット画像パス* / public static void watermarkbyimg(string waterimgpath、string srcimgpath、stringターゲットパス) } / ***透かし画像を画像に追加*** @param Waterimgpath*ウォーターマークイメージパス* @param Srcimgpath* @param Target Path*ターゲット画像パス* / public static void watermarkbyimg(string waterimgpath、string srcimgpath){try {waterimgpath、srcimgath、srcimgath、srcimpath、srcimgpath、srcimgpath、srcimgpath、srcimgpath、srcimgpath、srcimgpath、 } catch(例外e){// todo auto-enerated catch block e.printstacktrace(); }} / ***透かし画像を画像に追加し、透かしの画像の回転角を設定します** @param Waterimgpath*ウォーターマーク画像パス* @param Srcimgpath*ソース画像パス* @paramターゲットパス*ターゲット画像パスoutputStream os = null; 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(0!= degree){g.rotate(math.toradians(degree)、(double)buffimg.getWidth() / 2、(double)buffimg .getheigh() / 2); } // 4。透かし画像のパス透明イメージは一般にGIFまたはPNGであるため、透明度をImageIcon imgicon = new Imageicon(waterimgpath)に設定できます。 // 5。画像オブジェクトを取得します。画像img = imgicon.getimage(); g.setComposite(alphacomposite.getInstance(alphacomposite.src_atop、alpha)); // 6。透かし画像の位置は(int height = interval + imgicon.geticonheight(); height <buffimg .getheight(); height = height + interval + interval + imgicon.geticonheight())のためです。 imgicon.geticonwidth()){g.drawimage(img、weight -imgicon.geticonwidth()、height -imgicon.geticonheight()、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(); }}} public static void main(string [] args){system.out.println( "..ウォーターマーク画像の追加を開始..."); /***ウォーターマークパスウォーターマーク画像アドレスアップロードアップロードアップロード後のアップロードファイルアドレス*///デフォルトパラメーターの変更//imagemarkutil.watermarkbyimg(watermarkpath、uploadpath); system.out.println( "..透かし式画像を終了します..."); }}ステップ3:アップロードが成功する場合、次のようなクラスに電話してください。
/ *** WatermarkPath Watermark Image Address*アップロードの成功後のアップロードファイルアドレス*/ imagemarkutil.watermarkbyimg(watermarkpath、uploadpath);ウォーターマーク間隔が近すぎると思われる場合は、次のコードを介して間隔を設定できます。 imagemarkutil.watermarkbyimg(watermarkpath、uploadpath);
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。