图片合成

Posted sunshineicy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片合成相关的知识,希望对你有一定的参考价值。

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;
		
	}

  合成大小相同图片

以上是关于图片合成的主要内容,如果未能解决你的问题,请参考以下文章

一次用ffmpeg实现图片+音频合成视频的开发

有没有办法将2个非常相似的代码片段组合成一个函数并重复?

iOS 图片水印图片合成文字或图片实现

使用绑定从片段访问父活动的 UI 元素

vue 合成图片

IOS 视频分解图片图片合成视频