FileDocCategorySizeDatePackage
ImageWriter.javaAPI DocAndroid 1.5 API31555Wed May 06 22:41:54 BST 2009javax.imageio

ImageWriter

public abstract class ImageWriter extends Object implements ImageTranscoder
The ImageWriter class is an abstract class for encoding images. ImageWriter objects are instantiated by the service provider interface, ImageWriterSpi class, for the specific format. ImageWriterSpi class should be registered with the IIORegistry, which uses them for format recognition and presentation of available format readers and writers.
since
Android 1.0

Fields Summary
protected Locale[]
availableLocales
The available locales.
protected Locale
locale
The locale.
protected ImageWriterSpi
originatingProvider
The originating provider.
protected Object
output
The output.
protected List
progressListeners
The progress listeners.
protected List
warningListeners
The warning listeners.
protected List
warningLocales
The warning locales.
private boolean
aborted
The aborted.
Constructors Summary
protected ImageWriter(ImageWriterSpi originatingProvider)
Instantiates a new ImageWriter.

param
originatingProvider the ImageWriterSpi which instantiates this ImageWriter.

        this.originatingProvider = originatingProvider;
    
Methods Summary
public synchronized voidabort()
Requests an abort operation for current writing operation.

        aborted = true;
    
protected synchronized booleanabortRequested()
Checks whether or not a request to abort the current write operation has been made successfully.

return
true, if the request to abort the current write operation has been made successfully, false otherwise.

        return aborted;
    
public voidaddIIOWriteProgressListener(javax.imageio.event.IIOWriteProgressListener listener)
Adds the IIOWriteProgressListener listener.

param
listener the IIOWriteProgressListener listener.

        if (listener == null) {
            return;
        }

        if (progressListeners == null) {
            progressListeners = new ArrayList<IIOWriteProgressListener>();
        }

        progressListeners.add(listener);
    
public voidaddIIOWriteWarningListener(javax.imageio.event.IIOWriteWarningListener listener)
Adds the IIOWriteWarningListener.

param
listener the IIOWriteWarningListener listener.

        if (listener == null) {
            return;
        }

        if (warningListeners == null) {
            warningListeners = new ArrayList<IIOWriteWarningListener>();
            warningLocales = new ArrayList<Locale>();
        }

        warningListeners.add(listener);
        warningLocales.add(getLocale());
    
public booleancanInsertEmpty(int imageIndex)
Returns true if a new empty image can be inserted at the specified index.

param
imageIndex the specified index of image.
return
true if a new empty image can be inserted at the specified index, false otherwise.
throws
IOException Signals that an I/O exception has occurred.

        return checkOutputReturnFalse();
    
public booleancanInsertImage(int imageIndex)
Returns true if a new image can be inserted at the specified index.

param
imageIndex the specified index of image.
return
true if a new image can be inserted at the specified index, false otherwise.
throws
IOException Signals that an I/O exception has occurred.

        return checkOutputReturnFalse();
    
public booleancanRemoveImage(int imageIndex)
Returns true if the image with the specified index can be removed.

param
imageIndex the specified index of image.
return
true if the image with the specified index can be removed, false otherwise.
throws
IOException Signals that an I/O exception has occurred.

        return checkOutputReturnFalse();
    
public booleancanReplaceImageMetadata(int imageIndex)
Returns true if metadata of the image with the specified index can be replaced.

param
imageIndex the specified image index.
return
true if metadata of the image with the specified index can be replaced, false otherwise.
throws
IOException if an I/O exception has occurred.

        return checkOutputReturnFalse();
    
public booleancanReplacePixels(int imageIndex)
Returns true if pixels of the image with the specified index can be replaced by the replacePixels methods.

param
imageIndex the image's index.
return
true if pixels of the image with the specified index can be replaced by the replacePixels methods, false otherwise.
throws
IOException Signals that an I/O exception has occurred.

        return checkOutputReturnFalse();
    
public booleancanReplaceStreamMetadata()
Returns true if the stream metadata presented in the output can be removed.

return
true if the stream metadata presented in the output can be removed, false otherwise.
throws
IOException if an I/O exception has occurred.

        return checkOutputReturnFalse();
    
public booleancanWriteEmpty()
Returns true if the writing of a complete image stream which contains a single image is supported with undefined pixel values and associated metadata and thumbnails to the output.

return
true if the writing of a complete image stream which contains a single image is supported, false otherwise.
throws
IOException if an I/O exception has occurred.

        return checkOutputReturnFalse();
    
public booleancanWriteRasters()
Returns true if the methods which taken an IIOImageParameter can deal with a Raster source image.

return
true if the methods which taken an IIOImageParameter can deal with a Raster source image, false otherwise.

        return false;
    
public booleancanWriteSequence()
Returns true if the writer can add an image to stream that already contains header information.

return
if the writer can add an image to stream that already contains header information, false otherwise.

        return false;
    
private final booleancheckOutputReturnFalse()
Check output return false.

return
true, if successful.

        if (getOutput() == null) {
            throw new IllegalStateException("getOutput() == null!");
        }
        return false;
    
protected synchronized voidclearAbortRequest()
Clears all previous abort request, and abortRequested returns false after calling this method.

        aborted = false;
    
public abstract javax.imageio.metadata.IIOMetadataconvertImageMetadata(javax.imageio.metadata.IIOMetadata iioMetadata, javax.imageio.ImageTypeSpecifier imageTypeSpecifier, javax.imageio.ImageWriteParam imageWriteParam)

public abstract javax.imageio.metadata.IIOMetadataconvertStreamMetadata(javax.imageio.metadata.IIOMetadata iioMetadata, javax.imageio.ImageWriteParam imageWriteParam)

public voiddispose()
Disposes of any resources.

        // def impl. does nothing according to the spec.
    
public voidendInsertEmpty()
Ends the insertion of a new image.

throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidendReplacePixels()
Ends the replace pixels operation.

throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidendWriteEmpty()
Ends an empty write operation.

throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidendWriteSequence()
Ends the sequence of write operations.

throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public java.util.Locale[]getAvailableLocales()
Gets an array of available locales.

return
an of array available locales.

        if (availableLocales == null) {
            return null;
        }

        return availableLocales.clone();
    
public abstract javax.imageio.metadata.IIOMetadatagetDefaultImageMetadata(javax.imageio.ImageTypeSpecifier imageType, javax.imageio.ImageWriteParam param)
Gets an IIOMetadata object that contains default values for encoding an image with the specified type.

param
imageType the ImageTypeSpecifier.
param
param the ImageWriteParam.
return
the IIOMetadata object.

public abstract javax.imageio.metadata.IIOMetadatagetDefaultStreamMetadata(javax.imageio.ImageWriteParam param)
Gets an IIOMetadata object that contains default values for encoding a stream of images.

param
param the ImageWriteParam.
return
the IIOMetadata object.

public javax.imageio.ImageWriteParamgetDefaultWriteParam()
Gets the default write param. Gets a new ImageWriteParam object for this ImageWriter with the current Locale.

return
a new ImageWriteParam object for this ImageWriter.

        return new ImageWriteParam(getLocale());
    
public java.util.LocalegetLocale()
Gets the current locale of this ImageWriter.

return
the current locale of this ImageWriter.

        return locale;
    
public intgetNumThumbnailsSupported(javax.imageio.ImageTypeSpecifier imageType, javax.imageio.ImageWriteParam param, javax.imageio.metadata.IIOMetadata streamMetadata, javax.imageio.metadata.IIOMetadata imageMetadata)
Gets the number of thumbnails supported by the format being written with supported image type, image write parameters, stream, and image metadata objects.

param
imageType the ImageTypeSpecifier.
param
param the image's parameters.
param
streamMetadata the stream metadata.
param
imageMetadata the image metadata.
return
the number of thumbnails supported.

        return 0;
    
public javax.imageio.spi.ImageWriterSpigetOriginatingProvider()
Gets the ImageWriterSpi which instantiated this ImageWriter.

return
the ImageWriterSpi.

        return originatingProvider;
    
public java.lang.ObjectgetOutput()
Gets the output object that was set by setOutput method.

return
the output object such as ImageOutputStream, or null if it is not set.

        return output;
    
public java.awt.Dimension[]getPreferredThumbnailSizes(javax.imageio.ImageTypeSpecifier imageType, javax.imageio.ImageWriteParam param, javax.imageio.metadata.IIOMetadata streamMetadata, javax.imageio.metadata.IIOMetadata imageMetadata)
Gets the preferred thumbnail sizes. Gets an array of Dimensions with the sizes for thumbnail images as they are encoded in the output file or stream.

param
imageType the ImageTypeSpecifier.
param
param the ImageWriteParam.
param
streamMetadata the stream metadata.
param
imageMetadata the image metadata.
return
the preferred thumbnail sizes.

        return null;
    
public voidprepareInsertEmpty(int imageIndex, javax.imageio.ImageTypeSpecifier imageType, int width, int height, javax.imageio.metadata.IIOMetadata imageMetadata, java.util.List thumbnails, javax.imageio.ImageWriteParam param)
Prepares insertion of an empty image by requesting the insertion of a new image into an existing image stream.

param
imageIndex the image index.
param
imageType the image type.
param
width the width of the image.
param
height the height of the image.
param
imageMetadata the image metadata, or null.
param
thumbnails the array thumbnails for this image, or null.
param
param the ImageWriteParam, or null.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidprepareReplacePixels(int imageIndex, java.awt.Rectangle region)
Prepares the writer to call the replacePixels method for the specified region.

param
imageIndex the image's index.
param
region the specified region.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidprepareWriteEmpty(javax.imageio.metadata.IIOMetadata streamMetadata, javax.imageio.ImageTypeSpecifier imageType, int width, int height, javax.imageio.metadata.IIOMetadata imageMetadata, java.util.List thumbnails, javax.imageio.ImageWriteParam param)
Prepares the writer for writing an empty image by beginning the process of writing a complete image stream that contains a single image with undefined pixel values, metadata and thumbnails, to the output.

param
streamMetadata the stream metadata.
param
imageType the image type.
param
width the width of the image.
param
height the height of the image.
param
imageMetadata the image's metadata, or null.
param
thumbnails the image's thumbnails, or null.
param
param the image's parameters, or null.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidprepareWriteSequence(javax.imageio.metadata.IIOMetadata streamMetadata)
Prepares a stream to accept calls of writeToSequence method using the metadata object.

param
streamMetadata the stream metadata.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
protected voidprocessImageComplete()
Processes image completion by calling imageComplete method of registered IIOWriteProgressListeners.

        if (null != progressListeners) {
            for (IIOWriteProgressListener listener : progressListeners) {
                listener.imageComplete(this);
            }
        }
    
protected voidprocessImageProgress(float percentageDone)
Processes the current percentage of image completion by calling imageProgress method of registered IIOWriteProgressListener.

param
percentageDone the percentage done.

        if (null != progressListeners) {
            for (IIOWriteProgressListener listener : progressListeners) {
                listener.imageProgress(this, percentageDone);
            }
        }
    
protected voidprocessImageStarted(int imageIndex)
Processes the start of an image read by calling their imageStarted method of registered IIOWriteProgressListeners.

param
imageIndex the image index.

        if (null != progressListeners) {
            for (IIOWriteProgressListener listener : progressListeners) {
                listener.imageStarted(this, imageIndex);
            }
        }
    
protected voidprocessThumbnailComplete()
Processes the completion of a thumbnail read by calling their thumbnailComplete method of registered IIOWriteProgressListeners.

        if (progressListeners != null) {
            for (IIOWriteProgressListener listener : progressListeners) {
                listener.thumbnailComplete(this);
            }
        }
    
protected voidprocessThumbnailProgress(float percentageDone)
Processes the current percentage of thumbnail completion by calling their thumbnailProgress method of registered IIOWriteProgressListeners.

param
percentageDone the percentage done.

        if (progressListeners != null) {
            for (IIOWriteProgressListener listener : progressListeners) {
                listener.thumbnailProgress(this, percentageDone);
            }
        }
    
protected voidprocessThumbnailStarted(int imageIndex, int thumbnailIndex)
Processes the start of a thumbnail read by calling thumbnailStarted method of registered IIOWriteProgressListeners.

param
imageIndex the image index.
param
thumbnailIndex the thumbnail index.

        if (progressListeners != null) {
            for (IIOWriteProgressListener listener : progressListeners) {
                listener.thumbnailStarted(this, imageIndex, thumbnailIndex);
            }
        }
    
protected voidprocessWarningOccurred(int imageIndex, java.lang.String warning)
Processes a warning message by calling warningOccurred method of registered IIOWriteWarningListeners.

param
imageIndex the image index.
param
warning the warning.

        if (null == warning) {
            throw new NullPointerException("warning message should not be NULL");
        }
        if (null != warningListeners) {
            for (IIOWriteWarningListener listener : warningListeners) {
                listener.warningOccurred(this, imageIndex, warning);
            }
        }
    
protected voidprocessWarningOccurred(int imageIndex, java.lang.String bundle, java.lang.String key)
Processes a warning message by calling warningOccurred method of registered IIOWriteWarningListeners with string from ResourceBundle.

param
imageIndex the image index.
param
bundle the name of ResourceBundle.
param
key the keyword.

        if (warningListeners != null) { // Don't check the parameters
            return;
        }

        if (bundle == null) {
            throw new IllegalArgumentException("baseName == null!");
        }
        if (key == null) {
            throw new IllegalArgumentException("keyword == null!");
        }

        // Get the context class loader and try to locate the bundle with it
        // first
        ClassLoader contextClassloader = AccessController
                .doPrivileged(new PrivilegedAction<ClassLoader>() {
                    public ClassLoader run() {
                        return Thread.currentThread().getContextClassLoader();
                    }
                });

        // Iterate through both listeners and locales
        int n = warningListeners.size();
        for (int i = 0; i < n; i++) {
            IIOWriteWarningListener listener = warningListeners.get(i);
            Locale locale = warningLocales.get(i);

            // Now try to get the resource bundle
            ResourceBundle rb;
            try {
                rb = ResourceBundle.getBundle(bundle, locale, contextClassloader);
            } catch (MissingResourceException e) {
                try {
                    rb = ResourceBundle.getBundle(bundle, locale);
                } catch (MissingResourceException e1) {
                    throw new IllegalArgumentException("Bundle not found!");
                }
            }

            try {
                String warning = rb.getString(key);
                listener.warningOccurred(this, imageIndex, warning);
            } catch (MissingResourceException e) {
                throw new IllegalArgumentException("Resource is missing!");
            } catch (ClassCastException e) {
                throw new IllegalArgumentException("Resource is not a String!");
            }
        }
    
protected voidprocessWriteAborted()
Processes that the writing has been aborted by calling writeAborted method of registered IIOWriteProgressListeners.

        if (progressListeners != null) {
            for (IIOWriteProgressListener listener : progressListeners) {
                listener.writeAborted(this);
            }
        }
    
public voidremoveAllIIOWriteProgressListeners()
Removes the all IIOWriteProgressListener listeners.

        progressListeners = null;
    
public voidremoveAllIIOWriteWarningListeners()
Removes the all IIOWriteWarningListener listeners.

        warningListeners = null;
        warningLocales = null;
    
public voidremoveIIOWriteProgressListener(javax.imageio.event.IIOWriteProgressListener listener)
Removes the specified IIOWriteProgressListener listener.

param
listener the registered IIOWriteProgressListener to be removed.

        if (progressListeners != null && listener != null) {
            if (progressListeners.remove(listener) && progressListeners.isEmpty()) {
                progressListeners = null;
            }
        }
    
public voidremoveIIOWriteWarningListener(javax.imageio.event.IIOWriteWarningListener listener)
Removes the specified IIOWriteWarningListener listener.

param
listener the registered IIOWriteWarningListener listener to be removed.

        if (warningListeners == null || listener == null) {
            return;
        }

        int idx = warningListeners.indexOf(listener);
        if (idx > -1) {
            warningListeners.remove(idx);
            warningLocales.remove(idx);

            if (warningListeners.isEmpty()) {
                warningListeners = null;
                warningLocales = null;
            }
        }
    
public voidremoveImage(int imageIndex)
Removes the image with the specified index from the stream.

param
imageIndex the image's index.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidreplaceImageMetadata(int imageIndex, javax.imageio.metadata.IIOMetadata imageMetadata)
Replaces image metadata of the image with specified index.

param
imageIndex the image's index.
param
imageMetadata the image metadata.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidreplacePixels(java.awt.image.RenderedImage image, javax.imageio.ImageWriteParam param)
Replaces a part of an image presented in the output with the specified RenderedImage.

param
image the RenderedImage.
param
param the ImageWriteParam.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidreplacePixels(java.awt.image.Raster raster, javax.imageio.ImageWriteParam param)
Replaces a part of an image presented in the output with the specified Raster.

param
raster the Raster.
param
param the ImageWriteParam.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidreplaceStreamMetadata(javax.imageio.metadata.IIOMetadata streamMetadata)
Replaces the stream metadata of the output with new IIOMetadata.

param
streamMetadata the new stream metadata.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidreset()
Resets this ImageWriter.

        setOutput(null);
        setLocale(null);
        removeAllIIOWriteWarningListeners();
        removeAllIIOWriteProgressListeners();
        clearAbortRequest();
    
public voidsetLocale(java.util.Locale locale)
Sets the locale of this ImageWriter.

param
locale the new locale.

        if (locale == null) {
            this.locale = null;
            return;
        }

        Locale[] locales = getAvailableLocales();
        boolean validLocale = false;
        if (locales != null) {
            for (int i = 0; i < locales.length; i++) {
                if (locale.equals(locales[i])) {
                    validLocale = true;
                    break;
                }
            }
        }

        if (validLocale) {
            this.locale = locale;
        } else {
            throw new IllegalArgumentException("Invalid locale!");
        }
    
public voidsetOutput(java.lang.Object output)
Sets the specified Object to the output of this ImageWriter.

param
output the Object which represents destination, it can be ImageOutputStream or other objects.

        if (output != null) {
            ImageWriterSpi spi = getOriginatingProvider();
            if (null != spi) {
                Class[] outTypes = spi.getOutputTypes();
                boolean supported = false;
                for (Class<?> element : outTypes) {
                    if (element.isInstance(output)) {
                        supported = true;
                        break;
                    }
                }
                if (!supported) {
                    throw new IllegalArgumentException("output " + output + " is not supported");
                }
            }
        }
        this.output = output;
    
private final voidunsupportedOperation()
Unsupported operation.

        if (getOutput() == null) {
            throw new IllegalStateException("getOutput() == null!");
        }
        throw new UnsupportedOperationException("Unsupported write variant!");
    
public voidwrite(javax.imageio.IIOImage image)
Writes a completed image stream that contains the specified image, default metadata, and thumbnails to the output.

param
image the specified image to be written.
throws
IOException if an I/O exception has occurred during writing.

        write(null, image, null);
    
public voidwrite(java.awt.image.RenderedImage image)
Writes a completed image stream that contains the specified rendered image, default metadata, and thumbnails to the output.

param
image the specified RenderedImage to be written.
throws
IOException if an I/O exception has occurred during writing.

        write(null, new IIOImage(image, null, null), null);
    
public abstract voidwrite(javax.imageio.metadata.IIOMetadata streamMetadata, javax.imageio.IIOImage image, javax.imageio.ImageWriteParam param)
Writes a completed image stream that contains the specified image, metadata and thumbnails to the output.

param
streamMetadata the stream metadata, or null.
param
image the specified image to be written, if canWriteRaster() method returns false, then Image must contain only RenderedImage.
param
param the ImageWriteParam, or null.
throws
IOException if an error occurs during writing.

public voidwriteInsert(int imageIndex, javax.imageio.IIOImage image, javax.imageio.ImageWriteParam param)
Inserts image into existing output stream.

param
imageIndex the image index where an image will be written.
param
image the specified image to be written.
param
param the ImageWriteParam, or null.
throws
IOException if an I/O exception has occurred.

        unsupportedOperation();
    
public voidwriteToSequence(javax.imageio.IIOImage image, javax.imageio.ImageWriteParam param)
Writes the specified image to the sequence.

param
image the image to be written.
param
param the ImageWriteParam, or null.
throws
IOException if an I/O exception has occurred during writing.

        unsupportedOperation();