java is a method of combining two images by writing on them

  • 2020-05-17 05:26:06
  • OfStack

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(" Chinese clouds ", Font.PLAIN, 40);//  Add font property Settings  
  
  private Graphics2D g = null; 
  
  private int fontsize = 0; 
  
  private int x = 0; 
  
  private int y = 0; 
  
  /**
   *  Import the local image into the 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 into 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 locally 
   */ 
  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 of the text, etc 
   */ 
  public void setFont(String fontStyle, int fontSize) { 
    this.fontsize = fontSize; 
    this.font = new Font(fontStyle, Font.PLAIN, fontSize); 
  } 
  
  /**
   *  The images , Returns the modified image buffer (output only 1 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 font color  
      if (this.font != null) 
        g.setFont(this.font); 
      //  Verify the vertical 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; 
  } 
  
  /**
   *  The images , Returns the modified image buffer (outputs multiple text segments)  xory : true Indicates that the content will be in 1 Line output; false Means to output multiple lines of content 
   */ 
  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 vertical 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; 
  } 
  
  /**
   *  The images , Returns the modified image buffer (output only 1 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, 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 the document on the picture  
     ); 
  
    //tt.writeImageLocal("D:\\cc.jpg", tt.modifyImagetogeter(b, d)); 
    // Combine multiple pictures together 1 since  
    System.out.println("success"); 
  } 
  
}

Related articles: