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:
18: /*
19: * HSSFWorkbook.java
20: *
21: * Created on September 30, 2001, 3:37 PM
22: */
23: package org.apache.poi.hssf.usermodel;
24:
25: import junit.framework.TestCase;
26:
27: import javax.imageio.ImageIO;
28: import java.io.*;
29: import java.util.*;
30: import java.awt.image.BufferedImage;
31:
32: /**
33: * Test <code>HSSFPictureData</code>.
34: * The code to retrieve images from a workbook provided by Trejkaz (trejkaz at trypticon dot org) in Bug 41223.
35: *
36: * @author Yegor Kozlov (yegor at apache dot org)
37: * @author Trejkaz (trejkaz at trypticon dot org)
38: */
39: public class TestHSSFPictureData extends TestCase {
40:
41: static String cwd = System.getProperty("HSSF.testdata.path");
42:
43: public void testPictures() throws IOException {
44: FileInputStream is = new FileInputStream(new File(cwd,
45: "SimpleWithImages.xls"));
46: HSSFWorkbook wb = new HSSFWorkbook(is);
47: is.close();
48:
49: List lst = wb.getAllPictures();
50: //assertEquals(2, lst.size());
51:
52: for (Iterator it = lst.iterator(); it.hasNext();) {
53: HSSFPictureData pict = (HSSFPictureData) it.next();
54: String ext = pict.suggestFileExtension();
55: byte[] data = pict.getData();
56: if (ext.equals("jpeg")) {
57: //try to read image data using javax.imageio.* (JDK 1.4+)
58: BufferedImage jpg = ImageIO
59: .read(new ByteArrayInputStream(data));
60: assertNotNull(jpg);
61: assertEquals(192, jpg.getWidth());
62: assertEquals(176, jpg.getHeight());
63: } else if (ext.equals("png")) {
64: //try to read image data using javax.imageio.* (JDK 1.4+)
65: BufferedImage png = ImageIO
66: .read(new ByteArrayInputStream(data));
67: assertNotNull(png);
68: assertEquals(300, png.getWidth());
69: assertEquals(300, png.getHeight());
70: } else {
71: //TODO: test code for PICT, WMF and EMF
72: }
73: }
74:
75: }
76: }
|