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) {intCountpage =0; Try { //Instructiopath = "D:/instructio/2015-05-16/android 4 Programming Primer Classic. pdf"//PicturePath = "d:/instructio/picture/2015-05-16/"; File File=NewFile (Instructiopath); Randomaccessfile RAF=NewRandomaccessfile (file, "R"); FileChannel Channel=Raf.getchannel (); Mappedbytebuffer buf=Channel.map (FileChannel.MapMode.READ_ONLY,0, Channel.size ()); Pdffile Pdffile=NewPdffile (BUF); //Create a picture folderFile Dirfile =NewFile (PicturePath); if(!dirfile.exists ()) {Dirfile.mkdirs (); } //Get Picture PagesCountpage =pdffile.getnumpages (); for(inti = 1; I <= pdffile.getnumpages (); i++) {pdfpage page=pdffile.getpage (i); Rectangle rect=NewRectangle (0, 0, ((int) Page.getbbox (). GetWidth ()), ((int) Page.getbbox (). GetHeight ()); intn = 2; /**image sharpness (n>0 and n<7) "pdf magnification parameter"*/Image img= Page.getimage (Rect.width * N, Rect.height *N, Rect,/**enlarge the PDF to n times to create the picture. */ NULL,/**null for the ImageObserver*/ true,/**fill background with white*/ true /**block until drawing is done*/ ); BufferedImage Tag=NewBufferedImage (Rect.width *N, Rect.height*N, Bufferedimage.type_int_rgb); Tag.getgraphics (). DrawImage (IMG,0, 0, Rect.width *N, Rect.heightNNULL); /*** File Imgfile = new file ("d:\\work\\mybook\\filesnew\\img\\" + * i + ". jpg"); if (i Mgfile.exists ()) {* if (Imgfile.createnewfile ()) {System.out.println ("Create Picture:" + * "D:\\wor K\\mybook\\filesnew\\img\\ "+ i +". jpg "); } else {* SYSTEM.OUT.PRINTLN ("Create Picture Failed! "); } } */FileOutputStream out=NewFileOutputStream (picturepath+ "/" +I+ ". png"); /**output to file stream*/JPEGImageEncoder Encoder=Jpegcodec.createjpegencoder (out); JPEGEncodeParam param2=Encoder.getdefaultjpegencodeparam (tag); Param2.setquality (1f,true); /**1f~0.01f is to improve the resulting image quality*/Encoder.setjpegencodeparam (param2); Encoder.encode (tag); /**JPEG encoding*/Out.close (); } channel.close (); Raf.close (); Unmap (BUF); /**If you want to delete the PDF after you turn the picture, you have to close the stream and empty the buffer.*/ } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } returnCountpage; } @SuppressWarnings ("Unchecked") Public Static voidUnmapFinalObject Buffer) {accesscontroller.doprivileged (Newprivilegedaction () { PublicObject Run () {Try{Method Getcleanermethod=Buffer.getclass (). GetMethod ("Cleaner",NewClass[0]); Getcleanermethod.setaccessible (true); Sun.misc.Cleaner Cleaner=(Sun.misc.Cleaner) Getcleanermethod. Invoke (buffer,NewObject[0]); Cleaner.clean (); } Catch(Exception e) {e.printstacktrace (); } return NULL; } }); } }
Java implementation pdf cut into pictures by page