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

java實(shí)現(xiàn)pdf按頁轉(zhuǎn)換為圖片

 更新時間:2018年12月19日 14:59:30   作者:huanshirenjian  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)pdf按頁轉(zhuǎn)換為圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)pdf按頁轉(zhuǎn)換為圖片的具體代碼,供大家參考,具體內(nèi)容如下

本程序是利用jacob.jar包實(shí)現(xiàn)的,關(guān)于jacob.jar的配置見我上一篇文章,程序中可配置參數(shù)選擇圖片清晰圖。

package core.util;
 
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;
 
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 PDFchangToImage {
 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);
 /** 如果要在轉(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;
 }
 });
 }
}

如果需要將word轉(zhuǎn)pdf,也可參考我上一篇文章。

相關(guān)文章

  • Spring Boot報錯:No session repository could be auto-configured, check your configuration的解決方法

    Spring Boot報錯:No session repository could be auto-configured

    這篇文章主要給大家介紹了關(guān)于Spring Boot報錯:No session repository could be auto-configured, check your configuration的解決方法,文中給出了詳細(xì)的解決方法,對遇到這個問題的朋友們具有一定參考價值,需要的朋友下面來一起看看吧。
    2017-07-07
  • 一小時迅速入門Mybatis之Prepared Statement與符號的使用

    一小時迅速入門Mybatis之Prepared Statement與符號的使用

    這篇文章主要介紹了一小時迅速入門Mybatis之Prepared Statement與符號的使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • 一文了解Seata的實(shí)現(xiàn)原理

    一文了解Seata的實(shí)現(xiàn)原理

    隨著業(yè)務(wù)發(fā)展,單體系統(tǒng)逐漸無法滿足業(yè)務(wù)的需求,分布式架構(gòu)逐漸成為大型互聯(lián)網(wǎng)平臺首選。伴隨而來的問題是,本地事務(wù)方案已經(jīng)無法滿足,分布式事務(wù)相關(guān)規(guī)范和框架應(yīng)運(yùn)而生。本文主要介紹Seata的實(shí)現(xiàn)原理
    2021-06-06
  • Spring?Cloud?Eureka高可用配置(踩坑記錄)

    Spring?Cloud?Eureka高可用配置(踩坑記錄)

    在進(jìn)行Eureka高可用配置時,控制臺一直出現(xiàn)“......”的錯誤,但是在瀏覽器中輸入地址:peer1:8761 卻是可正常運(yùn)行,這篇文章主要介紹了Spring?Cloud踩坑之Eureka高可用配置,需要的朋友可以參考下
    2023-08-08
  • java線程池使用后到底要關(guān)閉嗎

    java線程池使用后到底要關(guān)閉嗎

    這篇文章主要給大家介紹了關(guān)于java線程池使用后到底要不要關(guān)閉的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Vue3源碼解讀effectScope API及實(shí)現(xiàn)原理

    Vue3源碼解讀effectScope API及實(shí)現(xiàn)原理

    這篇文章主要為大家介紹了Vue3源碼解讀effectScope API及實(shí)現(xiàn)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • java對象初始化代碼詳解

    java對象初始化代碼詳解

    這篇文章主要介紹了java對象初始化代碼詳解,涉及實(shí)例變量的初始化,類變量的初始化等相關(guān)介紹幾代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • 使用Spring AOP監(jiān)控指定方法執(zhí)行時間的代碼詳解

    使用Spring AOP監(jiān)控指定方法執(zhí)行時間的代碼詳解

    這篇文章主要介紹了使用Spring AOP監(jiān)控指定方法執(zhí)行時間,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • 自定義JmsListenerContainerFactory時,containerFactory字段解讀

    自定義JmsListenerContainerFactory時,containerFactory字段解讀

    這篇文章主要介紹了自定義JmsListenerContainerFactory時,containerFactory字段解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Spring boot整合log4j2過程解析

    Spring boot整合log4j2過程解析

    這篇文章主要介紹了Spring boot整合log4j2過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12

最新評論