Java合併圖片

1、以第一個圖片尺寸爲準,畫上第二個圖片

2、取兩圖合併後的最大邊框

工具代碼

/**
	 * 合併兩個圖片
	 * @param img1Path 圖片1地址
	 * @param img2Path 圖片2地址
	 * @param x 圖片2左上點在X軸起始座標
	 * @param y 圖片2左上點在y軸起始座標
	 * @param destPath 合併兩個圖片生成新的圖片地址
	 * @param formatName 圖片格式  bmp|gif|jpg|jpeg|png
	 * @param maxBorder ture:取兩圖最大邊框;false:以圖片1尺寸和座標爲準
	 * @return 
	 * @throws IOException 
	 */
	public static boolean mergeImages(String img1Path, String img2Path,
			int x, int y, String destPath, String formatName, boolean maxBorder) throws IOException {
		boolean isSuccess = false;
		if (StringUtils.isNotEmpty(img1Path) && StringUtils.isNotEmpty(img2Path)) {
			BufferedImage img1 = ImageIO.read(new File(img1Path));//讀取圖片1
			BufferedImage img2 = ImageIO.read(new File(img2Path));//讀取圖片2
			if (!maxBorder) {//以圖片1尺寸和座標爲準
				isSuccess = drawNewImageInImage1(img1, img2, x, y, destPath, formatName);
			} else {//取兩圖最大邊框
				if (!needReptain(img1, img2, x, y)) {
					isSuccess = drawNewImageInImage1(img1, img2, x, y, destPath, formatName);
				} else {
					int w1 = img1.getWidth();
					int w2 = img2.getWidth();
					int wx1 = w1 - x;
					
					int h1 = img1.getHeight();
					int h2 = img2.getHeight();
					int hy1 = h1 - y;
					//創建背景圖片
					BufferedImage blankImage = new BufferedImage(wx1 >= w2 ? (x >= 0 ? w1 : w1 - x) : (x >= 0 ? w2 + x : w2), 
							hy1 > h2 ? (y >= 0 ? h1 : h1 - y) : (y >= 0 ? h2 + y : h2), BufferedImage.TYPE_INT_RGB);
					//畫圖片1、圖片2
					Graphics blankImgGraphics = null;
					try {
						blankImgGraphics = blankImage.getGraphics();
						blankImgGraphics.drawImage(img1, x < 0 ? -1*x : 0, y < 0 ? -1*y : 0, img1.getWidth(), img1.getHeight(), null);
						blankImgGraphics.drawImage(img2, x < 0 ? 0 : x, y < 0 ? 0 : y, img2.getWidth(), img2.getHeight(), null);
						isSuccess = ImageIO.write(blankImage, formatName, new File(destPath));
					} finally {
						if (blankImgGraphics != null) {
							blankImgGraphics.dispose();
						}
					}
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 在圖片1中畫圖片2
	 * @param x X座標位置
	 * @param y Y座標位置
	 * @param destPath 新圖片存儲位置
	 * @param formatName 圖片格式 
	 * @param img1 圖片1
	 * @param img2 圖片2
	 * @return
	 * @throws IOException
	 */
	private static boolean drawNewImageInImage1(BufferedImage img1, BufferedImage img2, 
			int x, int y, String destPath, String formatName) throws IOException {
		boolean isSuccess;
		Graphics img1Graphics = null; 
		try {
			img1Graphics = img1.getGraphics();
			img1Graphics.drawImage(img2, x, y, img2.getWidth(), img2.getHeight(), null);
			isSuccess = ImageIO.write(img1, formatName, new File(destPath));
		} finally {
			if (img1Graphics != null) {
				img1Graphics.dispose();
			}
		}
		return isSuccess;
	}

	/**
	 * 判斷是否需要重新畫最大邊框的圖片背景
	 * @param img1  圖片1
	 * @param img2  圖片2
	 * @param x 圖片2起始X座標
	 * @param y 圖片2起始Y座標
	 * @return
	 */
	private static boolean needReptain(BufferedImage img1, BufferedImage img2,
			int x, int y) {
		if (img1 == null || img2 == null || img1.getWidth() <= 0 || img1.getHeight() <= 0
				|| img2.getWidth() <= 0 || img2.getHeight() <= 0) {
			throw new IllegalArgumentException("圖片信息不正確");
		}
		int w1 = img1.getWidth();
		int h1 = img1.getHeight();
		int w2 = img2.getWidth();
		int h2 = img2.getHeight();
		return x < 0 || y < 0 || w1 - x < w2 || h1 - y < h2;
	}

測試代碼

/**
	 * 測試圖片合併
	 * @throws IOException
	 */
	@Test
	public void testMergeImages() throws IOException {
		String imageName = "java_coffee.jpg";
		String img1Path = IMAGE_PATH + imageName;
		
		imageName = "scenery.jpg";
		String img2Path = IMAGE_PATH + imageName;
		
		String destPath1 = IMAGE_PATH + "merge.jpg";
		String destPath2 = IMAGE_PATH + "merge_maxborder.jpg";
		
		int x = -100;
		int y = -100;
		
		boolean maxBorder = false;//以圖片1座標和尺寸爲準
		ImageUtil.mergeImages(img1Path, img2Path, x, y, destPath1, ImageUtil.JPG, maxBorder);
		
		maxBorder = true;//取最大邊框
		ImageUtil.mergeImages(img1Path, img2Path, x, y, destPath2, ImageUtil.JPG, maxBorder);
	}

 合併後的圖片

 

完整源碼:https://github.com/ConstXiong/xtools 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章