1.合併圖片java
package com.tb.image; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; /** * 圖片拼接 * 把多張寬度同樣的圖片拼接成一張大圖片 * @author Administrator * */ public class CreateBigImage { public static void main(String[] args) { // System.out.println("123"); // // //設置圖片寬度相同 // changeImage("D:/imgs/", "1.jpg", "1.jpg", 300,200); // changeImage("D:/imgs/", "2.jpg", "2.jpg", 300,200); // changeImage("D:/imgs/", "3.jpg", "3.jpg", 300,200); // //獲取寬度相同的圖片 // String img1 = "D:/imgs/1.jpg"; // String img2 = "D:/imgs/2.jpg"; // String img3 = "D:/imgs/3.jpg"; // String[] imgs = new String[]{img1,img2,img3}; // //圖片拼接 // merge(imgs,"jpg","D:/imgs/big.jpg"); String folderPath = "D:/imgs"; changeFolderImages(folderPath,600,400); mergeFolderImgs(folderPath,"jpg","D:/imgs/merge.jpg"); } /** * 合併圖片 * @param folderPath 圖片所在文件夾的絕對路徑 * @param imgType 合併後的圖片類型(jpg、png...) * @param outAbsolutePath(輸出合併後文件的絕對路徑) * @return */ public static String mergeFolderImgs(String folderPath,String imgType,String outAbsolutePath){ File folder = new File(folderPath); File[] imgList = folder.listFiles(); String[] imgPaths = new String[imgList.length]; for (int i = 0; i < imgList.length; i++) { //System.out.println("文件個數:"+imgList[i].length()); imgPaths[i] = imgList[i].getAbsolutePath(); System.out.println("第"+i+"張圖片途徑:"+imgPaths[i]); } merge(imgPaths,imgType,outAbsolutePath); System.out.println("---------------------"); File newImg = new File(outAbsolutePath); System.out.println(newImg.getName()); return newImg.getName(); } /** * 設置圖片大小(單張圖片) * @param path 路徑 * @param oldimg 舊圖片名稱 * @param newimg 新圖片名稱 * @param newWidth 新圖片寬度 * @param newHeight 新圖片高度 */ public static void changeImage(String path, String oldimg, String newimg, int newWidth,int newHeight) { try { File file = new File(path + oldimg); Image img = ImageIO.read(file); // 構造Image對象 // int wideth = img.getWidth(null); // 獲得源圖寬 // int height = img.getHeight(null); // 獲得源圖長 BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); tag.getGraphics() .drawImage(img, 0, 0, newWidth, newHeight, null); // 繪製後的圖 FileOutputStream out = new FileOutputStream(path + newimg); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); // 近JPEG編碼 out.close(); } catch (IOException e) { System.out.println("處理文件出現異常"); e.printStackTrace(); } } /** * 設置圖片大小(批量處理整個文件夾中的圖片) * @param folderPath 文件夾路徑 * @param newWidth 新圖片寬度 * @param newHeight 新圖片高度 */ public static void changeFolderImages(String folderPath, int newWidth,int newHeight) { try { File folder = new File(folderPath);//獲得文件夾 File[] imgList = folder.listFiles();//獲得文件夾中的全部圖片 Image image = null;//定義一張圖片 BufferedImage bfImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); FileOutputStream outputStream = null; JPEGImageEncoder encoder = null; for (int i = 0; i < imgList.length; i++) { image = ImageIO.read(imgList[i]);//將獲得的圖片放入新定義的圖片中 bfImg.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);//繪製後的圖 outputStream = new FileOutputStream(imgList[i]); encoder = JPEGCodec.createJPEGEncoder(outputStream); encoder.encode(bfImg); } outputStream.close(); } catch (IOException e) { System.out.println("處理文件出現異常"); e.printStackTrace(); } } /** * Java拼接多張圖片 * * @param pics:圖片源文件 (必需要寬度同樣),如: * String img1 = "D:/imgs/3.jpg"; * String img2 = "D:/imgs/3.jpg"; * String img3 = "D:/imgs/big.jpg"; * String[] pics = new String[]{img1,img2,img3}; * @param type :圖片輸出類型(jpg,png,jpeg...) * @param dst_pic :圖片輸出絕對路徑,如 String dst_pic="D:/imgs/big2.jpg"; * @return */ public static boolean merge(String[] pics, String type, String dst_pic) { int len = pics.length; //圖片文件個數 if (len < 1) { System.out.println("pics len < 1"); return false; } File[] src = new File[len]; BufferedImage[] images = new BufferedImage[len]; int[][] ImageArrays = new int[len][]; for (int i = 0; i < len; i++) { try { src[i] = new File(pics[i]); images[i] = ImageIO.read(src[i]); } catch (Exception e) { e.printStackTrace(); return false; } int width = images[i].getWidth(); int height = images[i].getHeight(); ImageArrays[i] = new int[width * height];// 從圖片中讀取RGB ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width); } int dst_height = 0; int dst_width = images[0].getWidth(); for (int i = 0; i < images.length; i++) { dst_width = dst_width > images[i].getWidth() ? dst_width : images[i].getWidth(); dst_height += images[i].getHeight(); } System.out.println(dst_width); System.out.println(dst_height); if (dst_height < 1) { System.out.println("dst_height < 1"); return false; } // 生成新圖片 try { // dst_width = images[0].getWidth(); BufferedImage ImageNew = new BufferedImage(dst_width, dst_height, BufferedImage.TYPE_INT_RGB); int height_i = 0; for (int i = 0; i < images.length; i++) { ImageNew.setRGB(0, height_i, dst_width, images[i].getHeight(), ImageArrays[i], 0, dst_width); height_i += images[i].getHeight(); } File outFile = new File(dst_pic); ImageIO.write(ImageNew, type, outFile);// 寫圖片 } catch (Exception e) { e.printStackTrace(); return false; } return true; } }
