我使用以下代码将ppt幻灯片转换为图片。
BufferedImage imBuff = new BufferedImage(pgsize.width, (pgsize.height) * slides.size(), BufferedImage.TYPE_INT_RGB);
Graphics g = imBuff.getGraphics();
Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(Drawable.FONT_MAP, fontMap);
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(TRANSPARENT);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slides.get(i).draw(graphics);
g.drawImage(img, 0, i * (pgsize.height), null);
对于某些.ppt文件,背景显示为黑色。有谁知道是什么原因以及如何解决这个问题?我怀疑用于创建.ppt文件的办公软件可能是一个因素,但我无法确认。
答案 0 :(得分:0)
感谢您抽出宝贵时间将示例上传到我们的bugzilla - 请参阅#61112
图像的透明部分可以按照Stripping Alpha Channel from Images中的说明进行更换,当然您需要先使用ARGB颜色模型。
我在示例中添加了Arial Unicode字体,但您可能需要different font来覆盖所有使用过的字符。
完整示例:
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.sl.usermodel.SlideShowFactory;
import org.junit.Test;
public class TestRendering {
@Test
public void bug61112() throws Exception {
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("arialuni.ttf"));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
SlideShow<?,?> ppt = SlideShowFactory.create(new File("bug61112.ppt"), null, true);
double scale = 1;
Dimension pgsize = ppt.getPageSize();
int width = (int) (pgsize.width * scale);
int height = (int) (pgsize.height * scale);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = img.createGraphics();
DrawFactory.getInstance(graphics).fixFonts(graphics);
// default rendering options
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);
Map<String,String> fallbackMap = new HashMap<String,String>();
fallbackMap.put("*", font.getFamily());
graphics.setRenderingHint(Drawable.FONT_FALLBACK, fallbackMap);
ppt.getSlides().get(0).draw(graphics);
// Replace the transparent parts of the image
// Set composite rules to paint "behind"
graphics.setComposite(AlphaComposite.DstOver);
graphics.setPaint(Color.WHITE);
graphics.fillRect(0, 0, width, height);
ImageIO.write(img, "PNG", new File("bla.png"));
graphics.dispose();
img.flush();
}
}