ImageUtils.java 1.78 KB
package com.example.demo;

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

/**
 * Created by jack on 2018/7/2.
 */
public class ImageUtils {
    public static BufferedImage zoomInImage(BufferedImage originalImage) {
        int width = originalImage.getWidth();
        int height = originalImage.getHeight();
        int scale_width = width;
        int scale_height = height;
        if (width < 200) {
            scale_width = 225;
        }
        if (height < 200) {
            scale_height = 225;
        }
        BufferedImage newImage = new BufferedImage(scale_width, scale_height, originalImage.getType());
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0, 0, scale_width, scale_height, null);
        g.dispose();
        return newImage;
    }

    public static boolean zoomInImage(String srcPath, String newPath) {
        BufferedImage bufferedImage = null;
        try {
            File of = new File(srcPath);
            if (of.canRead()) {
                bufferedImage = ImageIO.read(of);
            }
        } catch (IOException e) {
            //TODO: 打印日志
            return false;
        }
        if (bufferedImage != null) {
            if (bufferedImage.getWidth() < 200 || bufferedImage.getHeight() < 200) {
                bufferedImage = zoomInImage(bufferedImage);
            }
            try {
                //TODO: 这个保存路径需要配置下子好一点
                ImageIO.write(bufferedImage, "JPG", new File(newPath)); //保存修改后的图像,全部保存为JPG格式
            } catch (IOException e) {
                // TODO 打印错误信息
                return false;
            }
        }
        return true;
    }


}