CompressPic.java 3.52 KB
package com.example.dahua.lib;

import com.example.dahua.async.ImageUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * 图片压缩工具
 */
public class CompressPic {

    public static String CompressPic(String srcPath, String targetPath, String studentcode) throws Exception {
        double cutPercent = 0.5;
        File file = new File(srcPath.trim());
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(file));
        int width = bufferedImage.getWidth(null);
        int height = bufferedImage.getHeight(null);
        long fileLength = file.length();
        if ((fileLength / 1024) < 64) {
            writeImgToFile(bufferedImage, width, height, targetPath);
        } else
            while ((fileLength / 1024) >= 64) {
                width = (int) (width * (1 - cutPercent));
                height = (int) (height * (1 - cutPercent));

                writeImgToFile(bufferedImage, width, height, targetPath);

                File file1 = new File(targetPath);
                BufferedImage bufferedImage1 = ImageIO.read(new FileInputStream(file1));
                width = bufferedImage1.getWidth(null);
                height = bufferedImage1.getHeight(null);
                fileLength = file1.length();
            }
//        System.out.printf("图片大小:"+fileLength/1024);
        return targetPath;
    }

    private static void writeImgToFile(BufferedImage bufferedImage, int width, int height, String targetPath) {
        try {
            BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            tag.getGraphics().drawImage(bufferedImage, 0, 0, width, height, null); // 绘制缩小后的图
            FileOutputStream deskImage = new FileOutputStream(targetPath); // 输出到文件流
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(deskImage);
//            encoder.encode(tag); // 近JPEG编码
            boolean write = ImageIO.write(tag, "jpg", deskImage);
            /*if (write) {
                int angle = ImageUtils.getImgRotateAngle(targetPath);
                System.out.println("angle:" + angle);
                if (angle == 0) {
//                    ImageUtils.rotatePhonePhoto(targetPath, 90);
                }

            }*/
            System.out.println("读写图片:" + write);
            deskImage.close();
        } catch (Exception e) {
            System.out.println("读写图片:" + e.toString());
            e.printStackTrace();
        }
    }


    /**
     * @param file     源文件
     * @param realpath 存放图片路径
     * @param fileName 图片文件名字
     * @return
     */
    public static String writeUploadFile(MultipartFile file, String realpath, String fileName) {
        File fileDir = new File(realpath);
        if (!fileDir.exists())
            fileDir.mkdirs();

        InputStream input = null;
        FileOutputStream fos = null;
        String outPath = realpath + fileName;
        try {
            input = file.getInputStream();
            fos = new FileOutputStream(outPath);
            IOUtils.copy(input, fos);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(fos);
        }
        return outPath;
    }

}