欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用renderer將pdf按頁轉(zhuǎn)換為圖片

 更新時間:2018年12月19日 12:02:32   作者:papima  
這篇文章主要為大家詳細(xì)介紹了java使用renderer將pdf按頁轉(zhuǎn)換為圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下

項(xiàng)目中遇到了需要把用戶上傳的word,execl,ppt每頁截圖保存。需要先用到j(luò)acob把資源轉(zhuǎn)換為pdf,在通過pdf-renderer把每頁截圖下來。

首先下載相關(guān)jar包:下載地址

import java.awt.Image; 
import java.awt.Rectangle; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.lang.reflect.Method; 
import java.nio.MappedByteBuffer; 
import java.nio.channels.FileChannel; 
import java.security.AccessController; 
import java.security.PrivilegedAction; 
//如果com.sun.image找不到,就是Eclipse默認(rèn)把這些受訪問限制的API設(shè)成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)選為Warning就可以編譯通過
import com.sun.image.codec.jpeg.JPEGCodec; 
import com.sun.image.codec.jpeg.JPEGEncodeParam; 
import com.sun.image.codec.jpeg.JPEGImageEncoder; 
import com.sun.pdfview.PDFFile; 
import com.sun.pdfview.PDFPage; 
public class pdfToImage {
  
 public static void main(String[] args) {
 String instructiopath="E:/臨時文件1.pdf";
 String picturepath = "E:/臨時文件1/";
 changePdfToImg(instructiopath,picturepath);
 }
 
 
  public static int changePdfToImg(String instructiopath,String picturepath) { 
    int countpage =0; 
    try { 
      //instructiopath ="D:/instructio/2015-05-16/Android 4編程入門經(jīng)典.pdf" 
      //picturepath = "D:/instructio/picture/2015-05-16/"; 
       
      File file = new File(instructiopath); 
      RandomAccessFile raf = new RandomAccessFile(file, "r"); 
      FileChannel channel = raf.getChannel(); 
      MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 
          0, channel.size()); 
      PDFFile pdffile = new PDFFile(buf); 
      //創(chuàng)建圖片文件夾 
      File dirfile = new File(picturepath); 
        if(!dirfile.exists()){ 
           dirfile.mkdirs(); 
        } 
      //獲得圖片頁數(shù) 
      countpage = pdffile.getNumPages(); 
      for (int i = 1; i <= pdffile.getNumPages(); i++) { 
        PDFPage page = pdffile.getPage(i); 
        Rectangle rect = new Rectangle(0, 0, ((int) page.getBBox() 
            .getWidth()), ((int) page.getBBox().getHeight())); 
        int n = 2; 
        /** 圖片清晰度(n>0且n<7)【pdf放大參數(shù)】 */ 
        Image img = page.getImage(rect.width * n, rect.height * n, 
            rect, /** 放大pdf到n倍,創(chuàng)建圖片。 */ 
            null, /** null for the ImageObserver */ 
            true, /** fill background with white */ 
            true /** block until drawing is done */ 
        ); 
        BufferedImage tag = new BufferedImage(rect.width * n, 
            rect.height * n, BufferedImage.TYPE_INT_RGB); 
        tag.getGraphics().drawImage(img, 0, 0, rect.width * n, 
            rect.height * n, null); 
        /** 
         * File imgfile = new File("D:\\work\\mybook\\FilesNew\\img\\" + 
         * i + ".jpg"); if(imgfile.exists()){ 
         * if(imgfile.createNewFile()) { System.out.println("創(chuàng)建圖片:"+ 
         * "D:\\work\\mybook\\FilesNew\\img\\" + i + ".jpg"); } else { 
         * System.out.println("創(chuàng)建圖片失??!"); } } 
         */ 
        FileOutputStream out = new FileOutputStream(picturepath+"/" + i 
            + ".png"); 
        /** 輸出到文件流 */ 
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
        JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag); 
        param2.setQuality(1f, true); 
        /** 1f~0.01f是提高生成的圖片質(zhì)量 */ 
        encoder.setJPEGEncodeParam(param2); 
        encoder.encode(tag); 
        /** JPEG編碼 */ 
        out.close(); 
      } 
      channel.close(); 
      raf.close(); 
      /*unmap(buf);*/  //pdf轉(zhuǎn)化成圖片后,釋放MappedByteBuffer資源。調(diào)用unmap(buf);無效。
      /** 如果要在轉(zhuǎn)圖片之后刪除pdf,就必須要這個關(guān)閉流和清空緩沖的方法 */ 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    return countpage; 
     
  } 
 
  @SuppressWarnings("unchecked") 
  public static void unmap(final Object buffer) { 
    AccessController.doPrivileged(new PrivilegedAction() { 
      public Object run() { 
        try { 
          Method getCleanerMethod = buffer.getClass().getMethod( 
              "cleaner", new Class[0]); 
          getCleanerMethod.setAccessible(true); 
          sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod 
              .invoke(buffer, new Object[0]); 
          cleaner.clean(); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
        return null; 
      } 
    }); 
  } 
}

成功釋放MappedByteBuffer資源

Method m = FileChannelImpl.class.getDeclaredMethod("unmap",
    MappedByteBuffer.class);
   m.setAccessible(true);
   m.invoke(FileChannelImpl.class, buf);

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Hibernate悲觀鎖和樂觀鎖實(shí)例詳解

    Hibernate悲觀鎖和樂觀鎖實(shí)例詳解

    這篇文章主要介紹了Hibernate悲觀鎖和樂觀鎖實(shí)例詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Netty分布式編碼器及寫數(shù)據(jù)事件處理使用場景

    Netty分布式編碼器及寫數(shù)據(jù)事件處理使用場景

    這篇文章主要為大家介紹了Netty分布式編碼器及寫數(shù)據(jù)事件處理使用場景剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,
    2022-03-03
  • 如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境)

    如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境)

    spark程序可以通過pom.xml的文件配置,添加spark-core依賴,可以直接在IDEA中編寫spark程序并運(yùn)行結(jié)果,這篇文章主要介紹了如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境),需要的朋友可以參考下
    2024-02-02
  • java 三角形類 Triangle的用法詳解

    java 三角形類 Triangle的用法詳解

    這篇文章主要介紹了java 三角形類 Triangle的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • java切分字符串的2種方法實(shí)例

    java切分字符串的2種方法實(shí)例

    在我們?nèi)粘9ぷ髦薪?jīng)常遇到截取字符串的需求,下面這篇文章主要給大家介紹了關(guān)于java切分字符串的2種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • java list用法示例詳解

    java list用法示例詳解

    java中可變數(shù)組的原理就是不斷的創(chuàng)建新的數(shù)組,將原數(shù)組加到新的數(shù)組中,下文對java list用法做了詳解
    2014-01-01
  • Netty分布式源碼分析監(jiān)聽讀事件

    Netty分布式源碼分析監(jiān)聽讀事件

    這篇文章主要介紹了Netty分布式監(jiān)聽讀事件方法的代碼跟蹤解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • Mybatis的幾種傳參方式詳解

    Mybatis的幾種傳參方式詳解

    這篇文章主要介紹了Mybatis的幾種傳參方式詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • java redis 實(shí)現(xiàn)簡單的用戶簽到功能

    java redis 實(shí)現(xiàn)簡單的用戶簽到功能

    這篇文章主要介紹了java redis 實(shí)現(xiàn)簡單的用戶簽到功能,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • Spring Boot中使用Activiti的方法教程(二)

    Spring Boot中使用Activiti的方法教程(二)

    工作流(Workflow),就是“業(yè)務(wù)過程的部分或整體在計(jì)算機(jī)應(yīng)用環(huán)境下的自動化”,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Activiti的相關(guān)資料,需要的朋友可以參考下
    2018-08-08

最新評論