Hi Venkat

Thanks for your response. I need this renderer to support GIF and JPEG, I was looking the code of TIFF Renderer to adapt it to a JPEG and GIF renderer, and I saw that "stopRenderer()" was the best place to write code using ImageIO to convert Java2D/AWT renderer work.

The most of the work was change the static strings of types (image/tiff), and I create the corresponding maker, configurator ,etc ...

If someone need it, I attach the classes here.

Here I leave a example of use the renderer:

Thanks for all. I'm pleased to help a little.

// OutputStream out = what you want
           FopFactory fopFactory = FopFactory.newInstance();
           RendererFactory rendererFactory = null;
           FOUserAgent foUserAgent = null;

           rendererFactory = fopFactory.getRendererFactory();
rendererFactory.addRendererMaker(new MultiRendererMaker("jpeg"));
           foUserAgent = fopFactory.newFOUserAgent();
           rendererFactory.createRenderer(foUserAgent, "image/jpeg");
Fop fop = fopFactory.newFop("image/jpeg", foUserAgent, out);

//Use fop as normal case



Venkat Reddy escribió:
Hi,

As per the below documentation, Java2D/AWT renderer used in FOP only supports PNG/TIFF output formats. For more information, read the following document...
http://xmlgraphics.apache.org/fop/trunk/output.html#bitmap

Venkat.

Diego Medina wrote:
Hi everyone

I have been using Fop from long time ago, and to start I have to say thanks for that great job. Now recently, I have a new need to get another output different of pdf. In later versions I see that mimeType image/jpeg is a valid constant to output format (but I don't test this functionality until now) but in versión 0.95 said " Don't know how to handle "image/jpeg" as an output format". I was studding the fop config file and other configuration issues, but all my test didn't works. Is possible to render a jpeg output without adding another render to fop 0.95?. In case of not, how can I add my own renderer to render a image/jpeg?.

Thanks so much.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]




---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

/*
 * 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.
 */

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import org.apache.batik.util.MimeTypeConstants;
import org.apache.commons.logging.Log;

import org.apache.xmlgraphics.image.writer.ImageWriterParams;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.render.java2d.Java2DRenderer;

/**
 * MultiRenderer for the multi Renderer.
 * A TIFFRendererMaker adaptation to get a valid redender using the
 * available ImageIO formats
 * @author dmedina ([email protected])
 * @author aj2r ([email protected])
 * @author raiben ([email protected])
 */
public class MultiRenderer extends Java2DRenderer {

    /** ImageWriter parameters */
    private ImageWriterParams writerParams;
    /** Image Type as parameter for the BufferedImage constructor (see BufferedImage.TYPE_*) */
    private int bufferedImageType = BufferedImage.TYPE_INT_RGB;
    private OutputStream outputStream;
    private String mimeType = null;

    /** {...@inheritdoc} */
    public String getMimeType() {
        return this.mimeType;
    }

    /** Creates a multi render renderer. */
    /**
     *
     * @param mimeType valid string for ImageIO to choose the format
     * examples (jpeg, jpg, gif, ....)
     * @see ImageIO
     */
    public MultiRenderer(String mimeType) {
        writerParams = new ImageWriterParams();
        this.mimeType = mimeType;
    }

    /**
     * {...@inheritdoc}
     *          org.apache.fop.apps.FOUserAgent)
     */
    public void setUserAgent(FOUserAgent foUserAgent) {
        super.setUserAgent(foUserAgent);

        //Set target resolution
        int dpi = Math.round(userAgent.getTargetResolution());
        writerParams.setResolution(dpi);
    }

    /** {...@inheritdoc} */
    public void startRenderer(OutputStream outputStream) throws IOException {
        this.outputStream = outputStream;
        super.startRenderer(outputStream);
    }

    /** {...@inheritdoc} */
    public void stopRenderer() throws IOException {
        super.stopRenderer();
        log.debug("Starting encoding ...");

        // Creates lazy iterator over generated page images
        Iterator pageImagesItr = new LazyPageImagesIterator(getNumberOfPages(), log);


        ImageIO.write((RenderedImage) pageImagesItr.next(), mimeType, outputStream);
        if (pageImagesItr.hasNext()) {
            log.error("Image encoder does not support multiple images. Only the first page" + " has been produced.");
        }

        // Cleaning
        outputStream.flush();
        clearViewportList();
        log.debug("encoding done.");
    }

    /** {...@inheritdoc} */
    protected BufferedImage getBufferedImage(int bitmapWidth, int bitmapHeight) {
        return new BufferedImage(bitmapWidth, bitmapHeight, bufferedImageType);
    }

    public void setBufferedImageType(int bufferedImageType) {
        this.bufferedImageType = bufferedImageType;
    }

    public ImageWriterParams getWriterParams() {
        return writerParams;
    }
     /** Private inner class to lazy page rendering. */
    private class LazyPageImagesIterator implements Iterator {

        /** logging instance */
        private Log log;
        private int count;
        private int current = 0;

        /**
         * Main constructor
         * @param c number of pages to iterate over
         * @param log the logger to use (this is a hack so this compiles under JDK 1.3)
         */
        public LazyPageImagesIterator(int c, Log log) {
            count = c;
            this.log = log;
        }

        public boolean hasNext() {
            return current < count;
        }

        public Object next() {
            if (log.isDebugEnabled()) {
                log.debug("[" + (current + 1) + "]");
            }
            // Renders current page as image
            BufferedImage pageImage = null;
            try {
                pageImage = getPageImage(current++);
            } catch (FOPException e) {
                log.error(e);
                return null;
            }
            return pageImage;
        }

        public void remove() {
            throw new UnsupportedOperationException(
                    "Method 'remove' is not supported.");
        }
    }
}

/*
 * 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.
 */

/* MultiRendererConfigurator.java */

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.render.PrintRendererConfigurator;
import org.apache.fop.render.Renderer;

/**
 * MultiRendererConfigurator for the multi Renderer.
 * A TIFFRendererMaker adaptation to get a valid redender using the
 * available ImageIO formats
 * @author dmedina ([email protected])
 * @author aj2r ([email protected])
 * @author ([email protected])
 */
public class MultiRendererConfigurator extends PrintRendererConfigurator {

    /**
     * Default constructor
     * @param userAgent user agent
     */
    public MultiRendererConfigurator(FOUserAgent userAgent) {
        super(userAgent);
    }

    /**
     * Configure the Multi renderer. Get the configuration to be used for
     * compression
     * @param renderer multi renderer
     * @throws FOPException fop exception
     * {...@inheritdoc}
     */
    public void configure(Renderer renderer) throws FOPException {
        super.configure(renderer);
    }
}
/*
 * 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.
 */

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.render.AbstractRendererMaker;
import org.apache.fop.render.Renderer;
import org.apache.fop.render.RendererConfigurator;

/**
 * MultiRendererMaker for the multi Renderer.
 * A TIFFRendererMaker adaptation to get a valid redender using the
 * available ImageIO formats
 * @author dmedina ([email protected])
 * @author aj2r ([email protected])
 * @author ([email protected])
 */
public class MultiRendererMaker extends AbstractRendererMaker {

    /**
     * You can add here the mimeTypes necessary to notify the FopFactory what formats
     * support this render, available from ImageIO
     */
    private static final String[] MIMES = new String[]{MimeConstants.MIME_JPEG, MimeConstants.MIME_GIF};
    private String mimeType = null;

    /**
     * Constructor
     * @param mimeType valid string for ImageIO to choose the format
     * examples (jpeg, jpg, gif, ....)
     */
    public MultiRendererMaker(String mimeType) {
        super();
        this.mimeType = mimeType;
    }

    /** {...@inheritdoc} */
    public Renderer makeRenderer(FOUserAgent userAgent) {
        return new MultiRenderer(mimeType);
    }

    /** {...@inheritdoc} */
    public RendererConfigurator getConfigurator(FOUserAgent userAgent) {
        return new MultiRendererConfigurator(userAgent);
    }

    /** {...@inheritdoc} */
    public boolean needsOutputStream() {
        return true;
    }

    /** {...@inheritdoc} */
    public String[] getSupportedMimeTypes() {
        return MIMES;
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to