java 二張不同的圖片進行拼圖

1.如果你想要上下拼圖,就把二張照片的寬度調成一致的

2.如果你想要左右拼,就先把二張照片的高度調成一致的

現在就開始拼圖:

既然你寫代碼拼圖肯定不是一張圖.所以先要把文件夾給我,然後把文件夾裏的所有圖片都遍歷一邊.一邊遍歷一邊拼圖

package tuPian;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class demo {
	/**
	 * @param fileUrl
	 *            文件絕對路徑或相對路徑
	 * @return 讀取到的緩存圖像
	 * @throws IOException
	 *             路徑錯誤或者不存在該文件時拋出IO異常
	 */
	public static BufferedImage getBufferedImage(String fileUrl) throws IOException {
		File f = new File(fileUrl);
		return ImageIO.read(f);
	}

	/**
	 * @param savedImg
	 *            待保存的圖像
	 * @param saveDir
	 *            保存的目錄
	 * @param fileName
	 *            保存的文件名,必須帶後綴,比如 "beauty.jpg"
	 * @param format
	 *            文件格式:jpg、png或者bmp
	 * @return
	 */
	public static boolean saveImage(BufferedImage savedImg, String saveDir, String fileName, String format) {
		boolean flag = false;

		// 先檢查保存的圖片格式是否正確
		String[] legalFormats = { "jpg", "JPG", "png", "PNG", "bmp", "BMP" };
		int i = 0;
		for (i = 0; i < legalFormats.length; i++) {
			if (format.equals(legalFormats[i])) {
				break;
			}
		}
		if (i == legalFormats.length) { // 圖片格式不支持
			System.out.println("不是保存所支持的圖片格式!");
			return false;
		}

		// 再檢查文件後綴和保存的格式是否一致
		String postfix = fileName.substring(fileName.lastIndexOf('.') + 1);
		if (!postfix.equalsIgnoreCase(format)) {
			System.out.println("待保存文件後綴和保存的格式不一致!");
			return false;
		}

		String fileUrl = saveDir + fileName;
		File file = new File(fileUrl);
		try {
			flag = ImageIO.write(savedImg, format, file);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return flag;
	}

	/**
	 * 待合併的兩張圖必須滿足這樣的前提,如果水平方向合併,則高度必須相等;如果是垂直方向合併,寬度必須相等。
	 * mergeImage方法不做判斷,自己判斷。
	 * 
	 * @param img1
	 *            待合併的第一張圖
	 * @param img2
	 *            帶合併的第二張圖
	 * @param isHorizontal
	 *            爲true時表示水平方向合併,爲false時表示垂直方向合併
	 * @return 返回合併後的BufferedImage對象
	 * @throws IOException
	 */
	public static BufferedImage mergeImage(BufferedImage img1, BufferedImage img2, boolean isHorizontal)
			throws IOException {
		int w1 = img1.getWidth();
		int h1 = img1.getHeight();
		int w2 = img2.getWidth();
		int h2 = img2.getHeight();

		// 從圖片中讀取RGB
		int[] ImageArrayOne = new int[w1 * h1];
		ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行掃描圖像中各個像素的RGB到數組中
		int[] ImageArrayTwo = new int[w2 * h2];
		ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);

		// 生成新圖片
		BufferedImage DestImage = null;
		if (isHorizontal) { // 水平方向合併
			DestImage = new BufferedImage(w1 + w2, h1, BufferedImage.TYPE_INT_RGB);
			DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 設置上半部分或左半部分的RGB
			DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);
		} else { // 垂直方向合併
			DestImage = new BufferedImage(w1, h1 + h2, BufferedImage.TYPE_INT_RGB);
			DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 設置上半部分或左半部分的RGB
			DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 設置下半部分的RGB
		}

		return DestImage;
	}

	public static void main(String[] args) {
		try {

			// 讀取待合併的文件
			BufferedImage bi1 = null;
			BufferedImage bi2 = null;
			String SJpath = "C:\\Users\\sclf\\Desktop\\開發臨時文檔\\任務狀態\\正在開發\\完整的描述\\";
			String XXpath = "C:\\Users\\sclf\\Desktop\\開發臨時文檔\\任務狀態\\完成,保留三十天\\拼接\\已完成\\新建文件夾\\";
			String pjpath = "C:\\Users\\sclf\\Desktop\\開發臨時文檔\\任務狀態\\正在開發\\合併\\";

			String SJjpgName = null;
			String XXjpgName = null;
			String bi1Url = null;
			String bi2Url = null;
			/*
			 * 肖像的照片路徑處理
			 */
			File xxfile = new File(XXpath);
			String XXfilePath = xxfile.getPath();
			File[] listxxfile = xxfile.listFiles();
			String re = "[r'.jpg']";
			/*
			 * 手跡的照片路徑 處理
			 */
			File SJfile = new File(SJpath);
			String SJfilepath = SJfile.getPath();
			File[] listSJfile = SJfile.listFiles();
			for (File sjurlFile : listxxfile) {
				bi1Url = sjurlFile.toString();
				String XXsubstring = bi1Url.substring(XXfilePath.length()+1);
				XXjpgName = XXsubstring.replaceAll(re, "");

				for (File xxurlFile : listSJfile) {
					bi2Url = xxurlFile.toString();
					String SJsubstring = bi2Url.substring(SJfilepath.length()+1);
					SJjpgName = SJsubstring.replaceAll(re, "");

					if (XXjpgName.equals(SJjpgName)) {
						// 如果名字一樣就合併

						bi1 = getBufferedImage(bi1Url);
						bi2 = getBufferedImage(bi2Url);

						// 調用mergeImage方法獲得合併後的圖像
						BufferedImage destImg = null;
						//false 上下拼圖   	   true是左右拼圖
						destImg = mergeImage(bi1, bi2, false);

						String name = SJjpgName + ".jpg";
						saveImage(destImg, pjpath, name, "jpg");
						System.out.println("垂直合併完畢!");
					}

				}

			}
			System.out.println("運行完畢!");

		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}

	}

}

 

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