欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

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

在大冶等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,網(wǎng)絡(luò)營銷推廣,外貿(mào)網(wǎng)站制作,大冶網(wǎng)站建設(shè)費(fèi)用合理。

首先下載相關(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:/臨時(shí)文件1.pdf";
 String picturepath = "E:/臨時(shí)文件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,就必須要這個(gè)關(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

新聞標(biāo)題:java使用renderer將pdf按頁轉(zhuǎn)換為圖片
網(wǎng)站網(wǎng)址:http://aaarwkj.com/article46/gjcdhg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、小程序開發(fā)、網(wǎng)頁設(shè)計(jì)公司自適應(yīng)網(wǎng)站、網(wǎng)站建設(shè)靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
欧美大片免费在线播放| 在线天堂一区二区三区| 五月婷婷丁香噜噜噜噜| 麻豆影片在线免费观看| 日本束缚人妻一区二区三区| 亚洲国产欧美日韩在线不卡成人 | 成人免费毛片内射视频| 男人自拍天堂在线视频| 人人狠狠综合久久亚洲| av资源中文字幕在线天堂| 亚洲天堂免费在线播放| 精品久久久久久久久极品| 传媒视频在线观看网站| 日韩高清不卡免费视频| 精品一区二区三区推荐| 在线免费观看午夜视频| 在线观看男人的天堂av| 女同伦理视频在线观看| 亚洲国产精品成人久久蜜臀| 超碰欧美性欧美最猛性| 中文字幕日韩一区二区| 五月天丁香婷婷狠狠狠| 熟妇人妻精品一区二区三区颏| 日本h电影一区二区三区| 精品一区二区在线欧美日韩| 国产一区 亚洲精品| av人妻熟女少妇蒂亚| 亚洲日本欧美一区二区| 青娱乐青青草91在线| 2020亚洲欧美日韩在线| 91久久精品国产免费一区| 91黄色国产在线播放| 欧美日韩免费一区二三区| 最新国产成人免费在线视频| 天天操天天射夜夜撸| 国产成人综合亚洲国产| 91欧美在线激情视频| 在线观看91精品国产秒播| 国产欧美日韩另类视频| 成人精品播放视频在线观看| 国产又猛又黄又爽无遮挡|