import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ThumbUtil {
private static final Logger logger = LoggerFactory.getLogger(ThumbUtil.class);
/**
* 预置允许生成缩略图的文件类型
*/
private static final String[] IMAGE_TYPE = { "jpg", "png", "gif", "bmp", "jpeg","tiff" };
/**
* 产生宽高比不变的缩略图
*/
public static boolean createRatioThumb(String srcUrl, int width, int height, String destUtl) {
logger.info("生成缩略图方法被调用:原图位置为:"+srcUrl+";新图位置为"+destUtl);
try {
Thumbnails.of(srcUrl).size(width, height).outputFormat("jpg").toFile(destUtl);
} catch (IOException e) {
e.printStackTrace();
logger.info("生成缩略图失败:"+e.getMessage());
return false;
}
logger.info("生成缩略图完成");
return true;
}
/**
* 根据文件名判断文件是不是图片类型
*/
public static boolean isImage(String fileName) {
if (StringUtils.hasText(fileName)) {
int index = fileName.lastIndexOf(".");
if (index != -1) {
String type = fileName.substring(index+1, fileName.length());
for (String imageType : IMAGE_TYPE) {
if (imageType.equalsIgnoreCase(type)) {
return true;
}
}
}else
{
for (String imageType : IMAGE_TYPE) {
if (imageType.equalsIgnoreCase(fileName)) {
return true;
}
}
}
}
return false;
}
public static void setThumbImage(String fromPath,String toPath)
{
try {
File fi = new File(fromPath); //大图文件
File fo = new File(toPath); //将要转换出的小图文件
int nw = 200;
/*
AffineTransform 类表示 2D 仿射变换,它执行从 2D 坐标到其他 2D
坐标的线性映射,保留了线的“直线性”和“平行性”。可以使用一系
列平移、缩放、翻转、旋转和剪切来构造仿射变换。
*/
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi); //读取图片
int w = bis.getWidth();
int h = bis.getHeight();
//double scale = (double)w/h;
int nh = (nw*h)/w ;
double sx = (double)nw/w;
double sy = (double)nh/h;
transform.setToScale(sx,sy); //setToScale(double sx, double sy) 将此变换设置为缩放变换。
System.out.println(w + " " +h);
/*
* AffineTransformOp类使用仿射转换来执行从源图像或 Raster 中 2D 坐标到目标图像或
* Raster 中 2D 坐标的线性映射。所使用的插值类型由构造方法通过
* 一个 RenderingHints 对象或通过此类中定义的整数插值类型之一来指定。
如果在构造方法中指定了 RenderingHints 对象,则使用插值提示和呈现
的质量提示为此操作设置插值类型。要求进行颜色转换时,可以使用颜色
呈现提示和抖动提示。 注意,务必要满足以下约束:源图像与目标图像
必须不同。 对于 Raster 对象,源图像中的 band 数必须等于目标图像中
的 band 数。
*/
AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
/*
* TYPE_3BYTE_BGR 表示一个具有 8 位 RGB 颜色分量的图像,
* 对应于 Windows 风格的 BGR 颜色模型,具有用 3 字节存
* 储的 Blue、Green 和 Red 三种颜色。
*/
ato.filter(bis,bid);
ImageIO.write(bid,"jpeg",fo);
} catch(Exception e) {
e.printStackTrace();
}
}
private static int WIDTH ; // 缩略图宽度
private static int HEIGHT ;// 缩略图高度
public static BufferedImage zoom(String srcFileName) {
// 使用源图像文件名创建ImageIcon对象。
ImageIcon imgIcon = new ImageIcon(srcFileName);
// 得到Image对象。
Image img = imgIcon.getImage();
return zoom(img);
}
public static BufferedImage zoom(Image srcImage) {
// 构造一个预定义的图像类型的BufferedImage对象。
BufferedImage buffImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// buffImg.flush();
// 创建Graphics2D对象,用于在BufferedImage对象上绘图。
Graphics2D g = buffImg.createGraphics();
// 设置图形上下文的当前颜色为白色。
g.setColor(Color.WHITE);
// 用图形上下文的当前颜色填充指定的矩形区域。
g.fillRect(0, 0, WIDTH, HEIGHT);
// 按照缩放的大小在BufferedImage对象上绘制原始图像。
g.drawImage(srcImage, 0, 0, WIDTH, HEIGHT, null);
// 释放图形上下文使用的系统资源。
g.dispose();
// 刷新此 Image 对象正在使用的所有可重构的资源.
srcImage.flush();
return buffImg;
}
/**
* 图片生成缩略图
* @param srcFileName 原来文件路径
* @param FileName 新生成缩略图路径
* @param jpeg 格式 默认为jpeg 可以不传
* @throws IOException
*/
public static void zoom(String srcFileName,String FileName,String jpeg) throws IOException {
if(org.apache.commons.lang3.StringUtils.isBlank(jpeg)){
jpeg="jpeg";
}
// 使用源图像文件名创建ImageIcon对象。
ImageIcon imgIcon = new ImageIcon(srcFileName);
// 得到Image对象。
Image srcImage = imgIcon.getImage();
WIDTH=imgIcon.getIconWidth();
if(WIDTH<0){
WIDTH=50;
}
HEIGHT=imgIcon.getIconHeight();
if(HEIGHT<0){
HEIGHT=50;
}
// 构造一个预定义的图像类型的BufferedImage对象。
BufferedImage buffImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// buffImg.flush();
// 创建Graphics2D对象,用于在BufferedImage对象上绘图。
Graphics2D g = buffImg.createGraphics();
// 设置图形上下文的当前颜色为白色。
g.setColor(Color.WHITE);
// 用图形上下文的当前颜色填充指定的矩形区域。
g.fillRect(0, 0, WIDTH, HEIGHT);
// 按照缩放的大小在BufferedImage对象上绘制原始图像。
g.drawImage(srcImage, 0, 0, WIDTH, HEIGHT, null);
// 释放图形上下文使用的系统资源。
g.dispose();
// 刷新此 Image 对象正在使用的所有可重构的资源.
srcImage.flush();
ImageIO.write(buffImg, jpeg, new File(FileName));
}
public static void main(String[] args) {
try {
createRatioThumb("D:/image/1.png", 100, 100, "D:/image/2.png");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
图片缩略图生成
最新推荐文章于 2024-07-05 14:51:01 发布