PDF生成縮略圖,視頻生成縮略圖

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章