Accelerer lecture fichier PDF
Bonjour,
J'ai un programme qui ouvre des fichiers pdf, et qui affiche des miniatures de ceux ci.
J'utilise pdfRenderer pour afficher les images.
je problème c'est que l'affichage des images est relativement long, je pense que cela provient de la lecture des fichier pdf.
j’essaie d'utiliser AsynchronousFileChannel pour lire le fichier (il parait que c'est plus rapide) mais je parviens pas a dimensionner le bytebuffer.
Si quelqu'un a une idée pour accélérer le chargement des PDF, je suis preneur. Je n'ai peut être pas la bonne approche.
méthode lente :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| private Image getImageFromPdf(String fileUrl) throws IOException {
//load a pdf from a byte buffer
File file = new File(fileUrl);
@SuppressWarnings("resource")
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
// draw the first page to an image
PDFPage page = pdffile.getPage(0);
//get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0,0, (int)page.getWidth(),(int)page.getHeight());
//generate the image
Image result = page.getImage(
rect.width, rect.height, //width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
).getScaledInstance(largeur, hauteur, BufferedImage.SCALE_SMOOTH);// redimentionne l'image au format de la jpanel
return result;
} |
méthode supposée plus rapide mais qui ne marche pas:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| private Image getImageFromPdf(String fileUrl) throws Exception {
//load a pdf from a byte buffer
AsynchronousFileChannel channel = AsynchronousFileChannel.open(new File(fileUrl).toPath(), StandardOpenOption.READ);
ByteBuffer buf = null; // ou ByteBuffer.allocate(int) mais je ne connais pas la taille du buffer
Future<Integer> fut = channel.read(buf, 0L);
fut.get();
PDFFile pdffile = new PDFFile(buf);
// draw the first page to an image
PDFPage page = pdffile.getPage(0);
//get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0,0, (int)page.getWidth(),(int)page.getHeight());
//generate the image
Image result = page.getImage(
rect.width, rect.height, //width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
).getScaledInstance(largeur, hauteur, BufferedImage.SCALE_SMOOTH);// redimentionne l'image au format de la jpanel
return result;
} |
en m'inspirant de ca :
http://www.java2s.com/Code/Java/JDK-...turetoread.htm
Merci.