java豎直方向等分切割圖片

因業務需求,需要將豎直的圖片等分切割。以便前端逐個加載

參考網上鍊接,並稍作改動,形成工具類。

可直接複用代碼。若需要水平切割,將部分內容改動即可。請看截圖

package com.comall.easyeco.backend.web.img;

import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
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;

/**
 * @Auther: lxp
 * @Date: 2019/12/10 10:35
 * @Description:
 */
public class CutImageUtil {

    /**
     * 切割圖片
     * @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 cHeight = height/index;// 切片高度
                BufferedImage image = null;
                for (int i = 0; i < index; i++) {
                    // x座標,y座標,寬度,高度
                    BASE64Encoder encoder = new BASE64Encoder();
                    int ch = i*cHeight;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    image = source.getSubimage(0,ch,width,cHeight);
                    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 cHeight = height/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 ch = i*cHeight;
                    image = source.getSubimage(0,ch,width,cHeight);
                    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;
    }
}

原博主鏈接:https://blog.csdn.net/hgyu_962464/article/details/79278222

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