java實現把兩張圖片合並(Graphics2D)


package com.yin.text;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 把兩張圖片合並
 * @version 2018-2-27 上午11:12:09
 *
 */
public class Picture1
{
     private Graphics2D g        = null;  
      
    /**  
     * 導入本地圖片到緩沖區  
     */  
    public BufferedImage loadImageLocal(String imgName) {  
        try {  
            return ImageIO.read(new File(imgName));  
        } catch (IOException e) {  
            System.out.println(e.getMessage());  
        }  
        return null;  
    } 
    
    public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {  
        
        try {  
            int w = b.getWidth();  
            int h = b.getHeight();  
  
            g = d.createGraphics();  
            g.drawImage(b, 300, -800, w, h, null);  
            g.dispose();  
        } catch (Exception e) {  
            System.out.println(e.getMessage());  
        }  
  
        return d;  
    } 
    
    /**  
     * 生成新圖片到本地  
     */  
    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 static void main(String[] args) {  
        
        Picture1 tt = new Picture1();  
  
        BufferedImage d = tt.loadImageLocal("C:/1.jpg");  
        BufferedImage b = tt.loadImageLocal("C:/11.jpg");
        
        tt.writeImageLocal("C:/new10.jpg", tt.modifyImagetogeter(b, d));    
        //將多張圖片合在一起    
        System.out.println("success");  
    } 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM