Java 파일 형식 이미지 압축 실현

4751 단어
package com.ibeetl.manage.core.util;

import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @author wenjq
 * @data 2018/10/15 13:52
 */
public class MinImageUtil {

    /**
     *                
     * @param file
     * @return
     */
    public static Boolean getMinImage(File file){
//        StringBuilder sb = new StringBuilder(file.getName());
//        StringBuilder sb1 = new StringBuilder(file.getPath());
//        String s = new StringBuilder(file.getName()).substring(0,file.getName().indexOf("."));
//        String path = new StringBuilder(file.getPath()).substring(0,file.getPath().indexOf(new StringBuilder(file.getName()).substring(0,file.getName().indexOf("."))));
        try {
            zoomImageScale(file,new StringBuilder(file.getPath()).substring(0,file.getPath().indexOf(new StringBuilder(file.getName()).substring(0,file.getName().indexOf("."))))+file.getName(),1000);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     *         
     * @param file
     * @return
     */
    public static Boolean getMinMinImage(File file){
//        StringBuilder sb = new StringBuilder(file.getName());
//        StringBuilder sb1 = new StringBuilder(file.getPath());
//        String s = new StringBuilder(file.getName()).substring(0,file.getName().indexOf("."));
//        String path = new StringBuilder(file.getPath()).substring(0,file.getPath().indexOf(new StringBuilder(file.getName()).substring(0,file.getName().indexOf("."))));
        try {
            zoomImageScale(file,new StringBuilder(file.getPath()).substring(0,file.getPath().indexOf(new StringBuilder(file.getName()).substring(0,file.getName().indexOf("."))))+"theThumbnail"+file.getName(),250);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     *              
     *
     * @param imageFile
     * @param newPath
     * @param newWidth      
     * @throws IOException
     */
    public static void zoomImageScale(File imageFile, String newPath, int newWidth) throws IOException {
        if(!imageFile.canRead())
            return;
        BufferedImage bufferedImage = ImageIO.read(imageFile);
        if (null == bufferedImage)
            return;

        int originalWidth = bufferedImage.getWidth();
        int originalHeight = bufferedImage.getHeight();
        double scale = (double)originalWidth / (double)newWidth;    //      

        int newHeight =  (int)(originalHeight / scale);

        zoomImageUtils(imageFile, newPath, bufferedImage, newWidth, newHeight);
    }

    private static void zoomImageUtils(File imageFile, String newPath, BufferedImage bufferedImage, int width, int height)
            throws IOException{

        String suffix = StringUtils.substringAfterLast(imageFile.getName(), ".");

        //    png        
        if(suffix != null && (suffix.trim().toLowerCase().endsWith("png") || suffix.trim().toLowerCase().endsWith("gif"))){
            BufferedImage to= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = to.createGraphics();
            to = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            g2d.dispose();

            g2d = to.createGraphics();
            Image from = bufferedImage.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
            g2d.drawImage(from, 0, 0, null);
            g2d.dispose();

            ImageIO.write(to, suffix, new File(newPath));
        }else{
            //      ,               
//            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//            tag.getGraphics().drawImage(bufferedImage, 0, 0, width, height, null);
//
//            FileOutputStream out = new FileOutputStream(newPath);    //       newPath
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
//            JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
//            jep.setQuality(1f, true);    //    , 1     
//            encoder.encode(tag, jep);
//            out.close();

            BufferedImage newImage = new BufferedImage(width, height, bufferedImage.getType());
            Graphics g = newImage.getGraphics();
            g.drawImage(bufferedImage, 0, 0, width, height, null);
            g.dispose();
            ImageIO.write(newImage, suffix, new File(newPath));
        }
    }
    
}

좋은 웹페이지 즐겨찾기