⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testpictures.java

📁 java 读写word excel ppt
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ====================================================================   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.==================================================================== */package org.apache.poi.hslf.usermodel;import org.apache.poi.hslf.*;import org.apache.poi.hslf.blip.*;import org.apache.poi.hslf.model.*;import junit.framework.TestCase;import java.io.*;import java.util.Arrays;/** * Test adding/reading pictures * * @author Yegor Kozlov */public class TestPictures extends TestCase{    protected File cwd;    public void setUp() throws Exception {        cwd = new File(System.getProperty("HSLF.testdata.path"));    }    /**     * Test read/write Macintosh PICT     */    public void testPICT() throws Exception {        SlideShow ppt = new SlideShow();        Slide slide = ppt.createSlide();        File img = new File(cwd, "cow.pict");        int idx = ppt.addPicture(img, Picture.PICT);        Picture pict = new Picture(idx);        assertEquals(idx, pict.getPictureIndex());        slide.addShape(pict);        //serialize and read again        ByteArrayOutputStream out = new ByteArrayOutputStream();        ppt.write(out);        out.close();        ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));        //make sure we can read this picture shape and it refers to the correct picture data        Shape[] sh = ppt.getSlides()[0].getShapes();        assertEquals(1, sh.length);        pict = (Picture)sh[0];        assertEquals(idx, pict.getPictureIndex());        //check picture data        PictureData[] pictures = ppt.getPictureData();        //the Picture shape refers to the PictureData object in the Presentation        assertEquals(pict.getPictureData(), pictures[0]);        assertEquals(1, pictures.length);        assertEquals(Picture.PICT, pictures[0].getType());        assertTrue(pictures[0] instanceof PICT);        //compare the content of the initial file with what is stored in the PictureData        byte[] src_bytes = read(img);        byte[] ppt_bytes = pictures[0].getData();        assertEquals(src_bytes.length, ppt_bytes.length);        //in PICT the first 512 bytes are MAC specific and may not be preserved, ignore them        byte[] b1 = new byte[src_bytes.length-512];        System.arraycopy(src_bytes, 512, b1, 0, b1.length);        byte[] b2 = new byte[ppt_bytes.length-512];        System.arraycopy(ppt_bytes, 512, b2, 0, b2.length);        assertTrue(Arrays.equals(b1, b2));    }    /**     * Test read/write WMF     */    public void testWMF() throws Exception {        SlideShow ppt = new SlideShow();        Slide slide = ppt.createSlide();        File img = new File(cwd, "santa.wmf");        int idx = ppt.addPicture(img, Picture.WMF);        Picture pict = new Picture(idx);        assertEquals(idx, pict.getPictureIndex());        slide.addShape(pict);        //serialize and read again        ByteArrayOutputStream out = new ByteArrayOutputStream();        ppt.write(out);        out.close();        ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));        //make sure we can read this picture shape and it refers to the correct picture data        Shape[] sh = ppt.getSlides()[0].getShapes();        assertEquals(1, sh.length);        pict = (Picture)sh[0];        assertEquals(idx, pict.getPictureIndex());        //check picture data        PictureData[] pictures = ppt.getPictureData();        //the Picture shape refers to the PictureData object in the Presentation        assertEquals(pict.getPictureData(), pictures[0]);        assertEquals(1, pictures.length);        assertEquals(Picture.WMF, pictures[0].getType());        assertTrue(pictures[0] instanceof WMF);        //compare the content of the initial file with what is stored in the PictureData        byte[] src_bytes = read(img);        byte[] ppt_bytes = pictures[0].getData();        assertEquals(src_bytes.length, ppt_bytes.length);        //in WMF the first 22 bytes - is a metafile header        byte[] b1 = new byte[src_bytes.length-22];        System.arraycopy(src_bytes, 22, b1, 0, b1.length);        byte[] b2 = new byte[ppt_bytes.length-22];        System.arraycopy(ppt_bytes, 22, b2, 0, b2.length);        assertTrue(Arrays.equals(b1, b2));    }    /**     * Test read/write EMF     */    public void testEMF() throws Exception {        SlideShow ppt = new SlideShow();        Slide slide = ppt.createSlide();        File img = new File(cwd, "wrench.emf");        int idx = ppt.addPicture(img, Picture.EMF);        Picture pict = new Picture(idx);        assertEquals(idx, pict.getPictureIndex());        slide.addShape(pict);        //serialize and read again        ByteArrayOutputStream out = new ByteArrayOutputStream();        ppt.write(out);        out.close();        ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));        //make sure we can get this picture shape and it refers to the correct picture data        Shape[] sh = ppt.getSlides()[0].getShapes();        assertEquals(1, sh.length);        pict = (Picture)sh[0];        assertEquals(idx, pict.getPictureIndex());        //check picture data        PictureData[] pictures = ppt.getPictureData();        //the Picture shape refers to the PictureData object in the Presentation        assertEquals(pict.getPictureData(), pictures[0]);        assertEquals(1, pictures.length);        assertEquals(Picture.EMF, pictures[0].getType());        assertTrue(pictures[0] instanceof EMF);        //compare the content of the initial file with what is stored in the PictureData        byte[] src_bytes = read(img);        byte[] ppt_bytes = pictures[0].getData();        assertTrue(Arrays.equals(src_bytes, ppt_bytes));    }    /**     * Test read/write PNG     */    public void testPNG() throws Exception {        SlideShow ppt = new SlideShow();        Slide slide = ppt.createSlide();        File img = new File(cwd, "tomcat.png");        int idx = ppt.addPicture(img, Picture.PNG);        Picture pict = new Picture(idx);        assertEquals(idx, pict.getPictureIndex());        slide.addShape(pict);        //serialize and read again        ByteArrayOutputStream out = new ByteArrayOutputStream();        ppt.write(out);        out.close();        ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));        //make sure we can read this picture shape and it refers to the correct picture data        Shape[] sh = ppt.getSlides()[0].getShapes();        assertEquals(1, sh.length);        pict = (Picture)sh[0];        assertEquals(idx, pict.getPictureIndex());        //check picture data        PictureData[] pictures = ppt.getPictureData();        //the Picture shape refers to the PictureData object in the Presentation        assertEquals(pict.getPictureData(), pictures[0]);        assertEquals(1, pictures.length);        assertEquals(Picture.PNG, pictures[0].getType());        assertTrue(pictures[0] instanceof PNG);        //compare the content of the initial file with what is stored in the PictureData        byte[] src_bytes = read(img);        byte[] ppt_bytes = pictures[0].getData();        assertTrue(Arrays.equals(src_bytes, ppt_bytes));    }    /**     * Test read/write JPEG     */    public void testJPEG() throws Exception {        SlideShow ppt = new SlideShow();        Slide slide = ppt.createSlide();        File img = new File(cwd, "clock.jpg");        int idx = ppt.addPicture(img, Picture.JPEG);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -