例は次のとおりです。
パッケージwriteimg; javax.imageio.imageioをインポートします。 java.awt.colorをインポートします。 java.awt.fontをインポートします。 java.awt.graphics2dをインポートします。 java.awt.image.bufferedimageをインポートします。 java.io.fileをインポートします。 java.io.ioexceptionをインポートします。 java.net.urlをインポートします。パブリッククラスPIC {プライベートフォントフォント= new Font( "Huawen Caiyun"、font.plain、40); // fontプロパティを追加してプライベートグラフィックス2d g = nullを設定します。 private int fontsize = 0;プライベートint x = 0;プライベートint y = 0; / ***ローカルイメージをバッファーにインポート*/ public bufferedimage loadimagelocal(string imgname){try {return imageio.read(new file(imgname)); } catch(ioException e){system.out.println(e.getmessage()); } nullを返します。 } / ***ネットワーク画像をバッファーにインポート* / public bufferedimage loadimageurl(string imgname){try {url url = new url(imgname); Imageio.read(url)を返します。 } catch(ioException e){system.out.println(e.getmessage()); } nullを返します。 } / ***ローカルに新しい画像を生成します* / public void writeimagelocal(string newimage、bufferedimage img){if(newimage!= null && img!= null){try {file outputfile = new File(newImage); Imageio.write(img、 "jpg"、outputfile); } catch(ioException e){system.out.println(e.getmessage()); }}}}} / ***テキストのフォントなどを設定* / public void setFont(string fontstyle、int fontsize){this.fontsize = fontsize; this.font = new font(fontstyle、font.plain、fontsize); } / ** *画像を変更して、変更された画像バッファー(テキスト出力の1行のみ)を返します * / public bufferedimage ModiyImage(bufferedimage IMG、Object content、int x、int y){try {int w = img.getWidth(); int h = img.getheigh(); g = img.creategraphics(); g.setbackground(color.white); g.setColor(color.orange); //フォントの色を設定if(this.font!= null)g.setfont(this.font); //(x> = h || y> = w){this.x = h -this.fontsize + 2; this.y = w; } else {this.x = x; this.y = y; } if(content!= null){g.drawstring(content.toString()、this.x、this.y); } g.dispose(); } catch(Exception e){System.out.println(e.getMessage()); } IMGを返します。 } /***画像を変更して、変更された画像バッファー(複数のテキストセグメントを出力)xoryを返します。 falseは、コンテンツを複数行に出力する*/ public bufferedimage Modiyimage(bufferedimage IMG、ObjectArr、int x、int y、boolean xory){try {int w = img.getWidth(); int h = img.getheigh(); g = img.creategraphics(); g.setbackground(color.white); g.setcolor(color.red); if(this.font!= null)g.setfont(this.font); //(x> = h || y> = w){this.x = h -this.fontsize + 2; this.y = w; } else {this.x = x; this.y = y; } if(contentArr!= null){int arrlen = contentArr.length; if(xory){for(int i = 0; i <arrlen; i ++){g.drawstring(contentArr [i] .toString()、this.x、this.y); this.x += contentArr [i] .toString()。length() * this.fontsize/ 2 +5; //テキスト出力位置を再計算}} else {for(int i = 0; i <arrlen; i ++){g.drawstring(contentArr [i] .toString()、this.x、this.y); this.y + = this.fontsize + 2; //テキスト出力位置を再計算}}} g.dispose(); } catch(Exception e){System.out.println(e.getMessage()); } IMGを返します。 } / ** *画像を変更して、変更された画像バッファーを返します(テキストの1行のみ) int h = img.getheigh(); g = img.creategraphics(); g.setbackground(color.white); g.setColor(color.blue); //フォントの色を設定if(this.font!= null)g.setfont(this.font); G.DrawString( "reyo.cn"、w -85、h -5); g.dispose(); } catch(Exception e){System.out.println(e.getMessage()); } IMGを返します。 } public bufferedimage modiyimagetogeter(bufferedimage b、bufferedimage d){try {int w = b.getwidth(); int h = b.getheight(); g = d.creategraphics(); G.drawimage(b、100、10、w、h、null); g.dispose(); } catch(Exception e){System.out.println(e.getMessage()); } return d; } public static void main(string [] args){pic tt = new pic(); bufferedimage d = tt.loadimagelocal( "d://11.jpg"); // bufferedimage b = tt // .loadimagelocal( "e:// file(word、excel、pdf、ppt.txt)// zte-logo.png"); tt.writeimagelocal( "d://cc.jpg"、tt.modifyimage(d、 "xichang apple"、90,90)//写真にファイルを書きます); //tt.writeimagelocal("d://cc.jpg "、tt.ModifyImagetogeter(b、d)); //複数の写真を一緒にグループ化system.out.println( "success"); }}上記は、写真にJava執筆の完全な内容と、メソッドを統合する2つの写真です。誰もがwulin.comをもっとサポートすることを願っています〜