图片的尺寸相关处理

public void photo(@RequestParam("file") MultipartFile file) throws IOException, IM4JavaException, InterruptedException {
    //存储到本地服务器
    String fileName = file.getOriginalFilename(); // 获取原始文件名
    String filePath = CustomConfig.filePath; // 文件保存的路径
    File dest = new File(filePath + fileName); // 文件存储路径
    // 判断文件路径是否存在,不存在就建立路径
    if (!dest.getParentFile().exists()) {
        dest.getParentFile().mkdirs();
        dest.createNewFile();
    }
    file.transferTo(dest);
    //进行相关尺寸处理
    IMOperation operation = new IMOperation();
    ConvertCmd cmd = new ConvertCmd();
    String filePath1 = dest.toString(); 
    operation.addImage(filePath1);
    File picture = new File(filePath1);
    FileInputStream fis = new FileInputStream(picture);
    BufferedImage sourceImg = ImageIO.read(fis);
    int width = sourceImg.getWidth();// 原图宽度
    int height = sourceImg.getHeight();// 原图高度
    if (width > 1920 || height > 1080) {
        operation.resize(1920, 1080);
    }
    String filePath2 = filePath + System.currentTimeMillis() + CustomConfig.fileSuffixName;
    operation.addImage(filePath2);
    cmd.setSearchPath(CustomConfig.imageMagickPath);  //Windows须要设置,Linux不须要
    cmd.run(operation);
    fis.close();
}复制代码
相关文章
相关标签/搜索