package com.sandu.common.file; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.Frame; import org.bytedeco.javacv.Java2DFrameConverter; import org.springframework.context.annotation.Configuration; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; @Configuration public class FileToThumbnail { /** *
Title: thumbnailImage
*Description: 根据图片路径生成缩略图
* * @param imagePath 原图片路径 * @param w 缩略图宽 * @param h 缩略图高 * @param prevfix 生成缩略图的前缀 * @param force 是否强制按照宽高生成缩略图(如果为false,则生成最佳比例缩略图) */ public void getThumbnailForPic(String imagePath, int w, int h, String prevfix, boolean force) throws IOException { File imgFile = new File(imagePath); if (imgFile.exists()) { // ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif] String types = Arrays.toString(ImageIO.getReaderFormatNames()); String suffix = null; // 获取图片后缀 if (imgFile.getName().indexOf(".") > -1) { suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1); } // 类型和图片后缀全部小写,然后判断后缀是否合法 if (suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()) < 0) { System.out.println("Sorry, the image suffix is illegal. the standard image suffix is {}." + types); return; } System.out.println("target image's size, width:{" + w + "}, height:{" + h + "}."); Image img = ImageIO.read(imgFile); if (!force) { // 根据原图与要求的缩略图比例,找到最合适的缩略图比例 int width = img.getWidth(null); int height = img.getHeight(null); if ((width * 1.0) / w < (height * 1.0) / h) { if (width > w) { h = Integer.parseInt(new java.text.DecimalFormat("0").format(height * w / (width * 1.0))); } } else { if (height > h) { w = Integer.parseInt(new java.text.DecimalFormat("0").format(width * h / (height * 1.0))); } } } BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null); g.dispose(); String p = imgFile.getPath(); // 将图片保存在原目录并加上前缀 ImageIO.write(bi, suffix, new File(p.substring(0, p.lastIndexOf(File.separator)) + File.separator + prevfix + imgFile.getName())); } } /** * 通过PDFbox生成文件的缩略图 * * @param filePath:文件路径 * @param outPath:输出图片路径 * @throws IOException */ public String getThumbnailForPdf(String filePath, String outPath) throws IOException { // 利用PdfBox生成图像 PDDocument pdDocument = PDDocument.load(new File(filePath)); PDFRenderer renderer = new PDFRenderer(pdDocument); // 构造图片 BufferedImage img_temp = renderer.renderImageWithDPI(0, 30, ImageType.RGB); ImageIO.write(img_temp, "png", new File(outPath)); pdDocument.close(); return outPath; } /** * @param sourcePath 保存路径 * @return String 保存路径 * @description //生成视频缩略图的URL地址 */ public String getThumbnailForVideo(InputStream inputStream, String sourcePath) throws IOException { File targetFile = new File(sourcePath); FFmpegFrameGrabber ff = new FFmpegFrameGrabber(inputStream); ff.start(); // 视频总帧数 int videoLength = ff.getLengthInFrames(); Frame f = null; int i = 0; while (i < videoLength) { // 过滤前20帧,因为前20帧可能是全黑的 // 这里看需求,也可以直接根据帧数取图片 f = ff.grabFrame(); if (i > 20 && f.image != null) { break; } i++; } int owidth = f.imageWidth; int oheight = f.imageHeight; // 对截取的帧进行等比例缩放 int width = 300; int height = (int) (((double) width / owidth) * oheight); Java2DFrameConverter converter = new Java2DFrameConverter(); BufferedImage fecthedImage = converter.getBufferedImage(f); BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); bi.getGraphics().drawImage(fecthedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); ImageIO.write(bi, "png", targetFile); // 查看stop源码会自动关流 ff.stop(); return sourcePath; } }