Class ImageTranscoder

  • All Implemented Interfaces:
    Transcoder
    Direct Known Subclasses:
    JPEGTranscoder, PNGTranscoder, TIFFTranscoder

    public abstract class ImageTranscoder
    extends SVGAbstractTranscoder
    This class enables to transcode an input to an image of any format.

    Two transcoding hints (KEY_WIDTH and KEY_HEIGHT) can be used to respectively specify the image width and the image height. If only one of these keys is specified, the transcoder preserves the aspect ratio of the original image.

    The KEY_BACKGROUND_COLOR defines the background color to use for opaque image formats, or the background color that may be used for image formats that support alpha channel.

    The KEY_AOI represents the area of interest to paint in device space.

    Three additional transcoding hints that act on the SVG processor can be specified:

    KEY_LANGUAGE to set the default language to use (may be used by a <switch> SVG element for example), KEY_USER_STYLESHEET_URI to fix the URI of a user stylesheet, and KEY_MM_PER_PIXEL to specify the number of millimeters in each pixel .

    • Field Detail

      • KEY_BACKGROUND_COLOR

        public static final TranscodingHints.Key KEY_BACKGROUND_COLOR
        The image background paint key.
        Key: KEY_BACKGROUND_COLOR
        Value: Paint
        Default: null
        Required: No
        Description: Specify the background color to use. The color is required by opaque image formats and is used by image formats that support alpha channel.
      • KEY_FORCE_TRANSPARENT_WHITE

        public static final TranscodingHints.Key KEY_FORCE_TRANSPARENT_WHITE
        The forceTransparentWhite key.
        Key: KEY_FORCE_TRANSPARENT_WHITE
        Value: Boolean
        Default: false
        Required: No
        Description: It controls whether the encoder should force the image's fully transparent pixels to be fully transparent white instead of fully transparent black. This is useful when the encoded file is displayed in a browser which does not support transparency correctly and lets the image display with a white background instead of a black background.
        However, note that the modified image will display differently over a white background in a viewer that supports transparency.
        Not all Transcoders use this key (in particular some formats can't preserve the alpha channel at all in which case this is not used).
    • Constructor Detail

      • ImageTranscoder

        protected ImageTranscoder()
        Constructs a new ImageTranscoder.
    • Method Detail

      • transcode

        protected void transcode​(org.w3c.dom.Document document,
                                 java.lang.String uri,
                                 TranscoderOutput output)
                          throws TranscoderException
        Transcodes the specified Document as an image in the specified output.
        Overrides:
        transcode in class SVGAbstractTranscoder
        Parameters:
        document - the document to transcode
        uri - the uri of the document or null if any
        output - the ouput where to transcode
        Throws:
        TranscoderException - if an error occured while transcoding
      • createRenderer

        protected ImageRenderer createRenderer()
        Method so subclasses can modify the Renderer used to render document.
      • forceTransparentWhite

        protected void forceTransparentWhite​(java.awt.image.BufferedImage img,
                                             java.awt.image.SinglePixelPackedSampleModel sppsm)
        Converts an image so that viewers which do not support the alpha channel will see a white background (and not a black one).
        Parameters:
        img - the image to convert
        sppsm -
      • createImage

        public abstract java.awt.image.BufferedImage createImage​(int width,
                                                                 int height)
        Creates a new image with the specified dimension.
        Parameters:
        width - the image width in pixels
        height - the image height in pixels
      • writeImage

        public abstract void writeImage​(java.awt.image.BufferedImage img,
                                        TranscoderOutput output)
                                 throws TranscoderException
        Writes the specified image to the specified output.
        Parameters:
        img - the image to write
        output - the output where to store the image
        Throws:
        TranscoderException - if an error occured while storing the image