org.apache.poi.xslf.usermodel.XSLFImageRenderer Maven / Gradle / Ivy
/*
* ====================================================================
* 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.xslf.usermodel;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.util.Beta;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* For now this class renders only images supported by the javax.imageio.ImageIO
* framework. Subclasses can override this class to support other formats, for
* example, Use Apache batik to render WMF:
*
*
*
* public class MyImageRendener extends XSLFImageRendener{
* public boolean drawImage(Graphics2D graphics, XSLFPictureData data, Rectangle2D anchor){
* boolean ok = super.drawImage(graphics, data, anchor);
* if(!ok){
* // see what type of image we are
* String contentType = data.getPackagePart().getContentType();
* if(contentType.equals("image/wmf")){
* // use Apache Batik to handle WMF
* // see http://xmlgraphics.apache.org/batik/
* }
*
* }
* return ok;
* }
* }
*
*
*
* and then pass this class to your instance of java.awt.Graphics2D:
*
*
*
* graphics.setRenderingHint(XSLFRenderingHint.IMAGE_RENDERER, new MyImageRendener());
*
*
*
* @author Yegor Kozlov
*/
@Beta
public class XSLFImageRenderer {
/**
* Render picture data into the supplied graphics
*
* @return true if the picture data was successfully rendered
*/
public boolean drawImage(Graphics2D graphics, XSLFPictureData data,
Rectangle2D anchor) {
try {
BufferedImage img = ImageIO.read(data.getPackagePart().getInputStream());
double sx = anchor.getWidth()/img.getWidth();
double sy = anchor.getHeight()/img.getHeight();
double tx = anchor.getX();
double ty = anchor.getY();
AffineTransform at = new AffineTransform(sx, 0, 0, sy, tx, ty) ;
graphics.drawRenderedImage(img, at);
return true;
} catch (Exception e) {
return false;
}
}
/**
* Create a buffered image from the supplied package part.
* This method is called to create texture paints.
*
* @return a BufferedImage
containing the decoded
* contents of the input, or null
.
*/
public BufferedImage readImage(PackagePart packagePart) throws IOException {
return ImageIO.read(packagePart.getInputStream());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy