Package writeimg; Import Javax.imageio.ImageIO; Import Java.awt.Color; Import Java.awt.Font; Import Java.awt.Graphics2D; Import Java.awt.image.BufferedImage; Import Java.io.File; Import java.io.IOException; Import Java.net.URL; public class Pic {Private font font = new Font ("Chinese cloud", Font.plain, 40);//Add Font property setting private graphics2d g = Null private int fontsize = 0; private int x = 0; private int y = 0; /** * Import local image to buffer */public bufferedimage loadimagelocal (String imgname) {try {retur N Imageio.read (new File (imgname)); } catch (IOException e) {System.out.println (E.getmessage ()); } return null; /** * Import Network picture to buffer */public bufferedimage Loadimageurl (String imgname) {try { URL url = new URL (imgname); return Imageio.read (URL); } catch (IOException e) {System.out.println (E.GetMessage ()); } return null; }/** * Generate new picture to local */public void writeimagelocal (String newimage, bufferedimage img) {if (n Ewimage! = NULL && img! = null) {try {file outputfile = new File (newimage); Imageio.write (img, "JPG", outputfile); } catch (IOException e) {System.out.println (E.getmessage ()); }}}/** * Set the font of the text, etc. */public void SetFont (String fontstyle, int fontSize) { This.fontsize = fontsize; This.font = new Font (FontStyle, Font.plain, fontSize); }/** * Modify the picture to return the modified picture buffer (output only one line of text) */public BufferedImage modifyimage (bufferedimage img, Object cont ent, int x, int y) {try {int w = img.getwidth (); int h = img.getheight (); g = Img.creategraphics (); G.setbackground (Color.White); G.setcolor (Color.orange);//Set Font color if (this.font! = null) G.setfont (This.font); Verify the ordinate and horizontal coordinates of the output position 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; }/** * Modify the picture to return the modified picture buffer (output multiple text segments) xory:true indicates that the content is output on one line; False indicates that the content is output multiple lines */public bufferedimage Modify Image (BufferedImage img, object[] contentarr, int x, int y, Boolean xory) {try {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); Verify the ordinate and horizontal coordinates of the output position 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;//recalculate text output location }} else {for (int i = 0; i < Arrlen; i++) { g.DrawString (Contentarr[i].tostring (), this.x, THIS.Y); This.y + = This. fontsize + 2;//recalculate text output location}}} g.dispose (); } catch (Exception e) {System.out.println (E.getmessage ()); } return img; }/** * Modify the picture to return the modified picture buffer (output only one line of text) * * Time: 2007-10-8 * * @param img * @return * * Public bufferedimage modifyimageye (bufferedimage img) {try {int w = img.getwidth (); int h = img.getheight (); g = Img.creategraphics (); G.setbackground (Color.White); G.setcolor (Color.Blue);//Set Font color 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) { try {int w = b.getwidth (); int h = b.getheight (); g = D.creategraphics (); G.drawimage (b, N, 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)//to the picture to write the document); Tt.writeimagelocal ("D:\\cc.jpg", Tt.modifyimagetogeter (b, D)); Combine multiple images together System.out.println ("Success"); } }
Original: http://www.cnblogs.com/interdrp/archive/2013/03/04/2942559.html
Java writes to a picture, two ways to merge pictures