基于geotools实现图片的缩放
作者:互联网
1. pom依赖
2. 代码展示
package cn.surpass;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.io.AbstractGridCoverage2DReader;
import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.coverage.grid.io.GridFormatFinder;
import org.geotools.coverage.processing.Operations;
import org.geotools.factory.GeoTools;
import org.geotools.factory.Hints;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import javax.media.jai.PlanarImage;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author surpass
* @version 1.0
* @date 2021/11/26 10:12
*/
public class ImageUtils {
private final static Logger LOGGER = LoggerFactory.getLogger(ImageUtils.class);
private static final Integer LARGE_THUMB_IMG_WIDTH = 512;
public static void generateThumb(String filePath, String saveDir,ImageFormat imageFormat) {
LOGGER.info("处理文件路径为{};输出文件夹路径为:{}", filePath, saveDir);
File file = new File(filePath);
AbstractGridFormat format = GridFormatFinder.findFormat(file);
Hints hints = GeoTools.getDefaultHints();
AbstractGridCoverage2DReader reader = format.getReader(file, hints);
try {
//获取图像的宽和高的像素
GridCoverage2D read = reader.read(null);
RenderedImage sourceImage = read.getRenderableImage(0, 1).createDefaultRendering();
int rasterXSize = sourceImage.getWidth();
int rasterYSize = sourceImage.getHeight();
LOGGER.info("原始影像的width:{};height:{}", rasterXSize, rasterYSize);
double scale = LARGE_THUMB_IMG_WIDTH / (double) Math.max(rasterXSize, rasterYSize);
LOGGER.info("=======>SCALE:{}", scale);
// 核心代码
Operations ops = new Operations(null);
RenderedImage image = ops.scale(read, scale, scale, 0, 0).getRenderedImage();
if (image == null) {
LOGGER.error("临时图片生成失败");
return;
}
BufferedImage largeThumb = PlanarImage.wrapRenderedImage(image).getAsBufferedImage();
//保存图像
String descFileName = file.getName().replaceAll("^(.*\\.).*$", "$1"+ imageFormat.getFormatStr());
//文件夹
File saveFolder = new File(saveDir);
if (!saveFolder.exists()) {
saveFolder.mkdirs();
}
saveImage(largeThumb, new File(saveFolder, descFileName), imageFormat.getFormatStr());
LOGGER.info("缩略图生成成功");
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException("缩略图生成失败," + e.getMessage());
} finally {
reader.dispose();
}
}
private static void saveImage(BufferedImage image, File descFile, String format) throws Exception {
try (FileOutputStream fos = new FileOutputStream(descFile)) {
ImageIO.write(image, format, fos);
fos.flush();
} catch (IOException e) {
throw e;
}
}
public enum ImageFormat{
IMAGE_FORMAT_PNG("png"),
IMAGE_FORMAT_WEBP("webp"),
IMAGE_FORMAT_JPG("jpg");
private String formatStr;
public String getFormatStr() {
return formatStr;
}
public void setFormatStr(String formatStr) {
this.formatStr = formatStr;
}
private ImageFormat(String formatStr){
this.formatStr = formatStr;
}
}
}
标签:基于,geotools,String,缩放,image,formatStr,org,import 来源: https://blog.csdn.net/oYinHeZhiGuang/article/details/121595944