最近用java對(duì)word三大辦公軟件和視頻進(jìn)行縮略圖展示,用到了spire這個(gè)插件,比較簡(jiǎn)單我直接展示下代碼,主要就是那個(gè)jar包得自己打包,我自己使用pom沒(méi)法直接導(dǎo)入(即使指定了網(wǎng)址),所以我這邊只展示代碼了。
1.word縮略圖
/**
* word獲取縮略圖
*
* @param wordFile word文件地址
* @param outputImgPath 輸出圖片目錄
* @return 圖片路徑
* @create 2023-01-09
*/
private static String wordToImage(String wordFile, String outputImgPath) throws Exception {
Document word = new Document();
word.loadFromFile(wordFile);
String fileName = getFileName(wordFile);
BufferedImage image = word.saveToImages(0, ImageType.Bitmap);
String imgUrl = outputImgPath + fileName + (0 + 1) + ".png";
File file = new File(imgUrl);
ImageIO.write(image, "PNG", file);
log.info("word縮略圖獲取完成,圖片目錄:[{}]", imgUrl);
return imgUrl;
}
2.ppt縮略圖
/**
* ppt獲取縮略圖
*
* @param pptFile ppt文件地址
* @param outputImgPath 輸出圖片目錄
* @return 圖片路徑
* @create 2023-01-09
*/
private static String pptToImage(String pptFile, String outputImgPath) throws Exception {
Presentation ppt = new Presentation();
ppt.loadFromFile(pptFile);
String fileName = getFileName(pptFile);
BufferedImage image = ppt.getSlides().get(0).saveAsImage();
String imgUrl = outputImgPath + fileName + (0 + 1) + ".png";
ImageIO.write(image, "PNG", new File(imgUrl));
ppt.dispose();
log.info("ppt縮略圖獲取完成,圖片目錄:[{}]", imgUrl);
return imgUrl;
}
3.pdf縮略圖
/**
* pdf獲取縮略圖
*
* @param pdfFile pdf文件地址
* @param outputImgPath 輸出圖片目錄
* @return 圖片路徑
* @create 2023-01-09
*/
private static String pdfToImage(String pdfFile, String outputImgPath) throws Exception {
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile(pdfFile);
String fileName = getFileName(pdfFile);
BufferedImage image = pdf.saveAsImage(0, PdfImageType.Bitmap);
String imgUrl = outputImgPath + fileName + (0 + 1) + ".png";
File file = new File(imgUrl);
ImageIO.write(image, "PNG", file);
log.info("pdf縮略圖獲取完成,圖片目錄:[{}]", imgUrl);
return imgUrl;
}
4.video縮略圖
/**
* 截取視頻第一幀的圖片
*
* @param videoFile 視頻路徑
* @param outputImgPath 文件存放的根目錄
* @return 圖片路徑
* @throws FrameGrabber.Exception
*/
private static String videoImage(String videoFile, String outputImgPath) throws FrameGrabber.Exception {
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(videoFile);
ff.start();
Frame f;
f = ff.grabImage();
//保存文件名稱
String pngPath = getFileName(videoFile) + ".png";
//最終圖片路徑
String resPath = outputImgPath + pngPath;
//截取縮略圖
String imageMat = "PNG";
if (null == f || null == f.image) {
return "";
}
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(f);
File output = new File(resPath);
try {
ImageIO.write(bufferedImage, imageMat, output);
} catch (IOException e) {
e.printStackTrace();
}
log.info("video縮略圖獲取完成,圖片目錄:[{}]", resPath);
ff.stop();
return resPath;
}
5.功能函數(shù)
這個(gè)主要就是生成的縮略圖的文件命名。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-636224.html
/**
* 增加時(shí)間戳的文件名(不帶后綴)
*
* @param filePath 文件路徑
* @return java.lang.String
* @create 2023-01-09
*/
private static String getFileName(String filePath) {
String fileName = new File(filePath).getName();
String res = System.currentTimeMillis() + "-" + fileName.substring(0, fileName.lastIndexOf("."));
return res;
}
/**
*縮略圖通用方法
*/
public static String generateThumbnail(String file, String outputImgPath) throws Exception {
if (!outputImgPath.endsWith(File.separator)) {
//如果不是斜杠結(jié)尾增加
outputImgPath += File.separator;
}
String filePath = "";
if (file.endsWith("doc") | file.endsWith("docx")) {
filePath = wordToImage(file, outputImgPath);
} else if (file.endsWith("ppt") | file.endsWith("pptx")) {
filePath = pptToImage(file, outputImgPath);
} else if (file.endsWith("pdf")) {
filePath = pdfToImage(file, outputImgPath);
} else if (file.endsWith("mp4")) {
filePath = videoImage(file, outputImgPath);
}
return filePath;
}
import com.spire.doc.Document;
import com.spire.doc.documents.ImageType;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfImageType;
import com.spire.presentation.Presentation;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* 類(lèi)描述:縮略圖工具類(lèi)
*
* @ClassName ThumbnailUtil
* @Author ward
* @Date 2023-01-06 17:46
*/
@Slf4j
public class ThumbnailUtil {
/**
* 增加時(shí)間戳的文件名(不帶后綴)
*
* @param filePath 文件路徑
* @return java.lang.String
* @create 2023-01-09
*/
private static String getFileName(String filePath) {
String fileName = new File(filePath).getName();
String res = System.currentTimeMillis() + "-" + fileName.substring(0, fileName.lastIndexOf("."));
return res;
}
/**
* word獲取縮略圖
*
* @param wordFile word文件地址
* @param outputImgPath 輸出圖片目錄
* @return 圖片路徑
* @create 2023-01-09
*/
private static String wordToImage(String wordFile, String outputImgPath) throws Exception {
Document word = new Document();
word.loadFromFile(wordFile);
String fileName = getFileName(wordFile);
BufferedImage image = word.saveToImages(0, ImageType.Bitmap);
String imgUrl = outputImgPath + fileName + (0 + 1) + ".png";
File file = new File(imgUrl);
ImageIO.write(image, "PNG", file);
log.info("word縮略圖獲取完成,圖片目錄:[{}]", imgUrl);
return imgUrl;
}
/**
* ppt獲取縮略圖
*
* @param pptFile ppt文件地址
* @param outputImgPath 輸出圖片目錄
* @return 圖片路徑
* @create 2023-01-09
*/
private static String pptToImage(String pptFile, String outputImgPath) throws Exception {
Presentation ppt = new Presentation();
ppt.loadFromFile(pptFile);
String fileName = getFileName(pptFile);
BufferedImage image = ppt.getSlides().get(0).saveAsImage();
String imgUrl = outputImgPath + fileName + (0 + 1) + ".png";
ImageIO.write(image, "PNG", new File(imgUrl));
ppt.dispose();
log.info("ppt縮略圖獲取完成,圖片目錄:[{}]", imgUrl);
return imgUrl;
}
/**
* pdf獲取縮略圖
*
* @param pdfFile pdf文件地址
* @param outputImgPath 輸出圖片目錄
* @return 圖片路徑
* @create 2023-01-09
*/
private static String pdfToImage(String pdfFile, String outputImgPath) throws Exception {
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile(pdfFile);
String fileName = getFileName(pdfFile);
BufferedImage image = pdf.saveAsImage(0, PdfImageType.Bitmap);
String imgUrl = outputImgPath + fileName + (0 + 1) + ".png";
File file = new File(imgUrl);
ImageIO.write(image, "PNG", file);
log.info("pdf縮略圖獲取完成,圖片目錄:[{}]", imgUrl);
return imgUrl;
}
/**
* 截取視頻第六幀的圖片
*
* @param videoFile 視頻路徑
* @param outputImgPath 文件存放的根目錄
* @return 圖片路徑
* @throws FrameGrabber.Exception
*/
private static String videoImage(String videoFile, String outputImgPath) throws FrameGrabber.Exception {
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(videoFile);
ff.start();
Frame f;
f = ff.grabImage();
//保存文件名稱
String pngPath = getFileName(videoFile) + ".png";
//最終圖片路徑
String resPath = outputImgPath + pngPath;
//截取縮略圖
String imageMat = "PNG";
if (null == f || null == f.image) {
return "";
}
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(f);
File output = new File(resPath);
try {
ImageIO.write(bufferedImage, imageMat, output);
} catch (IOException e) {
e.printStackTrace();
}
log.info("video縮略圖獲取完成,圖片目錄:[{}]", resPath);
ff.stop();
return resPath;
}
public static String generateThumbnail(String file, String outputImgPath) throws Exception {
if (!outputImgPath.endsWith(File.separator)) {
//如果不是斜杠結(jié)尾增加
outputImgPath += File.separator;
}
String filePath = "";
if (file.endsWith("doc") | file.endsWith("docx")) {
filePath = wordToImage(file, outputImgPath);
} else if (file.endsWith("ppt") | file.endsWith("pptx")) {
filePath = pptToImage(file, outputImgPath);
} else if (file.endsWith("pdf")) {
filePath = pdfToImage(file, outputImgPath);
} else if (file.endsWith("mp4")) {
filePath = videoImage(file, outputImgPath);
}
return filePath;
}
public static void main(String[] args) throws Exception {
String path = generateThumbnail("D:\\desktop\\test\\video.mp4",
"D:\\desktop\\test\\img");
System.out.println(path);
}
}
6.異常情況
項(xiàng)目部署在linux上,生成縮略圖的時(shí)候部分字體就會(huì)顯示不了,如下圖。后來(lái)發(fā)現(xiàn)是linux缺少中文字體導(dǎo)致的,這里介紹下導(dǎo)入中文字體的方法。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-636224.html
6.1安裝軟件
#安裝fontconfig和mkfontscale工具
yum install -y fontconfig
yum install -y mkfontscale
#創(chuàng)建字體文件夾(存中文字體)
mkdir -p /usr/share/fonts/chinese/
#給與權(quán)限
chmod -R 775 /usr/share/fonts/chinese/
#接下來(lái)就是把你電腦里的C:\Windows\Fonts路徑下的全部壓縮上傳解壓到剛剛創(chuàng)建的文件夾里
#進(jìn)入文件夾,以下命令要進(jìn)入文件夾下
cd /usr/share/fonts
#字體擴(kuò)展
mkfontscale
#新增字體目錄
mkfontdir
#刷新緩存
fc-cache -fv
#查看字體
fc-list :lang=zh
到了這里,關(guān)于Java實(shí)現(xiàn)doc、ppt、pdf和視頻的縮略圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!