PDF生成缩略图,视频生成缩略图

本文介绍了一种用于生成视频及多种文档格式缩略图的方法。针对视频文件,通过FFmpeg工具抓取第六帧作为缩略图;对于PDF文档,则直接转换其首页为图片。此外还探讨了对其他文档格式如DOC、XLS等生成缩略图的可能性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
	}

 

WIN7 X64 PDF缩略图 补丁软件 Introduction: This page contains simple fixes for Adobe's PDF preview handler and thumbnails on 64-bit versions of Windows. Before I looked into this, people had been complaining about it for over two years with no official response. Only the thumbnails fix is still needed, except if you upgraded from Vista to Windows 7, where both fixes may still be needed. Half a year after I published the information, Adobe finally incorporated the preview handler fix (but no thumbnails fix, and they leave the preview handler broken if you upgraded from Vista to Windows 7) into the installer for Adobe Reader 9.3.2 (April 2010). Maybe in 2011 Adobe will manage to fix the thumbnails as well; until then you can get my fix for them below. :-) Preview Handler Preview handlers are lightweight components which let you view various file types within programs like Windows Explorer (in Windows Vista and Windows 7), Outlook 2007/2010 and Directory Opus (via my bundled plugin). Adobe Reader comes with Adobe's PDF preview handler but the installer had a mistake which meant the preview handler did not work on 64-bit systems. It turned out the problem could be fixed via a simple registry change. The change is described below and a small program which performs the fix is also provided for your convenience. Unlike the thumbnail fix, described below, the preview handler fix should no longer be needed by most people, since Adobe have finally fixed their installer, but it's still provided in case it helps repair things. If you had Adobe Reader installed under Vista and later upgraded to Windows 7 then you will probably still need to run the preview handler fix once to clean up a registry value which Adobe set inconsistently between the two OS versions. If in doubt, run the fix and it will tell you if anything needs to be done. If everything is good already then the preview handler fix won't change anything.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值