图片处理

package com.yashi.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.*;

/**
 * @author Gavin.luo
 * @title: ImgCompression
 * @projectName MyData
 * @description:
 * @date 2021/7/21 14:29
 */
@Slf4j
public class PicUtil {

    public static byte[] getImageCom(MultipartFile file) throws IOException {
        //获取文件输入流
        InputStream inputStream = file.getInputStream();
        try {
            log.info("把图片读入到内存中");
            // 把图片读入到内存中
            BufferedImage bufImg = ImageIO.read(inputStream);
            log.info("压缩代码,存储图片文件byte数组");
            // 压缩代码,存储图片文件byte数组
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            //防止图片变红,这一步非常重要
            BufferedImage bufferedImage = new BufferedImage(bufImg.getWidth(), bufImg.getHeight(), BufferedImage.TYPE_INT_RGB);
            bufferedImage.createGraphics().drawImage(bufImg,0,0, Color.WHITE,null);
            //先转成jpg格式来压缩,然后在通过OSS来修改成源文件本来的后缀格式
            log.info("先转成jpg格式来压缩");
            ImageIO.write(bufferedImage,"jpg",bos);
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            inputStream.close();
        }
        return null;
    }


    public static boolean compressPic(MultipartFile srcFile, String descFilePath) throws IOException {
        InputStream inputStream = srcFile.getInputStream();
        FileOutputStream out = null;
        ImageWriter imgWrier;
        ImageWriteParam imgWriteParams;
        BufferedImage src = ImageIO.read(inputStream);
        // 指定写图片的方式为 jpg
        imgWrier = ImageIO.getImageWritersByFormatName("png").next();
        imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(
                null);
        // 要使用压缩,必须指定压缩方式为MODE_EXPLICIT
        imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
        // 这里指定压缩的程度,参数qality是取值0~1范围内,
        imgWriteParams.setCompressionQuality((float) 0.3);
        imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
        ColorModel colorModel =src.getColorModel();// ColorModel.getRGBdefault();
        // 指定压缩时使用的色彩模式
//        imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
//                colorModel, colorModel.createCompatibleSampleModel(16, 16)));
        imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
                colorModel, colorModel.createCompatibleSampleModel(16, 16)));

        try {
            String substring = descFilePath.substring(0, 20);
            File fi = new File(substring);
            if (!fi.exists()){
                fi.mkdirs();
            }

            File file = new File(descFilePath);

                file.createNewFile();

                out = new FileOutputStream(file);

                imgWrier.reset();
                // 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何
                // OutputStream构造
                imgWrier.setOutput(ImageIO.createImageOutputStream(out));
                // 调用write方法,就可以向输入流写图片
                imgWrier.write(null, new IIOImage(src, null, null),
                        imgWriteParams);


        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            out.flush();
            out.close();
        }
        return true;
    }

    public static void main(String[] args) {

    }

    public static boolean isBlank(String string) {
        if (string == null || string.length() == 0 || string.trim().equals("")) {
            return true;
        }
        return false;
    }

}

 

posted @ 2023-06-12 15:09  超级大菜鸡  阅读(32)  评论(0)    收藏  举报