天天看點

java 圖檔切割工具類 :水準方向等分切割

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import javax.imageio.ImageIO;

import sun.misc.BASE64Encoder;

public class CutImageUtil {

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

// List fileList = new ArrayList();

// //.cutImageToFile(“源檔案file/源檔案路徑”, “檔案儲存路徑”, “切割份數”);

// fileList = CutImageUtil.cutImageToFile(“F:/abcd.jpg”, “F:/aaaa”, 3);

// for (File file : fileList) {

// System.out.println(file.getAbsolutePath());

// }

// List list = new ArrayList();

// //.cutImageToBase64(“源檔案file/源檔案路徑”, “切割份數”);

// list = CutImageUtil.cutImageToBase64(“F:/abcd.jpg”, 2);

// for (String string : list) {

// System.out.println(string);

// }

// }

/**
 * 切割圖檔 
 * @param sourceFilePath 源檔案路徑
 * @param index 水準方向等分切割數
 * @return List<String>   base64編碼數組
 */
public static List<String> cutImageToBase64(String sourceFilePath,int index){
     File file = new File(sourceFilePath);
      if (file.exists()) {
          return cutImageToBase64(file,index);
       }else{
            return null;
       }
}

/**
 * 切割圖檔 
 * @param sourceFilePath 源檔案路徑
 * @param sourceFilePath 儲存檔案路徑
 * @param index 水準方向等分切割數
 * @return List<File>   File數組
 */
public static List<File> cutImageToFile(String sourceFilePath,String targetDir,int index){
     File file = new File(sourceFilePath);
      if (file.exists()) {
          return cutImageToFile(file, targetDir, index);
       }else{
            return null;
       }
}
/** 
 * 切割圖檔 
 *  
 * @param sourceFile 
 *            源檔案 
 * @param index 
 *            水準方向等分切割數
 * @return List<String> base64編碼數組
 */  
public static List<String> cutImageToBase64(File sourceFile,int index) {  
    List<String> list = new ArrayList<String>();  
    int suffixIndex = sourceFile.getName().lastIndexOf(".");
    String suffix = sourceFile.getName().substring(suffixIndex+1);
    try {
        BufferedImage source = ImageIO.read(sourceFile);
        int width = source.getWidth(); // 圖檔寬度  
        int height = source.getHeight(); // 圖檔高度  
        if (index>1) {  
            int cWidth = width/index; // 切片寬度  
            BufferedImage image = null;  
            for (int i = 0; i < index; i++) {  
                // x坐标,y坐标,寬度,高度  
                BASE64Encoder encoder = new BASE64Encoder();
                int cw = i*cWidth; 
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                image = source.getSubimage(cw,0,cWidth,height);
                ImageIO.write(image, "PNG", baos);
                byte[] bytes = baos.toByteArray();
                list.add("data:image/"+suffix+";base64,"+encoder.encodeBuffer(bytes));
                baos.close();  
            }
        }  
    } catch (IOException e) {
        e.printStackTrace();
    }
     return list;  
}  

/** 
 * 切割圖檔 
 *  
 * @param sourceFile 
 *            源檔案 
 * @param index 
 *            水準方向等分切割數
 * @return List<String> base64編碼數組
 */  
public static List<File> cutImageToFile(File sourceFile,String targetDir,int index) {  
    List<File> list = new ArrayList<File>();  
    int suffixIndex = sourceFile.getName().lastIndexOf(".");
    String suffix = sourceFile.getName().substring(suffixIndex+1);
    String name =  sourceFile.getName().substring(0,suffixIndex);
    try {
        BufferedImage source = ImageIO.read(sourceFile);
        int width = source.getWidth(); // 圖檔寬度  
        int height = source.getHeight(); // 圖檔高度  
        if (index>1) {  
            int cWidth = width/index; // 切片寬度  
            BufferedImage image = null;  
            File file = new File(targetDir);  
            if (!file.exists()) { // 存儲目錄不存在,則建立目錄  
                file.mkdirs();  
            }  
            int num =(int)(Math.random()*100000000);
            for (int i = 0; i < index; i++) {  
                // x坐标,y坐标,寬度,高度  
                int cw = i*cWidth; 
                image = source.getSubimage(cw,0,cWidth,height);
                String fileName = targetDir + "/"+name+"_"+num+"_"+i+ "."+suffix;
                file = new File(fileName);  
                ImageIO.write(image,"PNG", file);
                list.add(file);
            }
        }  
    } catch (IOException e) {
        e.printStackTrace();
    }
     return list;  
}  
           

}