Package net.codecrete.qrbill.canvas
Class PNGCanvas
- java.lang.Object
-
- net.codecrete.qrbill.canvas.AbstractCanvas
-
- net.codecrete.qrbill.canvas.PNGCanvas
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,ByteArrayResult,Canvas
public class PNGCanvas extends AbstractCanvas implements ByteArrayResult
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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface net.codecrete.qrbill.canvas.Canvas
Canvas.LineStyle
-
-
Field Summary
-
Fields inherited from class net.codecrete.qrbill.canvas.AbstractCanvas
MM_TO_PT
-
-
Constructor Summary
Constructors Constructor Description PNGCanvas(double width, double height, int resolution, java.lang.String fontFamilyList)Creates a new instance with the specified image size, resolution and font family.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddRectangle(double x, double y, double width, double height)Adds a rectangle to the pathvoidclose()voidcloseSubpath()Closes the current subpathvoidcubicCurveTo(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 specified position.voidfillPath(int color)Fills the current path and ends itvoidlineTo(double x, double y)Adds a line segment to the open path from the previous point to the specified position.voidmoveTo(double x, double y)Moves the current point of the open path to the specified position.voidputText(java.lang.String text, double x, double y, int fontSize, boolean isBold)Adds text to the graphics.voidsaveAs(java.nio.file.Path path)Saves the resulting PNG image to the specified path.voidsetTransformation(double translateX, double translateY, double rotate, double scaleX, double scaleY)Sets a translation, rotation and scaling for the subsequent operationsvoidstartPath()Starts a path that can be filled or strokedvoidstrokePath(double strokeWidth, int color)Strokes the current path and ends itvoidstrokePath(double strokeWidth, int color, Canvas.LineStyle lineStyle)Strokes the current path and ends itbyte[]toByteArray()Gets the resulting graphics as a byte array.voidwriteTo(java.io.OutputStream os)Writes the resulting PNG image to the specified output stream.-
Methods inherited from class net.codecrete.qrbill.canvas.AbstractCanvas
getAscender, getDescender, getLineHeight, getTextWidth, putTextLines, setupFontMetrics, splitLines
-
-
-
-
Constructor Detail
-
PNGCanvas
public PNGCanvas(double width, double height, int resolution, java.lang.String fontFamilyList)Creates a new instance with the specified image size, resolution and font family.It is recommended to use at least 144 dpi for a readable result.
The first font family in the list is used.
- Parameters:
width- image width, in mmheight- image height, in mmresolution- resolution of the result (in dpi)fontFamilyList- list of font families (comma separated, CSS syntax)
-
-
Method Detail
-
setTransformation
public void setTransformation(double translateX, double translateY, double rotate, double scaleX, double scaleY)Description copied from interface:CanvasSets a translation, rotation and scaling for the subsequent operationsBefore a new translation is applied, the coordinate system is reset to it's original state.
The transformations are applied in the order translation, rotation, scaling.
- Specified by:
setTransformationin interfaceCanvas- Parameters:
translateX- translation in x direction (in mm)translateY- translation in y direction (in mm)rotate- rotation angle, in radiansscaleX- 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:CanvasAdds text to the graphics.The text position refers to the left most point on the text's baseline.
-
startPath
public void startPath()
Description copied from interface:CanvasStarts a path that can be filled or stroked
-
moveTo
public void moveTo(double x, double y)Description copied from interface:CanvasMoves the current point of the open path to the specified position.
-
lineTo
public void lineTo(double x, double y)Description copied from interface:CanvasAdds a line segment to the open path from the previous point to the specified position.
-
cubicCurveTo
public void cubicCurveTo(double x1, double y1, double x2, double y2, double x, double y)Description copied from interface:CanvasAdds a cubic Beziér curve to the open path going from the previous point to the specified position. Two control points control the curve- Specified by:
cubicCurveToin interfaceCanvas- Parameters:
x1- x-coordinate of first control pointy1- y-coordinate of first control pointx2- x-coordinate of second control pointy2- y-coordinate of second control pointx- x-coordinate of positiony- y-coordinate of position
-
addRectangle
public void addRectangle(double x, double y, double width, double height)Description copied from interface:CanvasAdds a rectangle to the path- Specified by:
addRectanglein interfaceCanvas- 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:CanvasCloses the current subpath- Specified by:
closeSubpathin interfaceCanvas
-
fillPath
public void fillPath(int color)
Description copied from interface:CanvasFills the current path and ends it
-
strokePath
public void strokePath(double strokeWidth, int color) throws java.io.IOExceptionDescription copied from interface:CanvasStrokes the current path and ends itThe path is stroked with a solid line.
- Specified by:
strokePathin interfaceCanvas- Parameters:
strokeWidth- the stroke width (in pt)color- the stroke color (expressed similar to HTML, e.g. 0xffffff for white)- Throws:
java.io.IOException- thrown if the graphics cannot be generated
-
strokePath
public void strokePath(double strokeWidth, int color, Canvas.LineStyle lineStyle)Description copied from interface:CanvasStrokes the current path and ends it- Specified by:
strokePathin interfaceCanvas- Parameters:
strokeWidth- the stroke width (in pt)color- the stroke color (expressed similar to HTML, e.g. 0xffffff for white)lineStyle- the line style
-
toByteArray
public byte[] toByteArray() throws java.io.IOExceptionDescription copied from interface:ByteArrayResultGets the resulting graphics as a byte array.- Specified by:
toByteArrayin interfaceByteArrayResult- Returns:
- the byte array
- Throws:
java.io.IOException- thrown if the construction of the byte array fails
-
writeTo
public void writeTo(java.io.OutputStream os) throws java.io.IOExceptionWrites the resulting PNG image to the specified output stream.- Parameters:
os- the output stream- Throws:
java.io.IOException- thrown if the image cannot be written
-
saveAs
public void saveAs(java.nio.file.Path path) throws java.io.IOExceptionSaves the resulting PNG image to the specified path.- Parameters:
path- the path to write to- Throws:
java.io.IOException- thrown if the image cannot be written
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
-