Examples are as follows:
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("Huawen Caiyun", Font.PLAIN, 40);// Add font properties to set 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 { return ImageIO.read(new File(imgName)); } catch (IOException e) { System.out.println(e.getMessage()); } return null; } /** * Import network image 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 a new image to the local*/ 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()); } } } } /** * Set the font, etc. of the text*/ public void setFont(String fontStyle, int fontSize) { this.fontsize = fontSize; this.font = new Font(fontStyle, Font.PLAIN, fontSize); } /** * Modify the picture and return the modified image buffer (only output one line of text) */ public BufferedImage modifyImage(BufferedImage img, Object content, 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 the 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 and return the modified image buffer (output multiple text segments) xory: true means output the content in one line; false means output the content multiple lines*/ public BufferedImage modifyImage(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 the text output position} } else { for (int i = 0; i < arrlen; i++) { g.drawString(contentArr[i].toString(), this.x, this.y); this.y += this.fontsize + 2;// Recalculate the text output position} } } g.dispose(); } catch (Exception e) { System.out.println(e.getMessage()); } return img; } /** * Modify the picture and return the modified image buffer (only output 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 the 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, 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) //Write a file on the picture); //tt.writeImageLocal("D://cc.jpg", tt.modifyImagetogeter(b, d)); //Group multiple pictures together System.out.println("success"); } }The above is the full content of the Java writing on pictures and the two pictures merging methods. I hope everyone will support Wulin.com more~