01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17: package org.apache.poi.hslf.extractor;
18:
19: import org.apache.poi.hslf.usermodel.SlideShow;
20: import org.apache.poi.hslf.usermodel.PictureData;
21: import org.apache.poi.hslf.HSLFSlideShow;
22: import org.apache.poi.hslf.model.Picture;
23:
24: import java.io.IOException;
25: import java.io.FileOutputStream;
26:
27: /**
28: * Utility to extract pictures from a PowerPoint file.
29: *
30: * @author Yegor Kozlov
31: */
32: public class ImageExtractor {
33: public static void main(String args[]) throws IOException {
34: if (args.length < 1) {
35: System.err.println("Usage:");
36: System.err.println("\tImageExtractor <file>");
37: return;
38: }
39: SlideShow ppt = new SlideShow(new HSLFSlideShow(args[0]));
40:
41: //extract all pictures contained in the presentation
42: PictureData[] pdata = ppt.getPictureData();
43: for (int i = 0; i < pdata.length; i++) {
44: PictureData pict = pdata[i];
45:
46: // picture data
47: byte[] data = pict.getData();
48:
49: int type = pict.getType();
50: String ext;
51: switch (type) {
52: case Picture.JPEG:
53: ext = ".jpg";
54: break;
55: case Picture.PNG:
56: ext = ".png";
57: break;
58: case Picture.WMF:
59: ext = ".wmf";
60: break;
61: case Picture.EMF:
62: ext = ".emf";
63: break;
64: case Picture.PICT:
65: ext = ".pict";
66: break;
67: case Picture.DIB:
68: ext = ".dib";
69: break;
70: default:
71: continue;
72: }
73: FileOutputStream out = new FileOutputStream("pict_" + i
74: + ext);
75: out.write(data);
76: out.close();
77: }
78: }
79: }
|