java使用Graphics2D圖片疊加


/**
	 * 圖片覆蓋(覆蓋圖壓縮到width*height大小,覆蓋到底圖上)
	 * 
	 * @param baseFilePath 底圖
	 * @param coverFilePath 覆蓋圖
	 * @param x 起始x軸
	 * @param y 起始y軸
	 * @param width 覆蓋寬度
	 * @param height 覆蓋長度度
	 * @return
	 * @throws Exception
	 */
	public static BufferedImage coverImage(String baseFilePath, String coverFilePath, int x, int y, int width, int height) throws Exception{
		
		File baseFile = new File(baseFilePath);//底圖
		BufferedImage buffImg = ImageIO.read(baseFile);
		
		File coverFile = new File(coverFilePath); //覆蓋層
		BufferedImage coverImg = ImageIO.read(coverFile);
		
		buffImg = coverImage(buffImg, coverImg, x, y, width, height);
		
		return buffImg;
	}
	

  

	
	/**
	 * 圖片覆蓋(覆蓋圖壓縮到width*height大小,覆蓋到底圖上)
	 * 
	 * @param baseBufferedImage 底圖
	 * @param coverBufferedImage 覆蓋圖
	 * @param x 起始x軸
	 * @param y 起始y軸
	 * @param width 覆蓋寬度
	 * @param height 覆蓋長度度
	 * @return
	 * @throws Exception
	 */
	public static BufferedImage coverImage(BufferedImage baseBufferedImage, BufferedImage coverBufferedImage, int x, int y, int width, int height) throws Exception{
		
		// 創建Graphics2D對象,用在底圖對象上繪圖
		Graphics2D g2d = baseBufferedImage.createGraphics();
		
		// 繪制
		g2d.drawImage(coverBufferedImage, x, y, 300, 300, null);
		g2d.dispose();// 釋放圖形上下文使用的系統資源
		
		return baseBufferedImage;
	}

  


免責聲明!

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



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