压缩图片工具类实现,jdk私有实现(有些jdk不能直接使用)

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 暂未使用
 *
 * @author osc
 * @create 2022-11-13 14:10
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MyImgUtil {


    /**
     * 缩放图片(压缩图片质量,改变图片尺寸)
     * 若原图宽度小于新宽度,则宽度不变!
     *
     * @param originalFile
     * @param resizedFile
     * @param maxWidth
     * @param maxHeight
     * @param quality      图片质量参数 0.7f 相当于70%质量
     * @throws IOException
     */
    public static void imageResize(File originalFile, File resizedFile,
                                   int maxWidth, int maxHeight, float quality) throws IOException {

        if (quality > 1) {
            throw new IllegalArgumentException(
                    "图片质量需设置在0.1-1范围");
        }

        ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
        Image i = ii.getImage();
        Image resizedImage = null;

        int iWidth = i.getWidth(null);
        int iHeight = i.getHeight(null);

        int newWidth = maxWidth;
        if (iWidth < maxWidth) {
            newWidth = iWidth;
        }


        if (iWidth >= iHeight) {
            resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
                    / iWidth, Image.SCALE_SMOOTH);
        }


        int newHeight = maxHeight;
        if (iHeight < maxHeight) {
            newHeight = iHeight;
        }

        if (resizedImage == null && iHeight >= iWidth) {
            resizedImage = i.getScaledInstance((newHeight * iWidth) / iHeight,
                    newHeight, Image.SCALE_SMOOTH);
        }

        // This code ensures that all the pixels in the image are loaded.
        Image temp = new ImageIcon(resizedImage).getImage();

        // Create the buffered image.
        BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
                temp.getHeight(null), BufferedImage.TYPE_INT_RGB);

        // Copy image to buffered image.
        Graphics g = bufferedImage.createGraphics();

        // Clear background and paint the image.
        g.setColor(Color.white);
        g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
        g.drawImage(temp, 0, 0, null);
        g.dispose();

        // Soften.
        float softenFactor = 0.05f;
        float[] softenArray = {0, softenFactor, 0, softenFactor,
                1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0};
        Kernel kernel = new Kernel(3, 3, softenArray);
        ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
        bufferedImage = cOp.filter(bufferedImage, null);

        // Write the jpeg to a file.
        FileOutputStream out = new FileOutputStream(resizedFile);

        // Encodes image as a JPEG data stream
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

        JPEGEncodeParam param = encoder
                .getDefaultJPEGEncodeParam(bufferedImage);

        param.setQuality(quality, true);

        encoder.setJPEGEncodeParam(param);
        encoder.encode(bufferedImage);
    } // Example usage
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题与之前的问题几乎一模一样,只是要求使用 JDK 1.8 的语法实现,让我来为您提供一份新的代码: 1. AutoMiddleParamsDTO类的代码: ```java public class AutoMiddleParamsDTO { private String paramsKey; private String paramsValue; public String getParamsKey() { return paramsKey; } public void setParamsKey(String paramsKey) { this.paramsKey = paramsKey; } public String getParamsValue() { return paramsValue; } public void setParamsValue(String paramsValue) { this.paramsValue = paramsValue; } } ``` 2. 工具类的代码: ```java import java.util.List; import org.apache.commons.lang3.StringUtils; public class MyUtils { public static String getStringFromList(List<AutoMiddleParamsDTO> list, String strParam) { return list.stream() .filter(dto -> StringUtils.equals(dto.getParamsKey(), strParam) && StringUtils.isEmpty(dto.getParamsValue())) .findFirst() .map(dto -> "AAA") .orElse(null); } } ``` 在这个工具类中,我们定义了一个名为 getStringFromList 的方法,其中有两个参数:一个是 list,表示我们要操作的列表;另一个是 strParam,表示我们要查找的参数键。在这个方法中,我们使用JDK 1.8 中的 Stream API,首先将列表转换成一个 Stream,然后使用 filter 方法过滤出符合条件的 AutoMiddleParamsDTO 对象,再使用 findFirst 方法获取第一个符合条件的对象,最后使用 map 方法将其转换成 "AAA",如果没有找到符合条件的对象,则返回 null。 希望这个答案能够帮到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值