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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值