KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > clown > samples > ImageExtractionSample


1 package it.stefanochizzolini.clown.samples;
2
3 import it.stefanochizzolini.clown.bytes.FileInputStream;
4 import it.stefanochizzolini.clown.bytes.IBuffer;
5 import it.stefanochizzolini.clown.objects.*;
6 import it.stefanochizzolini.clown.files.File;
7 import it.stefanochizzolini.clown.tokens.FileFormatException;
8 import java.io.RandomAccessFile JavaDoc;
9
10 /**
11   This sample demonstrates how to extract XObject images from a PDF file.
12   <h3>Remarks</h3>
13   <p>Inline images are ignored.</p>
14 */

15 public class ImageExtractionSample
16   implements ISample
17 {
18   public void run(
19     PDFClownSampleLoader loader
20     )
21   {
22     /*
23       NOTE: This procedure is made up of this sequence of actions:
24       1. User choice.
25       2. Document editing.
26       2.1. Indirect objects collection iteration.
27       2.1.1. Image export.
28     */

29
30     // 1. User choice.
31
String JavaDoc filePath = loader.getPdfFileChoice("Please select a PDF file");
32
33     // 2. Document editing.
34
FileInputStream inputStream;
35     try
36     {
37       // Open the file stream!
38
inputStream = new FileInputStream(
39         new RandomAccessFile JavaDoc(filePath,"r")
40         );
41     }
42     catch(Exception JavaDoc e)
43     {throw new RuntimeException JavaDoc(filePath + " file access error.",e);}
44     File file;
45     try
46     {
47       // Open the PDF file!
48
file = new File(inputStream);
49     }
50     catch(FileFormatException e)
51     {throw new RuntimeException JavaDoc(filePath + " file has a bad file format.",e);}
52
53     // 2.1. Iterating through the indirect object collection...
54
int index = 0;
55     for(PdfIndirectObject indirectObject : file.getIndirectObjects())
56     {
57       // Get the data object associated to the indirect object!
58
PdfDataObject dataObject = indirectObject.getDataObject();
59       // Is this data object a stream?
60
if(dataObject instanceof PdfStream)
61       {
62         PdfDictionary header = ((PdfStream)dataObject).getHeader();
63         // Is this stream an image?
64
if(header.containsKey(PdfName.Type)
65           && header.get(PdfName.Type).equals(PdfName.XObject)
66           && header.get(PdfName.Subtype).equals(PdfName.Image))
67         {
68           // 2.1.1. Is this image a pass-through JPEG image?
69
if(header.get(PdfName.Filter).equals(PdfName.DCTDecode)) // JPEG image.
70
{
71             // Get the image data (keeping it encoded)!
72
IBuffer body = ((PdfStream)dataObject).getBody(false);
73             // Export the image!
74
exportImage(
75               body,
76               loader.getOutputPath() + java.io.File.separator + "ImageExtractionSample_" + (index++) + ".jpg"
77               );
78           }
79           else // Unsupported image.
80
{
81             System.out.println("Image XObject " + indirectObject.getReference() + " couldn't be extracted (filter: " + header.get(PdfName.Filter) + ")");
82           }
83         }
84       }
85     }
86   }
87
88   private void exportImage(
89     IBuffer data,
90     String JavaDoc outputPath
91     )
92   {
93     java.io.File JavaDoc outputFile = new java.io.File JavaDoc(outputPath);
94     java.io.BufferedOutputStream JavaDoc outputStream;
95     try
96     {
97       outputFile.createNewFile();
98       outputStream = new java.io.BufferedOutputStream JavaDoc(
99         new java.io.FileOutputStream JavaDoc(outputFile)
100         );
101     }
102     catch(Exception JavaDoc e)
103     {throw new RuntimeException JavaDoc(outputFile.getPath() + " file couldn't be created.",e);}
104
105     try
106     {
107       outputStream.write(data.toByteArray());
108       outputStream.flush();
109       outputStream.close();
110     }
111     catch(Exception JavaDoc e)
112     {throw new RuntimeException JavaDoc(outputFile.getPath() + " file writing has failed.",e);}
113
114     System.out.println("Output: " + outputFile.getPath());
115   }
116 }
Popular Tags