public static void createThumbnail2(File file) throws Exception{
String name = file.getName();
String path = file.getPath();
String md5 = ShiroUtil.md5(path, "123");
if(name.contains(".")){
//后缀
long thumbnail_start = System.currentTimeMillis();
long thumbnail_end = 0;
String substr = name.substring(name.lastIndexOf(".")+1);
if("mov".equals(substr)||"mp4".equals(substr)||"m4a".equals(substr)||"3gp".equals(substr)||"3g2".equals(substr)||"mj2".equals(substr)){
MediaPreview.videoImage(path,MyConfig.thumbnailPath+md5);
}
if("pdf".equals(substr)){
thumbnail_end = ToPdfUtils.tranfer(path, MyConfig.thumbnailPath+md5+".png", 1);
}
//如果不是PDF文档,则生成PDF文件,然后再生成缩略图.
// if("doc".equals(substr)||"docx".equals(substr)||"txt".equals(substr)){
// ToPdfUtils.doc2pdf(path, MyConfig.pdfPath+md5+".pdf");
// thumbnail_end = ToPdfUtils.tranfer(MyConfig.pdfPath+md5+".pdf", MyConfig.thumbnailPath+md5+".png", 1);
// }
//
// if("xls".equals(substr)){
// ToPdfUtils.excel2pdf(path, MyConfig.pdfPath+md5+".pdf");
// thumbnail_end = ToPdfUtils.tranfer(MyConfig.pdfPath+md5+".pdf", MyConfig.thumbnailPath+md5+".png", 1);
// }
//ppt转换pdf
// if("xls".equals(substr)){
// ToPdfUtils.excel2pdf(path, MyConfig.pdfPath+docId+".pdf");
// }
if(thumbnail_end!=0){
System.err.println("缩略图生成花费时间:"+(thumbnail_end-thumbnail_start)/1000);
}
}else{
// System.err.println("不创建缩略图");
}
}
/**
* 截取视频第六帧的图片 mov,mp4,m4a,3gp,3g2,mj2
* @param filePath 视频路径
* @param dir 文件存放的根目录
* @return 图片的相对路径 例:pic/1.png
* @throws FrameGrabber.Exception
*/
@ZhName(zhName="为视频生成缩略图")
public static String videoImage(String filePath,String dir) throws FrameGrabber.Exception {
String pngPath = "";
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
ff.start();
int ffLength = ff.getLengthInFrames();
Frame f;
int i = 0;
while (i < ffLength) {
f = ff.grabImage();
//截取第6帧
if((i>5) && (f.image != null)){
//生成图片的相对路径 例如:pic/uuid.png
pngPath = dir+".png";
//执行截图并放入指定位置
doExecuteFrame(f, pngPath);
break;
}
i++;
}
ff.stop();
return pngPath;
}
/**
* 截取缩略图
* @param f Frame
* @param targerFilePath:封面图片存放路径
*/
@ZhName(zhName="为视频生成缩略图")
private static void doExecuteFrame(Frame f, String targerFilePath) {
String imagemat = "png";
if (null == f || null == f.image) {
return;
}
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bi = converter.getBufferedImage(f);
File output = new File(targerFilePath);
try {
ImageIO.write(bi, imagemat, output);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将指定pdf文件的首页转换为指定路径的缩略图
*@param filepath 原文件路径,例如d:/test.pdf
*@param imagepath 图片生成路径,例如 d:/test-1.jpg
*@param zoom 缩略图显示倍数,1表示不缩放,0.3则缩小到30%
*/
@ZhName(zhName="将指定pdf文件的首页转换为指定路径的缩略图")
public static long tranfer(String filepath, String imagepath, float zoom) throws IOException, PDFException, PDFSecurityException ,InterruptedException{
// ICEpdf document class
Document document = null;
float rotation = 0f;
document = new Document();
document.setFile(filepath);
// maxPages = document.getPageTree().getNumberOfPages();
BufferedImage img = (BufferedImage) document.getPageImage(0, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, zoom);
Iterator iter = ImageIO.getImageWritersBySuffix("png");
ImageWriter writer = (ImageWriter) iter.next();
File outFile = new File(imagepath);
FileOutputStream out = new FileOutputStream(outFile);
ImageOutputStream outImage = ImageIO.createImageOutputStream(out);
writer.setOutput(outImage);
writer.write(new IIOImage(img, null, null));
out.close();
outImage.close();
System.out.println("缩略图生成成功成功!!!");
return System.currentTimeMillis();
}
PDF生成缩略图,视频生成缩略图
猜你喜欢
转载自blog.csdn.net/yanzi920403/article/details/88972516
今日推荐
周排行