java图片处理

最近有一个接口,原本是其他语言的。
用于上传图片,图片大小为1M, 最多16张。
由于原来的逻辑是上传到服务器再上传。且接口服务于手机app, 流程不能更改,用java重构后,接口较慢。
查看原来的逻辑,会对图片进行压缩,所以寻找java图片库对图片进行压缩。

    <dependency>
        <groupId>net.coobird</groupId>
        <artifactId>thumbnailator</artifactId>
        <version>0.4.8</version>
    </dependency>
    <!--图片信息获取 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-imaging</artifactId>
        <version>1.0-alpha2</version>
    </dependency>

package com.example.demo.aa;

import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.imaging.*;
import org.apache.commons.imaging.common.BufferedImageFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@org.springframework.stereotype.Controller
@RequestMapping("/test")
public class Controller {

@PostMapping("/a")
public String test(MultipartFile file) throws IOException, ImageReadException {
    long size = file.getSize();
    String originalFilename = file.getOriginalFilename();
    int i = originalFilename.lastIndexOf('.');
    String substring = originalFilename.substring(i);
    File file1 =File.createTempFile("ssss", substring);
    file.transferTo(file1);

    // 读取 w h
    BufferedImage image = imageReadExample(file1);
    int width = image.getWidth();
    int height = image.getHeight();
    

    Thumbnails.of(file1)
            // 比例缩放
            .scale(0.8f)
            .outputQuality(1f) //质量
            .toFile(new File( "SSSSSSSS111" + substring));
  return null;
}

public static BufferedImage imageReadExample(final File file)
        throws ImageReadException, IOException {
    final Map<String, Object> params = new HashMap<>();
    params.put(ImagingConstants.BUFFERED_IMAGE_FACTORY,
            new ManagedImageBufferedImageFactory());
    return Imaging.getBufferedImage(file, params);
}

public static class ManagedImageBufferedImageFactory implements
        BufferedImageFactory {

    @Override
    public BufferedImage getColorBufferedImage(final int width, final int height,
                                               final boolean hasAlpha) {
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        final GraphicsDevice gd = ge.getDefaultScreenDevice();
        final GraphicsConfiguration gc = gd.getDefaultConfiguration();
        return gc.createCompatibleImage(width, height,
                Transparency.TRANSLUCENT);
    }

    @Override
    public BufferedImage getGrayscaleBufferedImage(final int width, final int height,
                                                   final boolean hasAlpha) {
        return getColorBufferedImage(width, height, hasAlpha);
    }
}

}

Thumbnailator
https://www.xuebuyuan.com/3229489.html
https://www.cnblogs.com/zumengjie/p/13828203.html
图片库比较
https://www.tutorialspoint.com/java_dip/open_source_libraries.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值