我正在使用PowerPointPresentation(.ppt)将.ppt文件转换为ANDROID PLATFORM中的.png图像。为了实现这一点,我使用Apache Poi Api。在slide class中,有一个方法draw(java.awt.Graphics2D graphics)
,其中传递的参数为Graphics2D
。但在Android中,此类不受支持,而是使用canvas
。我的问题是这个,这就是Android的任何等效draw(java.awt.Graphics2D graphics)
方法,因为在否定的情况下我必须重写Api。所以在这里给我这个代码的最佳建议
public final class PPT2PNG {
public static void main(String args[]) throws Exception {
File file = new File("C:/Users/THIYAGARAJAN/Desktop/ppt52.ppt");
int scale = 10;
int slidenum = 0;
FileInputStream is = new FileInputStream(file);
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
int width = (int) (pgsize.width * scale);
int height = (int) (pgsize.height * scale);
System.out.println("w" + width + "h" + height);
Slide[] slide = ppt.getSlides();
System.out.println(slide.length);
for (int i = 0; i < slide.length; i++) {
String title = slide[i].getTitle();
System.out.println("Rendering slide " + slide[i].getSlideNumber() + (title == null ? "" : ": " + title));
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, width, height));
graphics.scale((double) width / pgsize.width, (double) height / pgsize.height);
slide[i].draw(graphics);
//String fname = file.getAbsolutePath() .replaceAll(".ppt", "-" + (i+1) + ".png");
File fname = new File("C:/Users/THIYAGARAJAN/Desktop/" + i + ".png");
FileOutputStream out = new FileOutputStream(fname);
ImageIO.write(img, "png", out);
out.close();
}
}
private static void usage() {
System.out.println("Usage: PPT2PNG [-scale <scale> -slide <num>] ppt");
}
}