Bug 46153 - Export the shape to images
Summary: Export the shape to images
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSLF (show other bugs)
Version: 3.5-dev
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-11-05 07:30 UTC by Jie Han
Modified: 2008-11-28 07:38 UTC (History)
1 user (show)



Attachments
I put all PowerPoint shapes in the slide (147.50 KB, application/octet-stream)
2008-11-05 07:30 UTC, Jie Han
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jie Han 2008-11-05 07:30:15 UTC
Created attachment 22833 [details]
I put all PowerPoint shapes in the slide

When I use the following code to export the shape to jpeg file, I trace the process and if the shape type is ShapeGroup in the Picuture's draw function 
the getPictureData() function always get null value

--------------------------
public void draw(Graphics2D graphics){
        AffineTransform at = graphics.getTransform();
        ShapePainter.paint(this, graphics);
 
        PictureData data = getPictureData();
        data.draw(graphics, this);

        graphics.setTransform(at);
    }


Below is my code:
---------------------------
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hslf.model.AutoShape;
import org.apache.poi.hslf.model.Freeform;
import org.apache.poi.hslf.model.Line;
import org.apache.poi.hslf.model.MovieShape;
import org.apache.poi.hslf.model.OLEShape;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.Shape;
import org.apache.poi.hslf.model.ShapeGroup;
import org.apache.poi.hslf.model.SimpleShape;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.Table;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.usermodel.PictureData;
import org.apache.poi.hslf.usermodel.SlideShow;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class TestShape {
	public static void main(String... strings) {
		String path = "./src/testfiles/demo1.ppt";
		try {
			InputStream is = new FileInputStream(path);
			SlideShow ppt = new SlideShow(is);
			Slide[] slides = ppt.getSlides();
			int i = 0;
			// PPTXMLDump xdump = new PPTXMLDump();
			int shpCount = 0;
			for (Slide slide : slides) {

				for (Shape shp : slide.getShapes()) {
					Rectangle anchor = shp.getAnchor();
					BufferedImage img = new BufferedImage(anchor.width + 1,
							anchor.height + 1, BufferedImage.TYPE_INT_RGB);
					Graphics2D graphics = img.createGraphics();

					graphics.translate(-anchor.x, -anchor.y);
					
					if (shp instanceof Line) {
						((Line) shp).draw(graphics);
					} else if (shp instanceof Picture) {
						((Picture) shp).draw(graphics);
					} else if (shp instanceof TextBox) {
						((TextBox) shp).draw(graphics);
					} else if (shp instanceof MovieShape) {
						((MovieShape) shp).draw(graphics);
					} else if (shp instanceof OLEShape) {
						((OLEShape) shp).draw(graphics);
					} else if (shp instanceof Table) {
						((Table) shp).draw(graphics);
					} else if (shp instanceof Freeform) {
						((Freeform) shp).draw(graphics);
					} else if (shp instanceof AutoShape) {
						((AutoShape) shp).draw(graphics);
					} else if (shp instanceof ShapeGroup) {
						((ShapeGroup) shp).draw(graphics);
					}
					TestShape.writeToFile("./src/shape/slide-"
							 + shp.getShapeId() + ".jpg", img);
					System.out.println(shp.getClass().getName());
					// }
				}
			}
		} catch (Exception ex) {
			System.out.println(ex.toString());
		}
	}

	public static void writeToFile(String fileName, BufferedImage img) {
		try {
			File file = new File(fileName);
			FileOutputStream out;
			out = new FileOutputStream(file);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder
					.getDefaultJPEGEncodeParam(img);
			param.setQuality(2f, true);
			encoder.setJPEGEncodeParam(param);
			encoder.encode(img);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ImageFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
Comment 1 Yegor Kozlov 2008-11-16 05:44:01 UTC
Try the latest svn build. Your code works fine with it.

Yegor