码迷,mamicode.com
首页 > 其他好文 > 详细

图片合成

时间:2016-05-25 18:11:10      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

private BufferedImage  compose (String  fileNameFront,String fileNameBack){
	   BufferedImage frontImage;
	   BufferedImage imageResult=null;
	   try {
		   long startFrontTime = System.currentTimeMillis();
		   DefaultLogger.info("StartfrontImage============="+startFrontTime);
		   frontImage = ImageIO.read(new File(fileNameFront));
		   int width = frontImage.getWidth();// width 
		   int height = frontImage.getHeight();// height
		   int[] imageArrayFirst = new int[width * height];// RGB  
		   imageArrayFirst = frontImage.getRGB(0, 0, width, height, imageArrayFirst, 0, width);  
		   frontImage.flush();
		   long startBackTime = System.currentTimeMillis();
		   DefaultLogger.info("frontImageTime============="+(startBackTime-startFrontTime));
		   
		   BufferedImage backImage = ImageIO.read(new File(fileNameBack));
		   int widthBack = backImage.getWidth();// width 
		   int heightBack = backImage.getHeight();// height		   
		   int[] imageArraySecond = new int[widthBack * heightBack];  
		   imageArraySecond = backImage.getRGB(0, 0, widthBack, heightBack, imageArraySecond, 0, widthBack); 
		   backImage.flush();
		   long endBackTime = System.currentTimeMillis();
		   DefaultLogger.info("backImageTime============="+(endBackTime-startBackTime));
		   // new image   
		   imageResult = new BufferedImage(width, height * 2+3,BufferedImage.TYPE_INT_RGB); 
		   //config images Transparency
		   Graphics2D g2d = imageResult.createGraphics();
		   imageResult = g2d.getDeviceConfiguration().createCompatibleImage(width, height* 2+3, Transparency.TRANSLUCENT);
		   g2d.dispose();
		   
		   imageResult.setRGB(0, 0, width, height, imageArrayFirst, 0, width);// set above RGB  
		   imageResult.setRGB(0, height+3, width, height, imageArraySecond, 0, width);// set below RGB  
		  
		   imageResult.flush();
		   long imageTime = System.currentTimeMillis();
		   DefaultLogger.info("ImageTime============="+(imageTime-endBackTime));
		   DefaultLogger.info("Form FrontImage To Image============="+(imageTime-startFrontTime));
		   
	   } catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	   } 
	   
	   
	   return imageResult;
	   }
	
	//vertical compose image
	private BufferedImage compose2 (String  fileNameFront,String fileNameBack){
		BufferedImage frontImage;
		 BufferedImage combined=null;
		try {
			frontImage = ImageIO.read(new File(fileNameFront));
			BufferedImage backImage = ImageIO.read(new File(fileNameBack));
		       combined = new BufferedImage(frontImage.getWidth() , frontImage.getHeight()*2, BufferedImage.TYPE_INT_RGB);  
		       
		       
		       Graphics g = combined.getGraphics();  
		       g.drawImage(frontImage, 0, 0, null);  
		       g.drawImage(backImage, 0, frontImage.getHeight(), null); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
     
       return combined;
		
	}

  合成大小相同图片

图片合成

标签:

原文地址:http://www.cnblogs.com/jingRegina/p/5527884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!