栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021SC@SDUSC-multimedia-utils-一款java后端的图片、视频处理工具jar包

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021SC@SDUSC-multimedia-utils-一款java后端的图片、视频处理工具jar包

2021SC@SDUSC 

目录

项目名称:multimedia-utils

FIleUtil(工具类)

文档

图片工具ImageMagickUtils

相关文件


项目名称:multimedia-utils

博客四

FIleUtil(工具类)

这是一个文件处理的工具类。该file文件操作工具类实现功能包括:文件的增删改查,移动文件,文件内容的读取,较大的特点是通过反射获取包下所有类。不过这只是我们一些的补充知识,关于我们的工具所用的无非两大项,一个是将上传的MultipartFile转化为File,另一个是获取文件拓展名。注意这里要考虑好文件名不存在的情况。下面给出我们项目的相关代码。

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUtil {

    
    public static File multipartFile2File(MultipartFile multipartFile, String directory) {
        String suffix = getSuffix(multipartFile.getOriginalFilename());
        File tempFile = null;
        try {
            tempFile = new File(directory + "/" + UUID.randomUUID().toString() + suffix);
            multipartFile.transferTo(tempFile);
        } catch (IOException e) {
            throw new RuntimeException("multipartFile转File失败", e);
        }
        return tempFile;
    }

    
    public static String getSuffix(String fileName) {
        if (fileName == null) {
            throw new RuntimeException("获取文件拓展名失败");
        }
        int index = fileName.lastIndexOf(".");
        if (-1 == index) {
            throw new RuntimeException("获取文件拓展名失败");
        }
        return fileName.substring(index);
    }

}

文档

下面我们介绍各个文件所实现的功能和用处。首先来看图片工具ImageMagickUtils。

图片工具ImageMagickUtils

方法

1.图片裁切后修改分辨率并指定输出文件格式

cropAndResize(tempDirectory,inputFileName,cropAttributes,resizeAttributes,outputFileSuffix)

2.图片裁切后修改分辨率

cropAndResize(tempDirectory,inputFileName,cropAttributes,resizeAttributes)

3.图片裁切并指定输出文件格式

crop(tempDirectory,inputFileName,cropAttributes,outputFileSuffix)

4.图片裁切

crop(tempDirectory,inputFileName,cropAttributes)

5.图片修改分辨率并指定输出文件格式

resize(tempDirectory,inputFileName,resizeAttributes,outputFileSuffix)

6.图片修改分辨率

resize(tempDirectory,inputFileName,resizeAttributes)

参数介绍

输入参数

参数类型必需注释
tempDirectoryString临时文件目录
inputFileNameString输入文件名
cropAttributesCropAttributes裁切参数
resizeAttributesResizeAttributes修改分辨率参数
outputFileSuffixSuffix输出文件格式,未设置则同输入文件格式

其中:

CropAttributes

参数类型必需注释
widthInteger裁切图片的宽
heightInteger裁切图片的高
leftOffsetInteger距左边偏移量
topOffsetInteger距顶部偏移量

ResizeAttributes

参数类型必需注释
widthInteger输出图片的宽,只填写宽则高自适应
heightInteger输出图片的高,只填写高则宽自适应
qualityInteger输出图片的质量,范围0~100,默认100

输出参数

参数类型必需注释
outputFileNameString输出文件名

看完上述内容后,我们便可以给出整个文件代码。

相关文件
package com.whty.zdxt.multimedia.util;

import com.whty.zdxt.multimedia.attribute.CropAttributes;
import com.whty.zdxt.multimedia.attribute.ResizeAttributes;
import com.whty.zdxt.multimedia.enumeration.Suffix;


public class ImageMagickUtils {

    
    private static final String MAGICK = "magick";

    
    public String cropAndResize(String tempDirectory, String inputFileName, CropAttributes cropAttributes, ResizeAttributes resizeAttributes, Suffix outputFileSuffix) {

        if (!FileUtils.checkFileName(inputFileName)) {
            throw new RuntimeException("输入文件名格式错误");
        }

        // 设置输出文件后缀及名称
        String suffix = null;
        if (outputFileSuffix == null) {
            suffix = FileUtils.getSuffix(inputFileName);
        } else {
            suffix = outputFileSuffix.getCode();
        }
        String outputFileName = FileUtils.createFileName(suffix);

        // 拼接命令
        StringBuilder command = new StringBuilder();
        command.append(MAGICK);
        command.append(" ");

        command.append(FileUtils.getFilePath(tempDirectory, inputFileName));
        command.append(" ");

        command.append("-background white -flatten ");

        // 设置裁切参数
        if (cropAttributes != null) {
            Integer width = cropAttributes.getWidth();
            Integer height = cropAttributes.getHeight();
            Integer leftOffset = cropAttributes.getLeftOffset();
            Integer topOffset = cropAttributes.getTopOffset();
            if (width == null || height == null || leftOffset == null || topOffset == null) {
                throw new RuntimeException("CropAttributes缺失必要参数");
            }

            command.append("-crop");
            command.append(" ");

            // 拼接裁切参数 例:3000x1200+1000+500
            command.append(String.format("%sx%s%s%s", width, height, leftOffset < 0 ? leftOffset : "+" + leftOffset, topOffset < 0 ? topOffset : "+" + topOffset));
            command.append(" ");
        }

        // 设置修改分辨率参数
        if (resizeAttributes != null) {
            Integer width = resizeAttributes.getWidth();
            Integer height = resizeAttributes.getHeight();
            if (width == null && height == null) {
                throw new RuntimeException("ResizeAttributes缺失必要参数");
            }
            command.append("-resize");
            command.append(" ");

            if (width != null) {
                command.append(width);
            }
            if (height != null) {
                command.append("x");
                command.append(height);
            }
            command.append(">");
            command.append(" ");

            // 设置输出质量参数
            if (resizeAttributes.getQuality() != null) {
                command.append("-quality ");
                command.append(resizeAttributes.getQuality());
                command.append(" ");
            }
        }

        // 如果输出为.webp,则设置为无损格式
        if (Suffix.WEBP_LOSSLESS == outputFileSuffix) {
            command.append("-define webp:lossless=true ");
        }

        // 设置输出文件名
        command.append(FileUtils.getFilePath(tempDirectory, outputFileName));

        RuntimeUtils.execSF(command.toString());

        return outputFileName;
    }

    
    public String cropAndResize(String tempDirectory, String inputFileName, CropAttributes cropAttributes, ResizeAttributes resizeAttributes) {
        return this.cropAndResize(tempDirectory, inputFileName, cropAttributes, resizeAttributes, null);
    }

    
    public String crop(String tempDirectory, String inputFileName, CropAttributes cropAttributes, Suffix outputFileSuffix) {
        return this.cropAndResize(tempDirectory, inputFileName, cropAttributes, null, outputFileSuffix);
    }

    
    public String crop(String tempDirectory, String inputFileName, CropAttributes cropAttributes) {
        return this.cropAndResize(tempDirectory, inputFileName, cropAttributes, null, null);
    }


    
    public String resize(String tempDirectory, String inputFileName, ResizeAttributes resizeAttributes, Suffix outputFileSuffix) {
        return this.cropAndResize(tempDirectory, inputFileName, null, resizeAttributes, outputFileSuffix);
    }

    
    public String resize(String tempDirectory, String inputFileName, ResizeAttributes resizeAttributes) {
        return this.cropAndResize(tempDirectory, inputFileName, null, resizeAttributes, null);
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号