`

图片*合并*追加*横*竖*

 
阅读更多
/**
	  * jointPic
	  * @param files
	  * @param path
	  */
	 public static void jointPic(List<File> files, String path) {   
	        try {
	            Integer allWidth = 0;   // 图片总宽度
	            Integer allHeight = 0;  // 图片总高度
	            List<BufferedImage> imgs = new ArrayList<>(); 
	            for(int i=0; i<files.size(); i++){
	                imgs.add(ImageIO.read(files.get(i)));
	                //竖向
	                if (i==0) {
	                    allWidth = imgs.get(0).getWidth();
	                }
	                allHeight += imgs.get(i).getHeight();
	                
	                 // 横向
//		   	         if (i==0) {
//		   	             allHeight = imgs.get(0).getHeight();
//		   	         }
//		   	         allWidth += imgs.get(i).getWidth();
	                
	              
	             }
	            BufferedImage combined = new BufferedImage(allWidth, allHeight, BufferedImage.TYPE_INT_RGB);  
	            // paint both images, preserving the alpha channels  
	            Graphics g = combined.getGraphics();   
	             // 竖向合成
	            Integer height = 0;
	            for(int i=0; i< imgs.size(); i++){
	                g.drawImage(imgs.get(i), 0, height, null);  
	                height +=  imgs.get(i).getHeight();
	            }
 
	         // 横向合成
//	         Integer width = 0;
//	         for(int i=0; i< imgs.size(); i++){
//	             g.drawImage(imgs.get(i), width, 0, null);
//	             width +=  imgs.get(i).getWidth();
//	         }
	         
	            ImageIO.write(combined, "jpg", new File(path));  
	            System.out.println("===合成成功====");
	        } catch (Exception e) {
	            System.out.println("===合成失败====");
	            e.printStackTrace();
	        }
	    }




   
public static void main(String[] args) throws Exception{
        List<File> files = new ArrayList<>();   
        File file1 = new File("C:/Desktop/NO1.jpg");
        File file2 = new File("C:/Desktop/NO2.jpg");
        files.add(file1);
        files.add(file2);
        String path = "C:/Users/Administrator/Desktop/new.jpg";
        jointPic(files, path);  
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics