この記事では、画像の透かし、テキスト透かし、モザイクを画像に追加するJava方法について説明します。次のように、参照のために共有してください。
Eclipseで新しいUtilsクラスを作成し、次のコードをコピーして直接使用できます。次の方法では、単一または複数の透かしの追加が実現します。
パッケージcom.rzxt.fyx.common.util; Import java.awt.alphacomposite; import java.awt.color; import java.awt.font; import java.awt.graphics2d;インポートjava.awt.image; java.io.file; Import javax.imageio.imageio;インポートjavax.swing.imageicon;/** *画像に透かしを追加 * @author tgy * */public class markimageutils {/** * @param args */public static void main(string [] {string output = "f:/images/" string source = "f:/images/6.jpg"; //ソース画像パス文字列ICON = "F:/IMAGES/ICON2.PNG"; //イメージパス文字列imagename = "mark_image"; //画像名String ImageType = "jpg"; //画像タイプjpg、jpeg、png、gif string text = "watermarked"; int size = 4; //モザイクサイズ整数度= null; //透かし回転角は-45です。NULLは、文字列結果を回転させないことを意味します= null; //画像に画像の透かしを追加してresult = markimageutils.markimagebomemoreicon(アイコン、ソース、出力、イメージネーム、イメージタイプ、程度); // result = markimageutils.markimagebysingleicon(アイコン、ソース、出力、イメージネーム、イメージタイプ、程度); //テキストの透かしを画像に追加する// result = markimageutils.markimagebymoretext(ソース、出力、イメージネーム、イメージタイプ、color.red、テキスト、程度); // result = markimagebysingletext(ソース、出力、イメージナメ、イメージタイプ、カラー、テキスト、程度); markimageutils.markimagebymosaic(ソース、出力、イメージネーム、イメージタイプ、サイズ); system.out.println(result); }/** *画像の異なる位置に複数の画像透かしを追加し、透かしのイメージの回転角を設定します * @paramアイコン透かしパス(f:/images/icon.pngなど) * @paramソースイメージパス(f:/images/6.jpgなど) AS:11111) * @Param ImageTypeイメージタイプ(jpgなど) * @param degureウォーターマーク画像の回転角度、nullは回転を意味しません */ public string string string icon、string source、string emagename、string imageType、integergedege) try {file file = new file(source);ファイルficon = new file(icon); if(!file.isfile()){return source + "image file!"; } //アイコンをメモリ画像にロードic = imageio.read(ficon); //アイコンの高さint iheight = ic.getheigh(null); //ソース画像をメモリ画像に読み取りますimg = imageio.read(file); //画像幅int width = img.getWidth(null); //画像高さint height = img.getheight(null); bufferedimage bi = new bufferedimage(幅、高さ、bufferedimage.type_int_rgb); //指定されたbufferedimageグラフィックス2d g = bi.creategraphics()を使用してグラフィックス2Dオブジェクトを作成します。 // x、y軸はデフォルトで0から開始されますint x = 0; int y = 0; // 2つのウォーターマーク画像の間隔の高さは、ウォーターマーク画像の1/3です。 int space = 1; if(height> = iheight){space = height/iheight; if(space> = 2){temp = y = iheight/2; if(space == 1 || space == 0){x = 0; y = 0; }}} else {x = 0; y = 0; } //ラインセグメントG.SetRenderingHintのジャグエッジ処理を設定します(renderinghints.key_interpolation、renderinghints.value_interpolation_bilinear); //画像を表し、G.drawimage(img.getscaledInstance(width、height、yimas.scale_smooth)、0,0、null)を描画する前に画像空間からユーザー空間に変換します。 for(int i = 0; i <space; i ++){if(null!= degute){//ウォーターマーク回転g.rotate(math.toradians(degree)、(double)bi.getwidth() / 2、(double)bi.getheigh() / 2); } //透かし画像のパスは一般にGIFまたはPNGであるため、透明性をImageIcon = new ImageIcon(Icon)に設定できます。 //画像オブジェクトを取得します。画像con = imgicon.getimage(); //透明性、最小値は0、最大値は1フロートクラリティ= 0.6Fです。 g.setComposite(alphacomposite.getInstance(alphacomposite.src_atop、clarity)); //ウォーターマーク画像の座標位置(x、y)//g.drawimage(con、300、220、null)を示します。 G.drawimage(con、x、y、null); g.setComposite(alphacomposite.getInstance(alphacomposite.src_over)); y+=(iCheight+temp); } g.dispose();ファイルsf = new File(output、imagename+"。"+imageType); Imageio.Write(BI、ImageType、SF); // image resultを保存= "画像が完成した後に透け式が追加されます"; } catch(Exception e){e.printstacktrace(); } return result; }/** *単一の画像透かしを画像に追加し、ウォーターマークイメージの回転角を設定します * @paramアイコンウォーターマークイメージパス(f:/images/icon.pngなど) * @paramソースイメージパス(f:/images/6.jpgなど) 11111) * @param imageTypeイメージタイプ(jpgなど) * @param度ウォーターマーク画像の回転角、nullは回転を意味しません */ public string string string icon、string icon、string output、string imagename、string imageType、integergeed) try {file file = new file(source);ファイルficon = new file(icon); if(!file.isfile()){return source + "image file!"; } //アイコンをメモリ画像にロードic = imageio.read(ficon); //アイコンの高さint iheight = ic.getheigh(null); //ソース画像をメモリ画像に読み取りますimg = imageio.read(file); //画像幅int width = img.getWidth(null); //画像高さint height = img.getheight(null); bufferedimage bi = new bufferedimage(幅、高さ、bufferedimage.type_int_rgb); //指定されたbufferedImageを使用してGraphics2Dオブジェクトを作成します。 graphics2d g = bi.creategraphics(); // x、y軸はデフォルトで0から開始されますint x = 0; int y =(height/2) - (iheight/2); //ラインセグメントG.SetRenderingHintのジャグエッジ処理を設定します(renderinghints.key_interpolation、renderinghints.value_interpolation_bilinear); //画像を表し、G.drawimage(img.getscaledInstance(width、height、yimas.scale_smooth)、0,0、null)を描画する前に画像空間からユーザー空間に変換します。 if(null!= degree){//透かし回転g.rotate(math.toradians(degree)、(double)bi.getWidth() / 2、(double)bi.getheigh() / 2)を設定します。 } //透かし画像のパスは一般にGIFまたはPNGであるため、透明性をImageCon = new ImageIcon(Icon)に設定できます。 //画像オブジェクトを取得します。画像con = imgicon.getimage(); //透明性、最小値は0、最大値は1フロートクラリティ= 0.6Fです。 g.setComposite(alphacomposite.getInstance(alphacomposite.src_atop、clarity)); //ウォーターマーク画像の座標位置(x、y)//g.drawimage(con、300、220、null)を示します。 G.drawimage(con、x、y、null); g.setComposite(alphacomposite.getInstance(alphacomposite.src_over)); g.dispose();ファイルsf = new File(output、imagename+"。"+imageType); Imageio.Write(BI、ImageType、SF); //画像を保存しますresult = "画像が完成した後に透かしが追加されます"; } catch(Exception e){e.printstacktrace(); } return result; }/** *複数のテキスト透かしを画像に追加し、透かしテキストの回転角度を設定します * @paramソース追加する必要がある画像パス(f:/images/6.jpgなど) @param色の透かしテキストの色* @param単語透かしテキスト* @param degute透かしテキストの回転角、つまり回転はありません*/ public static string string source source、string output、string imagename、string imageType、color coler、integer degree){sring result " try {//元の画像情報ファイル= new file(source); if(!file.isfile()){file + "Image Fileではない!"; } image img = imageio.read(file); //画像幅int width = img.getWidth(null); //画像高さint height = img.getheight(null); //テキストサイズint size = 50; //透かしの追加bufferedimage bi = new bufferedimage(幅、高さ、bufferedimage.type_int_rgb); graphics2d g = bi.creategraphics(); G.Drawimage(IMG、0、0、幅、高さ、ヌル); //ウォーターマークフォントスタイルフォントフォント=新しいフォント( "宋体"、font.plain、size); //画像の背景G.SetColor(色)に従って透かし色を設定します。 int x = width/3; int y = size; int space = height/size; for(int i = 0; i <space; i ++){//最後の座標のy軸が高さよりも高い場合、((y+size)> height){break; } if(null!= degree){//ウォーターマーク回転g.rotate(math.toradians(degree)、(double)bi.getWidth() / 2、(double)bi.getheigh() / 2)を設定します。 } g.setfont(font); //透かし位置G.DrawString(word、x、y); y+=(2*size); } g.dispose(); //出力画像ファイルsf = newファイル(output、imagename+"。"+imageType); Imageio.Write(BI、ImageType、SF); //画像結果を保存= "画像完了後に単語の透かしを追加します"; } catch(Exception e){e.printstacktrace(); } return result; }/** *画像に単一のテキスト透かしを追加し、透かしテキストの回転角度を設定します * @paramソース透かしで追加する必要がある画像パス(f:/images/6.jpgなど) jpg)* @param color透かしのテキストの色* @param word watermarkテキスト* @param degute透かしテキストの回転角、回転はないことを意味します*/ public static string source、string source、string imagename、string imageType、color、color、string word、integergeの学位) try {//元の画像情報ファイル= new file(source); if(!file.isfile()){file + "Image Fileではない!"; } image img = imageio.read(file); int width = img.getWidth(null); int height = img.getheight(null); //透かしの追加bufferedimage bi = new bufferedimage(幅、高さ、bufferedimage.type_int_rgb); graphics2d g = bi.creategraphics(); G.Drawimage(IMG、0、0、幅、高さ、ヌル); //ウォーターマークフォントスタイルフォントフォント= new Font( "宋体"、font.plain、50); //画像の背景G.SetColor(色)に従って透かし色を設定します。 if(null!= degree){//透かし回転g.rotate(math.toradians(degree)、(double)bi.getWidth() / 2、(double)bi.getheigh() / 2)を設定します。 } g.setfont(font); int x = width/3; int y = height/2; //透かし位置G.DrawString(word、x、y); g.dispose(); //出力画像ファイルsf = newファイル(output、imagename+"。"+imageType); Imageio.Write(BI、ImageType、SF); //画像結果を保存= "画像完了後に単語の透かしを追加します"; } catch(Exception e){e.printstacktrace(); } return result; }/** *モザイクを画像に追加 * @paramソースオリジナル画像パス(f:/images/6.jpgなど) *モザイン後の@param出力、画像を保存するパス(f:/image/など) * @param imageNameイメージ名(11111) * @param imageType zizeタイプなど各長方形 * @return */ public static string markimagebymosaic(string source、string output、string imagename、string imagetype、int size){string result = "画像モザイクエラーが発生しました"; try {file file = new file(source); if(!file.isfile()){file + "Image Fileではない!"; } bufferedimage img = imageio.read(file); //画像を読み取りますint width = img.getWidth(null); //元の画像幅int height = img.getheight(null); //元の画像高さbufferedimage bi = new bufferedimage(幅、高さ、bufferedimage.type_int_rgb); //モザイクグリッドサイズが大きすぎるか小さすぎる場合(幅<サイズ||高さ<サイズ){return "モザイクグリッドサイズが大きすぎる"; } if(size <= 0){return "モザイクグリッドのサイズが小さすぎる"; } int xcount = 0; // x方向に数値を描画しますint ycount = 0; // y方向に数値を描画します(width%size == 0){xcount = width /size; } else {xcount = width / size + 1; } if(height%size == 0){ycount = height / size; } else {ycount = height / size + 1; } int x = 0; // x座標int y = 0; // y coordinate //モザイクを描く(長方形と塗りつぶしの色を描く)グラフィック2d g = bi.creategraphics(); for(int i = 0; i <xcount; i ++){for(int j = 0; j <ycount; j ++){// mosaic rectangle size int mwidth = size; int mheight = size; if(i == xcount-1){//水平方向の最後のものは、1サイズのmwidth = width-xには十分ではありません。 } if(j == ycount-1){//垂直方向の最後のものは、1つのサイズmheight = height-yに十分ではありません。 } //長方形のRGB値は、中心ピクセルint centerx = x; int Centery = y; if(mwidth%2 == 0){centerx += mwidth / 2; } else {centerx +=(mwidth -1) / 2; } if(mheight%2 == 0){Centery += mheight / 2; } else {Centery +=(mheight -1) / 2; } color color = new Color(img.getRgb(CenterX、Centery)); G.SetColor(色); g.fillrect(x、y、mwidth、mheight); y = y + size; //次の長方形のy座標を計算} y = 0;ファイルsf = new File(output、imagename+"。"+imageType); Imageio.Write(BI、ImageType、SF); //画像結果を保存= "Mosaic Success"; } catch(Exception e){e.printstacktrace(); } return result; }}Java関連のコンテンツについては、このサイトに興味のある読者は、「Java画像操作スキルの要約」、「Javaの日付と時刻の操作スキルの概要」、「Java操作DOMノードスキルの概要」、Javaファイルの概要とディレクトリ操作スキルの概要」、「Javaデータ構造の概要」、およびAlgorithmm」を見ることができます。
この記事がみんなのJavaプログラミングに役立つことを願っています。