Contohnya adalah sebagai berikut:
paket writeimg; impor javax.imageio.imageio; impor java.awt.color; impor java.awt.font; impor java.awt.graphics2d; impor java.awt.image.bufferedimage; impor java.io.file; impor java.io.ioException; impor java.net.url; pic kelas publik {private font font = font baru ("huawen caiyun", font.plain, 40); // tambahkan properti font untuk mengatur grafis pribadi2d g = null; private int fontSize = 0; private int x = 0; private int y = 0; / *** Impor Gambar Lokal ke Buffer*/ Public BufferedImage LoadImagelocal (String IMGNAME) {coba {return Imageo.read (File baru (imGname)); } catch (ioException e) {System.out.println (e.getMessage ()); } return null; } / *** Impor gambar jaringan ke buffer* / public BufferedImage loadImageUrl (string imGname) {coba {url url = URL baru (imGname); return imageo.read (url); } catch (ioException e) {System.out.println (e.getMessage ()); } return null; } / *** Hasilkan gambar baru ke lokal* / public void writeMagelocal (string newImage, bufferedImage img) {if (newImage! = Null && img! = Null) {coba {file outputFile = file baru (newImage); Imageo.write (img, "jpg", outputFile); } catch (ioException e) {System.out.println (e.getMessage ()); }}}} / *** Setel font, dll. Dari teks* / public void setFont (String fontstyle, int fontSize) {this.fontsize = fontSize; this.font = font baru (fontstyle, font.plain, fontsize); } / ** * Ubah gambar dan kembalikan buffer gambar yang dimodifikasi (hanya output satu baris teks) * / public BufferedImage ModifyImage (bufferedImage IMG, konten objek, int x, int y) {coba {int w = img.getWidth (); int h = img.getHeight (); g = img.creategraphics (); G.SetBackground (Color.White); g.setColor (color.orange); // atur warna font if (this.font! = null) g.setfont (this.font); // Verifikasi koordinat ordinat dan horizontal dari posisi output if (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 ()); } return img; } /*** Ubah gambar dan kembalikan buffer gambar yang dimodifikasi (output beberapa segmen teks) xory: true berarti output konten dalam satu baris; false berarti output Konten beberapa baris*/ public bufferedImage ModifyImage (bufferedImage IMG, objek [] contentArr, int x, int y, boolean xory) {coba {int w = img.getWidth (); int h = img.getHeight (); g = img.creategraphics (); G.SetBackground (Color.White); G.SetColor (Color.Red); if (this.font! = null) g.setfont (this.font); // Verifikasi koordinat ordinat dan horizontal dari posisi output if (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; // menghitung ulang posisi output teks}} else {for (int i = 0; i <arrlen; i ++) {g.drawstring (contentArr [i] .toString (), this.x.x. this.y + = this.fontsize + 2; // menghitung ulang posisi output teks}}} g.dispose (); } catch (Exception e) {System.out.println (e.getMessage ()); } return img; } / ** * Ubah gambar dan kembalikan buffer gambar yang dimodifikasi (hanya output satu baris teks) * * Waktu: 2007-10-8 * * @param img * @return * / public bufferedImage ModifyImageye (bufferedImage img) {coba {int w = img.getwidth (); int h = img.getHeight (); g = img.creategraphics (); G.SetBackground (Color.White); g.setColor (color.blue); // atur warna font 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 ()); } return img; } public BufferedImage ModifyImagetogeter (bufferedImage B, bufferedImage d) {coba {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.writeMagelocal ("d: //cc.jpg", tt.modifyimage (d, "xichang apple", 90,90) // tulis file pada gambar); //tt.writeimagelocal("d://cc.jpg ", tt.modifyimagetogeter (b, d)); // grup beberapa gambar bersama System.out.println ("Success"); }}Di atas adalah konten lengkap dari Java yang menulis pada gambar dan dua metode penggabungan gambar. Saya harap semua orang akan mendukung wulin.com lebih banyak ~