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.hssf.usermodel;
18:
19: import junit.framework.TestCase;
20:
21: import java.io.IOException;
22: import java.io.FileInputStream;
23: import java.io.ByteArrayOutputStream;
24:
25: /**
26: * Test <code>HSSFPicture</code>.
27: *
28: * @author Yegor Kozlov (yegor at apache.org)
29: */
30: public class TestHSSFPicture extends TestCase {
31:
32: public void testResize() throws Exception {
33: HSSFWorkbook wb = new HSSFWorkbook();
34: HSSFSheet sh1 = wb.createSheet();
35: HSSFPatriarch p1 = sh1.createDrawingPatriarch();
36:
37: int idx1 = loadPicture("src/resources/logos/logoKarmokar4.png",
38: wb);
39: HSSFPicture picture1 = p1.createPicture(new HSSFClientAnchor(),
40: idx1);
41: HSSFClientAnchor anchor1 = picture1.getPreferredSize();
42:
43: //assert against what would BiffViewer print if we insert the image in xls and dump the file
44: assertEquals(0, anchor1.getCol1());
45: assertEquals(0, anchor1.getRow1());
46: assertEquals(1, anchor1.getCol2());
47: assertEquals(9, anchor1.getRow2());
48: assertEquals(0, anchor1.getDx1());
49: assertEquals(0, anchor1.getDy1());
50: assertEquals(848, anchor1.getDx2());
51: assertEquals(240, anchor1.getDy2());
52: }
53:
54: /**
55: * Copied from org.apache.poi.hssf.usermodel.examples.OfficeDrawing
56: */
57: private static int loadPicture(String path, HSSFWorkbook wb)
58: throws IOException {
59: int pictureIndex;
60: FileInputStream fis = null;
61: ByteArrayOutputStream bos = null;
62: try {
63: fis = new FileInputStream(path);
64: bos = new ByteArrayOutputStream();
65: int c;
66: while ((c = fis.read()) != -1)
67: bos.write(c);
68: pictureIndex = wb.addPicture(bos.toByteArray(),
69: HSSFWorkbook.PICTURE_TYPE_PNG);
70: } finally {
71: if (fis != null)
72: fis.close();
73: if (bos != null)
74: bos.close();
75: }
76: return pictureIndex;
77: }
78:
79: }
|