Class PNGCanvas

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, Canvas

    public class PNGCanvas
    extends AbstractCanvas
    Canvas for generating PNG files.

    PNGs are not an optimal file format for QR bills. Vector formats such a SVG or PDF are of better quality and use far less processing power to generate.

    • Constructor Summary

      Constructors 
      Constructor Description
      PNGCanvas​(int resolution)
      Creates a new instance
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addRectangle​(double x, double y, double width, double height)
      Adds a rectangle to the path
      void close()  
      void closeSubpath()
      Closes the current subpath
      void cubicCurveTo​(double x1, double y1, double x2, double y2, double x, double y)
      Adds a cubic Beziér curve to the open path going from the previous point to the speicifed position.
      void fillPath​(int color)
      Fills the current path and ends it
      byte[] getResult()
      Returns the generated graphics as a byte array
      void lineTo​(double x, double y)
      Adds a line segment to the open path from the previous point to the speicifed position.
      void moveTo​(double x, double y)
      Moves the current point of the open path to the specified position.
      void putText​(java.lang.String text, double x, double y, int fontSize, boolean isBold)
      Adds text to the graphics.
      void setTransformation​(double translateX, double translateY, double rotate, double scaleX, double scaleY)
      Sets a translation, rotation and scaling for the subsequent operations
      void setupPage​(double width, double height, java.lang.String fontFamilyList)
      Sets up the page
      void startPath()
      Starts a path that can be filled or stroked
      void strokePath​(double strokeWidth, int color)
      Strokes the current path and ends it
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PNGCanvas

        public PNGCanvas​(int resolution)
        Creates a new instance

        It is recommended to use at least 144 dpi for a readable result.

        Parameters:
        resolution - resolution of the result (in dpi)
    • Method Detail

      • setupPage

        public void setupPage​(double width,
                              double height,
                              java.lang.String fontFamilyList)
        Description copied from interface: Canvas
        Sets up the page

        The page (and graphics context) is not valid until this method has been called.

        The font family can be specified as a comma separated list, e.g. "Helvetica,Arial,sans". The first family will be used to calculate text widths. For formats that support it (e.g. SVG), the entire list will be used in the generated graphics file. Other format will just use the first one.

        Parameters:
        width - width of page (in mm)
        height - height of page (in mm)
        fontFamilyList - font family to use
      • setTransformation

        public void setTransformation​(double translateX,
                                      double translateY,
                                      double rotate,
                                      double scaleX,
                                      double scaleY)
        Description copied from interface: Canvas
        Sets a translation, rotation and scaling for the subsequent operations

        Before a new translation is applied, the coordinate system is reset to it's original state after page setup (see setupPage).

        The transformations are applied in the order translation, rotation, scaling.

        Parameters:
        translateX - translation in x direction (in mm)
        translateY - translation in y direction (in mm)
        rotate - rotation angle, in radians
        scaleX - scale factor in x direction (1.0 = no scaling)
        scaleY - scale factor in y direction (1.0 = no scaling)
      • putText

        public void putText​(java.lang.String text,
                            double x,
                            double y,
                            int fontSize,
                            boolean isBold)
        Description copied from interface: Canvas
        Adds text to the graphics.

        The text position refers to the left most point on the text's baseline.

        Parameters:
        text - the text
        x - x position of the text's start (in mm)
        y - y position of the text's top (in mm)
        fontSize - the font size (in pt)
        isBold - indicates if the text is in bold or regular weight
      • startPath

        public void startPath()
        Description copied from interface: Canvas
        Starts a path that can be filled or stroked
      • moveTo

        public void moveTo​(double x,
                           double y)
        Description copied from interface: Canvas
        Moves the current point of the open path to the specified position.
        Parameters:
        x - x-coordinate of position
        y - y-coordinate of position
      • lineTo

        public void lineTo​(double x,
                           double y)
        Description copied from interface: Canvas
        Adds a line segment to the open path from the previous point to the speicifed position.
        Parameters:
        x - x-coordinate of position
        y - y-coordinate of position
      • cubicCurveTo

        public void cubicCurveTo​(double x1,
                                 double y1,
                                 double x2,
                                 double y2,
                                 double x,
                                 double y)
        Description copied from interface: Canvas
        Adds a cubic Beziér curve to the open path going from the previous point to the speicifed position. Two control points control the curve
        Parameters:
        x1 - x-coordinate of first control point
        y1 - y-coordinate of first control point
        x2 - x-coordinate of second control point
        y2 - y-coordinate of second control point
        x - x-coordinate of position
        y - y-coordinate of position
      • addRectangle

        public void addRectangle​(double x,
                                 double y,
                                 double width,
                                 double height)
        Description copied from interface: Canvas
        Adds a rectangle to the path
        Parameters:
        x - the rectangle's left position (in mm)
        y - the rectangle's top position (in mm)
        width - the rectangle's width (in mm)
        height - rectangle's height (in mm)
      • closeSubpath

        public void closeSubpath()
        Description copied from interface: Canvas
        Closes the current subpath
      • fillPath

        public void fillPath​(int color)
        Description copied from interface: Canvas
        Fills the current path and ends it
        Parameters:
        color - the fill color (expressed similar to HTML, e.g. 0xffffff for white)
      • strokePath

        public void strokePath​(double strokeWidth,
                               int color)
        Description copied from interface: Canvas
        Strokes the current path and ends it
        Parameters:
        strokeWidth - the stroke width (in pt)
        color - the stroke color (expressed similar to HTML, e.g. 0xffffff for white)
      • getResult

        public byte[] getResult()
                         throws java.io.IOException
        Description copied from interface: Canvas
        Returns the generated graphics as a byte array

        After this method was called, the page is no longer valid.

        Returns:
        the byte array
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • close

        public void close()