packagecom.ces.component.pictrueCut.entity;importjava.awt.Image;importjava.awt.Rectangle;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.RandomAccessFile;importjava.lang.reflect.Method;importjava.nio.MappedByteBuffer;importjava.nio.channels.FileChannel;importjava.security.AccessController;importjava.security.PrivilegedAction;importcom.sun.image.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGEncodeParam;importcom.sun.image.codec.jpeg.JPEGImageEncoder;importcom.sun.pdfview.PDFFile;importcom.sun.pdfview.PDFPage;public classpdfToImage {public static intchangePdfToImg(String instructiopath,String picturepath) {int countpage =0;try{//instructiopath ="D:/instructio/2015-05-16/Android 4编程入门经典.pdf"//picturepath = "D:/instructio/picture/2015-05-16/";
File file= newFile(instructiopath);
RandomAccessFile raf= new RandomAccessFile(file, "r");
FileChannel channel=raf.getChannel();
MappedByteBuffer buf=channel.map(FileChannel.MapMode.READ_ONLY,0, channel.size());
PDFFile pdffile= newPDFFile(buf);//创建图片文件夹
File dirfile = newFile(picturepath);if(!dirfile.exists()){
dirfile.mkdirs();
}//获得图片页数
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放大参数】*/Image img= page.getImage(rect.width * n, rect.height *n,
rect,/**放大pdf到n倍,创建图片。*/
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("创建图片:"+
* "D:\\work\\mybook\\FilesNew\\img\\" + i + ".jpg"); } else {
* System.out.println("创建图片失败!"); } }*/FileOutputStream out= new FileOutputStream(picturepath+"/" +i+ ".png");/**输出到文件流*/JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param2=encoder.getDefaultJPEGEncodeParam(tag);
param2.setQuality(1f,true);/**1f~0.01f是提高生成的图片质量*/encoder.setJPEGEncodeParam(param2);
encoder.encode(tag);/**JPEG编码*/out.close();
}
channel.close();
raf.close();
unmap(buf);/**如果要在转图片之后删除pdf,就必须要这个关闭流和清空缓冲的方法*/}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}returncountpage;
}
@SuppressWarnings("unchecked")public static void unmap(finalObject buffer) {
AccessController.doPrivileged(newPrivilegedAction() {publicObject 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;
}
});
}
}