Package ca.cgjennings.imageio
Class SimpleImageWriter
- java.lang.Object
-
- ca.cgjennings.imageio.SimpleImageWriter
-
public class SimpleImageWriter extends java.lang.ObjectA simple image writer writes images to output streams and files. It provides a simplified mechanism for configuring basic format, compression, and metadata options compared to theimageiolibrary.Note:
it is important todispose()of instances of this class when finished with them, as many of the underlying image encoders consume significant native resources.- Since:
- 1.0
- Author:
- Chris Jennings
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringFORMAT_BMPFormat identifier for BMP images.static java.lang.StringFORMAT_GIFFormat identifier for GIF89a images.static java.lang.StringFORMAT_JPEGFormat identifier for JPEG images.static java.lang.StringFORMAT_JPEG2000Format identifier for JPEG2000 part 1 images.static java.lang.StringFORMAT_PNGFormat identifier for PNG images.
-
Constructor Summary
Constructors Constructor Description SimpleImageWriter()Creates an image writer for the PNG format using the default locale.SimpleImageWriter(java.lang.String fileFormat)Creates an image writer for the requested format using the default locale.SimpleImageWriter(java.lang.String fileFormat, java.util.Locale locale)Create aSimpleImageWriterthat will produce images in the format specified byfileFormat.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voiddispose()Causes resources used by this writer to be released, including native resources.protected voidfinalize()Deprecated.java.lang.StringgetComment()Return the comment to be embedded in files written by this writer, if possible.floatgetCompressionQuality()Returns the current compression quality.java.lang.String[]getCompressionQualityDescriptions()Return an array of locale-dependent descriptions of the available compression quality settings.java.lang.String[]getCompressionTypes()Returns an array of the supported compression methods.float[]getCompressionValues()Return an array of quality values indicating the ranges of compression qualities covered by the strings returned bygetCompressionValues().java.lang.StringgetFormat()Get the name of the file format this instance will write files in.static WritableImageFormat[]getImageFormats()Returns an array of registered image formats.floatgetPixelsPerInch()Return the resolution to be written with images, in pixels per inch.floatgetPixelsPerMillimetre()Return the resolution to be written with images, in pixels per mm.booleanisCompressionEnabled()Returnstrueif compression is enabled.booleanisCompressionOptional()Returnstrueif disabling compression has any effect.booleanisCompressionSupported()Returnstrueif one or more compression methods are supported.booleanisLossless()Returntrueif the format's compression method is lossless.booleanisMetadataEnabled()Returnstrueif comment and resolution metadata will be written to output files.booleanisProgressiveScan()Returnstrueif progressive encoding is enabled.booleanisProgressiveScanConfigurable()Returnstrueif the progressive scan setting can be changed.booleanisTransparencySupported()Returnstrueif the file format for this writer supports transparency or not.static voidregisterImageFormat(WritableImageFormat wif)Registers a new image format supported for use withSimpleImageWriter.voidsetComment(java.lang.String comment)Set the comment text that will be written along with files created by this writer.voidsetComment(java.lang.String comment, java.util.Locale commentLocale)Set the comment text that will be written along with files created by this writer.voidsetCompressionEnabled(boolean enable)Sets whether compression is enabled.voidsetCompressionQuality(float quality)Control the compression quality during image writing.voidsetCompressionQuality(java.lang.String description)Set image compression according to one of the strings returned bygetCompressionQualityDescriptions().voidsetCompressionType(java.lang.String type)Sets the compression type to use.voidsetEncodingHint(java.lang.Object key, java.lang.Object value)Sets an internal encoding hint.voidsetMetadataEnabled(boolean enable)Sets whether comment and resolution metadata should be written to output files, if supported by the writer and image format.voidsetPixelsPerInch(float ppi)Set the pixels-per-inch resolution that will be written with the file's metadata, if possible.voidsetPixelsPerMillimetre(float ppmm)Set image resolution that will be written with the file's metadata, if possible.voidsetProgressiveScan(boolean useProgressiveScanIfPossible)Sets whether the writer should use progressive encoding, if possible.java.lang.StringtoString()static voidunregisterImageFormat(WritableImageFormat wif)Unregisters a previously registered image format.voidwrite(java.awt.Image image, java.io.File output)Writes the imageimto a file, creating the intermediate stream and closing it on completion.voidwrite(java.awt.Image image, java.io.OutputStream out)Writes the imageimto an output stream.
-
-
-
Field Detail
-
FORMAT_PNG
public static final java.lang.String FORMAT_PNG
Format identifier for PNG images.- See Also:
- Constant Field Values
-
FORMAT_JPEG
public static final java.lang.String FORMAT_JPEG
Format identifier for JPEG images.- See Also:
- Constant Field Values
-
FORMAT_JPEG2000
public static final java.lang.String FORMAT_JPEG2000
Format identifier for JPEG2000 part 1 images.- See Also:
- Constant Field Values
-
FORMAT_GIF
public static final java.lang.String FORMAT_GIF
Format identifier for GIF89a images.- See Also:
- Constant Field Values
-
FORMAT_BMP
public static final java.lang.String FORMAT_BMP
Format identifier for BMP images.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SimpleImageWriter
public SimpleImageWriter()
Creates an image writer for the PNG format using the default locale.
-
SimpleImageWriter
public SimpleImageWriter(java.lang.String fileFormat)
Creates an image writer for the requested format using the default locale.- Parameters:
fileFormat- the file extension or name of a supported file format- Throws:
java.lang.UnsupportedOperationException- iffileFormatis not a known type.- See Also:
SimpleImageWriter(java.lang.String, java.util.Locale)
-
SimpleImageWriter
public SimpleImageWriter(java.lang.String fileFormat, java.util.Locale locale)Create aSimpleImageWriterthat will produce images in the format specified byfileFormat. ThefileFormatparameter must either be the name or file extension of a format registered with theimageiolibrary.- Parameters:
fileFormat- name of file format to be produced by this writerlocale- the preferred locale for labels and messages- Throws:
java.lang.UnsupportedOperationException- iffileFormatis not a known type.
-
-
Method Detail
-
getFormat
public java.lang.String getFormat()
Get the name of the file format this instance will write files in.- Returns:
- the file format specified at construction
-
isTransparencySupported
public boolean isTransparencySupported()
Returnstrueif the file format for this writer supports transparency or not.- Returns:
trueif transparency is supported
-
isCompressionSupported
public boolean isCompressionSupported()
Returnstrueif one or more compression methods are supported.- Returns:
trueif compression is supported
-
isCompressionOptional
public boolean isCompressionOptional()
Returnstrueif disabling compression has any effect.- Returns:
trueif compression can be disabled
-
setCompressionEnabled
public void setCompressionEnabled(boolean enable)
Sets whether compression is enabled. If the compression setting cannot be changed, this will have no effect.- Parameters:
enable- whether to enable compression
-
isCompressionEnabled
public boolean isCompressionEnabled()
Returnstrueif compression is enabled.- Returns:
- whether compression is enabled
-
getCompressionTypes
public java.lang.String[] getCompressionTypes()
Returns an array of the supported compression methods. Most formats support at most one compression method.- Returns:
- an array of supported compression methods, or
null
-
setCompressionType
public void setCompressionType(java.lang.String type)
Sets the compression type to use. If the type isnull, a default type is selected. Otherwise, the type must be one of the types specified bygetCompressionTypes(). Changing the compression type may alter the quality descriptions and values.- Parameters:
type- the name of the compression type to use
-
getCompressionQualityDescriptions
public java.lang.String[] getCompressionQualityDescriptions()
Return an array of locale-dependent descriptions of the available compression quality settings. This method may returnnull, and must returnnullifgetCompressionValues()does.- Returns:
- an array of description
Strings, or null if there are descriptions available - See Also:
getCompressionValues()
-
getCompressionValues
public float[] getCompressionValues()
Return an array of quality values indicating the ranges of compression qualities covered by the strings returned bygetCompressionValues(). The quality settinggetCompressionQualities()[i]covers all compression settings fromgetCompressionValues()[i], inclusive, togetCompressionValues()[i+1], exclusive. The only exception is fori == getCompressionQualities().length - 1, which includes the final value in the range.- Returns:
- an array of description
Strings, or null if there are descriptions available
-
isLossless
public boolean isLossless()
Returntrueif the format's compression method is lossless. A lossless format does not degrade in quality when written.- Returns:
trueif the selected format does not lose information
-
setCompressionQuality
public void setCompressionQuality(java.lang.String description)
Set image compression according to one of the strings returned bygetCompressionQualityDescriptions(). The compression level will be set to the midpoint of the thedescription's value range.- Parameters:
description- the quality description string matching the desired quality level- Throws:
java.lang.IllegalArgumentException- ifdescriptionis not a valid quality description- See Also:
getCompressionQualityDescriptions(),setCompressionQuality(float)
-
setCompressionQuality
public void setCompressionQuality(float quality)
Control the compression quality during image writing. Ifqualityis between 0 and 1 (inclusive), then a relative quality is requested, with a value of 0 emphasizing maximum compression and a value of 1 requesting maximum image quality. (If the format is lossless, then image quality is not affected.) Ifqualityis less than 0, then a default compression level will be selected.- Parameters:
quality- the relative quality (0-1), or a negative value to request default compression
-
getCompressionQuality
public float getCompressionQuality()
Returns the current compression quality.- Returns:
- the compression quality level, from 0 to 1
-
setProgressiveScan
public void setProgressiveScan(boolean useProgressiveScanIfPossible)
Sets whether the writer should use progressive encoding, if possible. Progressive scan images can be displayed in multiple passes of increasing quality.- Parameters:
useProgressiveScanIfPossible- iftrue, the writer will write images in progressive scans if the format supports it
-
isProgressiveScan
public boolean isProgressiveScan()
Returnstrueif progressive encoding is enabled.- Returns:
trueis progressive scan is enabled
-
isProgressiveScanConfigurable
public boolean isProgressiveScanConfigurable()
Returnstrueif the progressive scan setting can be changed. Some formats do not support progressive scans, while other formats support it inherently.- Returns:
trueif the progressive scan setting is alterable
-
setPixelsPerInch
public void setPixelsPerInch(float ppi)
Set the pixels-per-inch resolution that will be written with the file's metadata, if possible. The default resolution is 72 pixels per inch.- Parameters:
ppi- the image resolution, in pixels per inch
-
getPixelsPerInch
public float getPixelsPerInch()
Return the resolution to be written with images, in pixels per inch.- Returns:
- the image resolution, in pixels per inch
-
setPixelsPerMillimetre
public void setPixelsPerMillimetre(float ppmm)
Set image resolution that will be written with the file's metadata, if possible. The resolution is specified in pixels per millimetre. As a convenience,setPixelsPerInch(float)is also provided.The default resolution is 72 pixels per inch (approximately 2.8 pixels per mm). Images written with the writer will specify a horizontal and vertical resolution as close to this value as the file format allows.
- Parameters:
ppmm- the intended image resolution, in pixels per mm- See Also:
setPixelsPerInch(float)
-
getPixelsPerMillimetre
public float getPixelsPerMillimetre()
Return the resolution to be written with images, in pixels per mm. As a convenience,getPixelsPerInch()is also provided. Images written with the writer will specify a horizontal and vertical resolution as close to this value as the file format allows.- Returns:
- the current image resolution to use for writing, pixels per mm
- See Also:
getPixelsPerInch()
-
setComment
public void setComment(java.lang.String comment)
Set the comment text that will be written along with files created by this writer. Ifcommentisnull, the current comment is cleared. The comment will be assumed to be written in the language specified by this writer's locale.- Parameters:
comment- the comment text to be included in the file's metadata.
-
setComment
public void setComment(java.lang.String comment, java.util.Locale commentLocale)Set the comment text that will be written along with files created by this writer. Ifcommentisnull, the current comment (if any) is cleared.- Parameters:
comment- the comment text to be included in the file's metadata.commentLocale- a locale describing the language the comment is written in
-
getComment
public java.lang.String getComment()
Return the comment to be embedded in files written by this writer, if possible. If no comment has been set, returnsnull.- Returns:
- the currently set comment, or
nullif there is no comment
-
isMetadataEnabled
public boolean isMetadataEnabled()
Returnstrueif comment and resolution metadata will be written to output files. Metadata writing may not be supported for all file types.- Returns:
- if metadata writing is enabled
-
setMetadataEnabled
public void setMetadataEnabled(boolean enable)
Sets whether comment and resolution metadata should be written to output files, if supported by the writer and image format.- Parameters:
enable- iftrue, metadata will be written if supported
-
write
public void write(java.awt.Image image, java.io.File output) throws java.io.IOExceptionWrites the imageimto a file, creating the intermediate stream and closing it on completion. If the file exists, it will be overwritten.- Parameters:
image- anImageto be writtenoutput- theFilewhich contains the destination file to be written- Throws:
java.io.IOException- if I/O errors occur during the write operation
-
write
public void write(java.awt.Image image, java.io.OutputStream out) throws java.io.IOExceptionWrites the imageimto an output stream. The stream is left open after writing.- Parameters:
image- theImageto be writtenout- theOutputStreamto writeBufferedImage to- Throws:
java.io.IOException- if I/O errors occur during the write operation
-
dispose
public void dispose()
Causes resources used by this writer to be released, including native resources. The result of calling any method of this writer after callingdispose()is undefined (although the typical result would be aNullPointerException).
-
finalize
@Deprecated protected void finalize() throws java.lang.ThrowableDeprecated.- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
setEncodingHint
public void setEncodingHint(java.lang.Object key, java.lang.Object value)Sets an internal encoding hint. These hints are typically used to work around bugs with specific image format encoders.- Parameters:
key- the hint keyvalue- the hint value
-
registerImageFormat
public static void registerImageFormat(WritableImageFormat wif)
Registers a new image format supported for use withSimpleImageWriter.- Parameters:
wif- a descriptor for the image format- Throws:
java.lang.NullPointerException- if the format isnull
-
unregisterImageFormat
public static void unregisterImageFormat(WritableImageFormat wif)
Unregisters a previously registered image format.- Parameters:
wif- the format to remove
-
getImageFormats
public static WritableImageFormat[] getImageFormats()
Returns an array of registered image formats.- Returns:
- a new array of the supported image formats
-
-