![](/img/trans.png)
[英]Chinese characters converted to squares when using APACHE POI to convert PPT to Image
[英]Ppt background is black when using Apache POI to convert slide to image
我正在使用下面的代碼將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文件的Office軟件可能是一個因素,但是我無法確認。
感謝您抽出寶貴的時間將示例上傳到我們的Bugzilla-請參閱#61112
圖像的透明部分可以按照從圖像中剝離Alpha通道中所述進行替換,當然,您首先需要使用ARGB顏色模型。
我已經在示例中添加了Arial Unicode字體,但是您可能需要一個涵蓋所有使用的字符的不同字體 。
完整的例子:
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.