• Java-图片压缩


    一.前端上传文件,要求上传的图片小于500kb,然后后端进行压缩


    1.当我们接收到前端传来的 MultipartFile file 文件
    我们需要转成File 文件

       MultipartFile file

      File file2 = null;

    file2=File.createTempFile("tmp", null);

    file.transferTo(file2);

    注意在最后执行完是需要删除文件,因为这是在内存中进行的转换操作,在你自己的业务处理完以后删除

     file2.delete();

    这样就获得了File文件,然后开始进行压缩

    * @param file 文件 @param desFileSize 指定图片大小,单位kb * @param accuracy 精度,递归压缩的比率,建议小于0.9 @param desMaxWidth 最大宽度 * @param desMaxHeight 最大高度

    public class ImageUtils {
    public static File commpressPicForScale(File file
    ,long desFileSize, double accuracy,int desMaxWidth,int desMaxHeight) {
                                 
    try {
    long srcFileSize = file.length();
    System.out.println("源图片:" + file + ",大小:" + srcFileSize / 1024
    + "kb");
    //获取图片信息
    BufferedImage bim = ImageIO.read(file);
    int srcWidth = bim.getWidth();
    int srcHeight = bim.getHeight();

    //先转换成jpg
    Thumbnails.Builder builder = Thumbnails.of(file).outputFormat("jpg");

    // 指定大小(宽或高超出会才会被缩放)
    if(srcWidth > desMaxWidth || srcHeight > desMaxHeight) {
    builder.size(desMaxWidth, desMaxHeight);
    }else{
    //宽高均小,指定原大小
    builder.size(srcWidth,srcHeight);
    }

    // 写入到内存
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节输出流(写入到内存)
    builder.toOutputStream(baos);

    // 递归压缩,直到目标文件大小小于desFileSize
    byte[] bytes = commpressPicCycle(baos.toByteArray(), desFileSize, accuracy);

    FileOutputStream fos = new FileOutputStream(file);
    fos.write(bytes);
    fos.close();
    System.out.println("目标图片:" + file + ",大小" + file.length() / 1024 + "kb");
    System.out.println("图片压缩完成!");
    return file;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    }
    }

    private static byte[] commpressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException {
    long srcFileSizeJPG = bytes.length;
    // 2、判断大小,如果小于500kb,不压缩;如果大于等于500kb,压缩
    if (srcFileSizeJPG <= desFileSize * 1024) {
    return bytes;
    }
    // 计算宽高
    BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes));
    int srcWdith = bim.getWidth();
    int srcHeigth = bim.getHeight();
    int desWidth = new BigDecimal(srcWdith).multiply(
    new BigDecimal(accuracy)).intValue();
    int desHeight = new BigDecimal(srcHeigth).multiply(
    new BigDecimal(accuracy)).intValue();

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节输出流(写入到内存)
    Thumbnails.of(new ByteArrayInputStream(bytes)).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos);
    return commpressPicCycle(baos.toByteArray(), desFileSize, accuracy);
    }
    }

    这样就获得了一个压缩过后的文件了
  • 相关阅读:
    poj3463 Sightseeing(最短路,次短路计数)
    poj3463 Sightseeing(读题很重要)
    poj3463 Sightseeing(读题很重要)
    hdu6181 Two Paths(次短路)
    hdu6181 Two Paths(次短路)
    Tyvj1293(新姿势:次短路)
    Tyvj1293(新姿势:次短路)
    10.bitset
    9.优先队列,priority_queue
    8.queue
  • 原文地址:https://www.cnblogs.com/bt2882/p/11419744.html
Copyright © 2020-2023  润新知