按图片类型进行压缩图片的工具类(Java实现)

该工具类可以支持jpg和png两种格式图片进行压缩,可以按宽度压缩也可以支持按一定尺度进行压缩,具有很好的扩展性

具体代码如下:

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

/**
 * @author lyb
 * @since  2018/8/3
 */
public class ImageUtils {

    private ImageUtils() {
    }

    /**
     * 按宽度压缩
     * @param file
     * @param width
     * @return
     * @throws Exception
     */
    public static InputStream scale(File file, int width) throws Exception {
        ImageType type = getImageType(file);
        return scale(new FileInputStream(file), type, width);
    }

    public static InputStream scale(String file, int width) throws Exception {
        File f = new File(file);
        return scale(f, width);
    }

    public static InputStream scale(InputStream stream, ImageType type, int width) throws Exception {
        BufferedImage image = ImageIO.read(stream);
        int w = image.getWidth();
        double scale = width / w;
        return scale(image, type, scale);
    }

    public static ImageType getImageType(File file) {
        String fn = file.getName().toLowerCase();
        return getImageType(fn);
    }

    public static ImageType getImageType(String fileName) {
        ImageType type = ImageType.JPG;
        if (fileName.endsWith("jpg") || fileName.endsWith("jpeg")) {
            // nothing to do
        } else if (fileName.endsWith("png")) {
            type = ImageType.PNG;
        } else {
            throw new ServiceException("不支持的文件类型");
        }
        return type;
    }

    public static InputStream scale(File file, double scale) throws Exception {
        return scale(new FileInputStream(file), getImageType(file), scale);
    }

    public static InputStream scale(String file, double scale) throws Exception {
        File f = new File(file);
        return scale(new FileInputStream(file), getImageType(f), scale);
    }

    public static InputStream scale(BufferedImage image, ImageType type, double scale) throws Exception {
        if (scale < 0) scale = 1;
        AffineTransform transform = new AffineTransform();
        if (scale == 1) return writeImage(image, type);
        int w = image.getWidth();
        int h = image.getHeight();
        int sw = Double.valueOf(w * scale).intValue();
        int sh = Double.valueOf(h * scale).intValue();
        transform.setToScale(scale, scale);
        AffineTransformOp transOp = new AffineTransformOp(transform, null);

        BufferedImage td = new BufferedImage(sw, sh, type.getImageType());
        transOp.filter(image, td);
        return writeImage(td, type);
    }

    /**
     * 图片压缩
     */
    public static InputStream scale(InputStream stream, ImageType type, double scale) throws Exception {
        BufferedImage image = ImageIO.read(stream);
        return scale(image, type, scale);
    }


    private static InputStream writeImage(BufferedImage image, ImageType type) throws Exception {
        OutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image, type.getExtension(), out);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray());
        return inputStream;
    }

    public enum ImageType {
        JPG("jpg", BufferedImage.TYPE_3BYTE_BGR),
        PNG("png", BufferedImage.TYPE_INT_ARGB);

        private String extension;

        private int imageType;

        ImageType(String extension, int imageType) {
            this.extension = extension;
            this.imageType = imageType;
        }

        public String getExtension() {
            return extension;
        }

        public int getImageType() {
            return imageType;
        }
    }
}

其中的异常处理大家可以根据自己的需要进行自定义

你可能感兴趣的:(java,JavaimgageIo,Java,imageio的使用)